packagejdbcOper;import java.sql.*;importjava.util.ArrayList;//ZC: 基类(基本全是static方法)
public classTjdbcOperA
{public voidfinalize()
{
System.out.println("TjdbcOperA.finalize");//ConnectionClose();
}private static Class> Fclazz = null;//加载驱动
protected staticBoolean DriverLoad(String _strDriver)
{//_strDriver 类似 "oracle.jdbc.driver.OracleDriver"
if (Fclazz == null)
{try{
Fclazz=Class.forName(_strDriver);
}catch(ClassNotFoundException e) {
Fclazz= null;
e.printStackTrace();
}
}return (Fclazz != null);
}private static Connection Fconn = null;protected staticConnection ConnectionOpen(String _strUrl, String _strUserName, String _strPassword)
{//_strUrl 类似 "jdbc:oracle:thin:@localhost:1521:ORCLHSP"//_strUserName 类似 "scott"//_strPassword 类似 "tiger"
if (Fconn == null)
{try{
Fconn=DriverManager.getConnection(_strUrl, _strUserName, _strPassword);
}catch(SQLException e) {
Fconn= null;
e.printStackTrace();
}
}returnFconn;
}protected static voidConnectionClose()
{if (Fconn != null)
{try{
Fconn.close();
}catch(SQLException e) {
e.printStackTrace();
}
Fconn= null;
}
}//ZC: 单条的 insert语句//ZC: 返回 成功的SQL语句条数
protected static intInsertSingle(Connection _conn, String _strInsert, String[] _strsParameter)
{int iRtn = 0;
PreparedStatement ps= null;try{
_conn.setAutoCommit(true);
ps=_conn.prepareStatement(_strInsert);if (_strsParameter != null)
{for (int i=0; i<_strsparameter.length i>
ps.setString(i+1, _strsParameter[i]);
}
iRtn=ps.executeUpdate();
}catch(SQLException e) {
e.printStackTrace();
}finally{if (ps != null)
{try{
ps.close();
}catch(SQLException e) {//e.printStackTrace();
}//ZC: (1)、若 上面关闭成功了,这里 赋值为 null,没有任何效果//ZC: (2)、若 上面关闭失败,这里 赋值为 null,以便让垃圾回收机制来处理//ZC: 这里的 ps是局部变量,函数退出后,栈空间回收,也没有变量再指向 PreparedStatement实例 了,应该不用显示的赋值为null了?//ZC: 观察到ps的关闭需要调用close()函数,是否 和 普通的类实例不同?函数退出时 不会自动关闭?//ZC: 然而,记得在哪里见过函数退出后 还能保持局部变量的 机制,是在 C#里还是在java里?
ps = null;
}
}returniRtn;
}//ZC: executeUpdate() 执行单条 insert/delete/update语句//ZC: 返回 成功的SQL语句条数
protected static intExecuteUpdate_Single(Connection _conn, String _str, String[] _strsParameter)
{
PreparedStatement ps= null;try{
_conn.setAutoCommit(true);
ps=_conn.prepareStatement(_str);for (int i=0; i<_strsparameter.length i>
ps.setString(i+1, _strsParameter[i]);int iRtn =ps.executeUpdate();returniRtn;
}catch(SQLException e) {
e.printStackTrace();return 0;
}
}protected static intExecuteUpdate_SingleNoParameter(Connection _conn, String _str)
{
PreparedStatement ps= null;try{
_conn.setAutoCommit(true);
ps=_conn.prepareStatement(_str);int iRtn =ps.executeUpdate();returniRtn;
}catch(SQLException e) {
e.printStackTrace();return 0;
}
}//ZC: 使用的是 单个 PreparedStatement (多个 PreparedStatement 怎么耍?)//ZC: executeUpdate() 执行多条 insert/delete/update语句//ZC: 返回 成功的SQL语句条数(ZC: 如果是insert/delete/update语句的组合,那返回的条数如何计算?加起来?)
protected static intExecuteUpdate_Multi(Connection _conn, String[] _strsSql, String[][] _strssParameter)
{int iRtn = 0;
PreparedStatement ps= null;try{
_conn.setAutoCommit(false);for (int i=0; i<_strssql.length i>
{if (_strssParameter[i] != null)
{
ps=_conn.prepareStatement(_strsSql[i]);for (int j=0; j<_strssparameter j>
ps.setString(j+1, _strssParameter[i][j]);//ZC: 每次得到 PreparedStatement,都要 执行executeUpdate(),不然 它只会执行会有一条语句
iRtn +=ps.executeUpdate();//ZC: 每次获得的都是 新的实例,记得关闭
ps.close();
ps= null;
}
}
_conn.commit();
}catch(SQLException e) {
e.printStackTrace();try{
iRtn= 0;//ZC: 如果回滚也异常了,那怎么办?已经执行的语句 就没办法撤回了?//ZC: 还是说不执行commit()的话,SQL语句默认没有提交?可能是的.
_conn.rollback();
}catch(SQLException e1) {
e1.printStackTrace();
}
}finally{if (ps != null)
{//try {//ps.close();//} catch (SQLException e) {//e.printStackTrace();//}//上面已经有关闭的代码了,这里就不必再关闭了,赋值为null后若有需要 就交给JVM处理吧
ps = null;
}
}returniRtn;
}//ZC: 貌似 PreparedStatement.addBatch(); 和 PreparedStatement.addBatch(sql); 不能组合使用。
public static intBatch_SameSQL(Connection _conn, String _strSql, String[][] _strssParameter)
{int iRtn = 0;
PreparedStatement ps= null;try{
_conn.setAutoCommit(false);
ps=_conn.prepareStatement(_strSql);for (int i=0; i<_strssparameter.length i>
{if (_strssParameter[i] != null)
{for (int j=0; j<_strssparameter j>
ps.setString(j+1, _strssParameter[i][j]);
ps.addBatch();
}
}int[] iRtns =ps.executeBatch();for (int i=0; i
iRtn+=iRtns[i];
_conn.commit();
}catch(SQLException e) {
e.printStackTrace();try{
iRtn= 0;//ZC: 如果回滚也异常了,那怎么办?已经执行的语句 就没办法撤回了?//ZC: 还是说不执行commit()的话,SQL语句默认没有提交?可能是的.
_conn.rollback();
}catch(SQLException e1) {
e1.printStackTrace();
}
}finally{if (ps != null)
{try{
ps.close();
}catch(SQLException e) {
e.printStackTrace();
}
ps= null;
}
}returniRtn;
}//ZC: PreparedStatement.addBatch(sql);中的sql语句不能带?参数。//ZC: ==> ∵ PreparedStatement.addBatch(); 和 PreparedStatement.addBatch(sql); 不能组合使用。//ZC: PreparedStatement.addBatch(sql); 测试下来,貌似只能加入不带参数的SQL语句//ZC: 貌似 一旦执行了"PreparedStatement.addBatch(sql2);","Connection.prepareStatement(sql1)"中传入的参数sql1就无效了...
public static int Batch_DifferentSQLs(Connection _conn, ArrayList_listSql)
{int iRtn = 0;//PreparedStatement ps = null;
Statement st = null;try{//_conn.setAutoCommit(false);//ps = _conn.prepareStatement(_listSql.get(0));//
//for (int i=0; i<_listsql.size i>
//int[] iRtns = ps.executeBatch();//for (int i=0; i
//_conn.commit();
_conn.setAutoCommit(false);
st=_conn.createStatement();for (int i=0; i<_listsql.size i>
st.addBatch(_listSql.get(i));int[] iRtns =st.executeBatch();for (int i=0; i
iRtn+=iRtns[i];
_conn.commit();
}catch(SQLException e) {
e.printStackTrace();try{
iRtn= 0;//ZC: 如果回滚也异常了,那怎么办?已经执行的语句 就没办法撤回了?//ZC: 还是说不执行commit()的话,SQL语句默认没有提交?可能是的.
_conn.rollback();
}catch(SQLException e1) {
e1.printStackTrace();
}
}finally{//if (ps != null)//{//try {//ps.close();//} catch (SQLException e) {//e.printStackTrace();//}//ps = null;//}
if (st != null)
{try{
st.close();
}catch(SQLException e) {
e.printStackTrace();
}
st= null;
}
}returniRtn;
}public staticTselectResult SelectGet(Connection _conn, String _strSql, String[] _strsParameter)
{
PreparedStatement ps= null;try{
_conn.setAutoCommit(true);//加上两个参数:ResultSet.TYPE_SCROLL_INSENSITIVE / ResultSet.CONCUR_READ_ONLY,才能使得结果集指针滚动
ps =_conn.prepareStatement(_strSql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);if (_strsParameter != null)
{for (int i=0; i<_strsparameter.length i>
ps.setString(i+1, _strsParameter[i]);
}
ResultSet rs=ps.executeQuery();
TselectResult selRst= newTselectResult();
selRst.Fps=ps;
selRst.Frs=rs;returnselRst;
}catch(SQLException e) {
e.printStackTrace();
}return null;
}public static voidSelectClose(TselectResult _selRst)
{if (_selRst.Frs != null)
{try{
_selRst.Frs.close();
}catch(SQLException e) {//e.printStackTrace();
}
_selRst.Frs= null;
}if (_selRst.Fps != null)
{try{
_selRst.Fps.close();
}catch(SQLException e) {//e.printStackTrace();
}
_selRst.Fps= null;
}
}public static void main(String[] args) throwsException
{//PreparedStatement批量处理,参考网址:http://mousepc.iteye.com/blog/1131462
DriverLoad("com.mysql.jdbc.Driver");
Connection conn=ConnectionOpen("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8","root", "");
conn.setAutoCommit(false);//insert
String strInsert = "insert into test_z (username,password)values(?,?);";
PreparedStatement ps= conn.prepareStatement("");//update
String strUpdate = "update test_z set password=\"TTT\" where username=\"AAA\";";
ps.addBatch(strInsert);//ps.clearBatch();
ps.setString(1, "111");
ps.setString(2, "222");
ps.addBatch();
ps.addBatch(strUpdate);int[] iRtns =ps.executeBatch();
conn.commit();for (int i=0; i
System.out.println(i+ " : " +iRtns[i]);
ConnectionClose();
}
}