首页 > 代码库 > PreparedStatement批量处理和事务

PreparedStatement批量处理和事务

[java] view plain copy
 
  1. PreparedStatement批量处理和事务代码如下:     
  2.   
  3.  /*   
  4.  * PreparedStatement:   
  5.  1.addBatch() 将一组参数添加到 PreparedStatement对象内部   
  6.  2.executeBatch() 将一批参数提交给数据库来执行,如果全部命令执行成功,则返回更新计数组成的数组。   
  7.  *    
  8.  */    
  9. public class PreparedStatementCommitAndRollback {     
  10.     public static void main(String args[]) {     
  11.         Connection con = null;     
  12.         PreparedStatement pstm = null;     
  13.     
  14.         try {     
  15.             // 1. 建立与数据库的连接     
  16.             con = JDBCUtil.getConnection();     
  17.             // 2. 执行sql语句     
  18.             // 1).先创建PreparedStatement语句(发送slq请求):     
  19.             pstm = con.prepareStatement("insert into student values(?,?,?,?)");     
  20.             con.setAutoCommit(false);//1,首先把Auto commit设置为false,不让它自动提交     
  21.             // 2) 设置sql语句1     
  22.             pstm.setInt(1, 33);     
  23.             pstm.setString(2,"wangqin");     
  24.             pstm.setString(3, "c++");     
  25.             pstm.setDouble(4, 78.5);     
  26.             // 3) 将一组参数添加到此 PreparedStatement 对象的批处理命令中。     
  27.             pstm.addBatch();     
  28.             // 2) 设置sql语句2     
  29.             pstm.setInt(1, 34);     
  30.             pstm.setString(2,"wuytun");     
  31.             pstm.setString(3, "c");     
  32.             pstm.setDouble(4, 77);     
  33.             // 3) 将一组参数添加到此 PreparedStatement 对象的批处理命令中。     
  34.             pstm.addBatch();     
  35.             // 2) 设置sql语句3     
  36.             pstm.setInt(1, 31);     
  37.             pstm.setString(2,"tetet");     
  38.             pstm.setString(3, "c++");     
  39.             pstm.setDouble(4, 90);     
  40.             // 3) 将一组参数添加到此 PreparedStatement 对象的批处理命令中。     
  41.             pstm.addBatch();     
  42.             // 2) 设置sql语句4     
  43.             pstm.setInt(1, 32);     
  44.             pstm.setString(2,"liug");     
  45.             pstm.setString(3, "c");     
  46.             pstm.setDouble(4, 50);     
  47.             // 3) 将一组参数添加到此 PreparedStatement 对象的批处理命令中。     
  48.             pstm.addBatch();     
  49.             // 4) 将一批参数提交给数据库来执行,如果全部命令执行成功,则返回更新计数组成的数组。     
  50.             pstm.executeBatch();     
  51.             System.out.println("插入成功!");     
  52.             // 若成功执行完所有的插入操作,则正常结束     
  53.             con.commit();//2,进行手动提交(commit)     
  54.             System.out.println("提交成功!");     
  55.             con.setAutoCommit(true);//3,提交完成后回复现场将Auto commit,还原为true,     
  56.     
  57.         } catch (SQLException e) {     
  58.             try {     
  59.                 // 若出现异常,对数据库中所有已完成的操作全部撤销,则回滚到事务开始状态     
  60.                 if(!con.isClosed()){     
  61.                     con.rollback();//4,当异常发生执行catch中SQLException时,记得要rollback(回滚);     
  62.                     System.out.println("插入失败,回滚!");     
  63.                     con.setAutoCommit(true);     
  64.                 }     
  65.            } catch (SQLException e1) {     
  66.                e1.printStackTrace();     
  67.            }     
  68.         }finally{     
  69.             JDBCUtil.closePreparedStatement(pstm);     
  70.             JDBCUtil.closeConnection(con);     
  71.         }     
  72.     }     
  73. }   
[java] view plain copy
 
  1.   
[java] view plain copy
 
  1. 这是Statement的代码,同上:  
  2. stm = con.createStatement();     
  3.            con.setAutoCommit(false);     
  4.            // 若不出现异常,则继续执行到try语句完,否则跳转到catch语句中     
  5.             stm.addBatch("insert into student values(23,‘tangbao‘,‘高数‘,100)");     
  6.             stm.addBatch("insert into student values(24,‘王定‘,‘c#‘,98)");     
  7.             stm.addBatch("insert into student values(25,‘王国云‘,‘java‘,90)");     
  8.             stm.addBatch("insert into student values(26,‘溜出‘,‘英语‘,89)");     
  9.             stm.addBatch("insert into student values(27,‘wqde‘,‘java‘,63)");     
  10.             /*   
  11.              * int[] executeBatch() throws   
  12.              * SQLException将一批命令提交给数据库来执行,如果全部命令执行成功,则返回更新计数组成的数组。   
  13.              */    
  14.             stm.executeBatch();     

PreparedStatement批量处理和事务