代码中反复开关自动提交没有必要. 其他方面写得还是很不错的.清晰.摘自 http://wangqinqin.iteye.com/blog/547277
PreparedStatement:
1) addBatch()将一组参数添加到PreparedStatement对象内部。
2) executeBatch()将一批参数提交给数据库来执行,如果全部命令执行成功,则返回更新计数组成的数组。
Statement:
1) addBatch(String sql)方法会在批处理缓存中加入一条sql语句。
2) executeBatch()执行批处理缓存中的所有sql语句。
注意:PreparedStatement中使用批量更新时,要先设置好参数后再使用addBatch()方法加入缓存。批量更新中只能使用更改、删除或插入语句。
1 Statement批量处理和事务代码如下: 2 package com.ambow.day20.jdbc.JDBCTestCommitAndRollback; 3 import java.sql.Connection; 4 import java.sql.SQLException; 5 import java.sql.Statement; 6 import com.ambow.day19.jdbc.util.JDBCConAndClo; 7 /**//* 8 *1,首先把Auto commit设置为false,不让它自动提交 9 *2,进行手动提交(commit) 10 *3,提交完成后回复现场将Auto commit,还原为true, 11 *4,当异常发生执行catch中SQLException时,记得要rollback(回滚); 12 * */ 13 public class StatementCommitAndRollbackTest { 14 public static void main(String args[]) { 15 Connection con = null; 16 Statement stm = null; 17 try { 18 con = JDBCConAndClo.getConnectionBao(); 19 stm = con.createStatement(); 20 con.setAutoCommit(false); 21 // 若不出现异常,则继续执行到try语句完,否则跳转到catch语句中 22 stm.addBatch("insert into student values(23,'tangbao','高数',100)"); 23 stm.addBatch("insert into student values(24,'王定','c#',98)"); 24 stm.addBatch("insert into student values(25,'王国云','java',90)"); 25 stm.addBatch("insert into student values(26,'溜出','英语',89)"); 26 stm.addBatch("insert into student values(27,'wqde','java',63)"); 27 /**//* 28 * int[] executeBatch() throws 29 * SQLException将一批命令提交给数据库来执行,如果全部命令执行成功,则返回更新计数组成的数组。 30 */ 31 stm.executeBatch(); 32 System.out.println("插入成功!"); 33 // commit:若成功执行完所有的插入操作,则正常结束 34 con.commit(); 35 System.out.println("提交成功!"); 36 con.setAutoCommit(true); 37 38 } catch (SQLException e) { 39 e.printStackTrace(); 40 try { 41 //rollback: 若出现异常,对数据库中所有已完成的操作全部撤销,则回滚到事务开始状态 42 if (!con.isClosed()) { 43 con.rollback(); 44 System.out.println("提交失败,回滚!"); 45 con.setAutoCommit(true); 46 } 47 } catch (SQLException e1) { 48 e1.printStackTrace(); 49 } finally { 50 JDBCConAndClo.closeStatement(stm); 51 JDBCConAndClo.closeConnection(con); 52 } 53 } 54 } 55 } 56 PreparedStatement批量处理和事务代码如下: 57 package com.ambow.day20.jdbc.JDBCTestCommitAndRollback; 58 import java.sql.Connection; 59 import java.sql.PreparedStatement; 60 import java.sql.SQLException; 61 import com.ambow.day19.jdbc.util.JDBCConAndClo; 62 63 /**//* 64 * PreparedStatement: 65 1.addBatch() 将一组参数添加到 PreparedStatement对象内部 66 2.executeBatch() 将一批参数提交给数据库来执行,如果全部命令执行成功,则返回更新计数组成的数组。 67 * 68 */ 69 public class PreparedStatementCommitAndRollbackTest { 70 public static void main(String args[]) { 71 Connection con = null; 72 PreparedStatement pstm = null; 73 74 try { 75 // 1. 建立与数据库的连接 76 con = JDBCConAndClo.getConnectionBao(); 77 // 2. 执行sql语句 78 // 1).先创建PreparedStatement语句(发送slq请求): 79 pstm = con.prepareStatement("insert into student values(?,?,?,?)"); 80 con.setAutoCommit(false);//1,首先把Auto commit设置为false,不让它自动提交 81 // 2) 设置sql语句1 82 pstm.setInt(1, 33); 83 pstm.setString(2,"wangqin"); 84 pstm.setString(3, "c++"); 85 pstm.setDouble(4, 78.5); 86 // 3) 将一组参数添加到此 PreparedStatement 对象的批处理命令中。 87 pstm.addBatch(); 88 // 2) 设置sql语句2 89 pstm.setInt(1, 34); 90 pstm.setString(2,"wuytun"); 91 pstm.setString(3, "c"); 92 pstm.setDouble(4, 77); 93 // 3) 将一组参数添加到此 PreparedStatement 对象的批处理命令中。 94 pstm.addBatch(); 95 // 2) 设置sql语句3 96 pstm.setInt(1, 31); 97 pstm.setString(2,"tetet"); 98 pstm.setString(3, "c++"); 99 pstm.setDouble(4, 90); 100 // 3) 将一组参数添加到此 PreparedStatement 对象的批处理命令中。 101 pstm.addBatch(); 102 // 2) 设置sql语句4 103 pstm.setInt(1, 32); 104 pstm.setString(2,"liug"); 105 pstm.setString(3, "c"); 106 pstm.setDouble(4, 50); 107 // 3) 将一组参数添加到此 PreparedStatement 对象的批处理命令中。 108 pstm.addBatch(); 109 // 4) 将一批参数提交给数据库来执行,如果全部命令执行成功,则返回更新计数组成的数组。 110 pstm.executeBatch(); 111 System.out.println("插入成功!"); 112 // 若成功执行完所有的插入操作,则正常结束 113 con.commit();//2,进行手动提交(commit) 114 System.out.println("提交成功!"); 115 con.setAutoCommit(true);//3,提交完成后回复现场将Auto commit,还原为true, 116 117 } catch (SQLException e) { 118 e.printStackTrace(); 119 try { 120 // 若出现异常,对数据库中所有已完成的操作全部撤销,则回滚到事务开始状态 121 if(!con.isClosed()) { 122 con.rollback();//4,当异常发生执行catch中SQLException时,记得要rollback(回滚); 123 System.out.println("插入失败,回滚!"); 124 con.setAutoCommit(true); 125 } 126 } catch (SQLException e1) { 127 e1.printStackTrace(); 128 } 129 }finally { 130 JDBCConAndClo.closePreparedStatement(pstm); 131 JDBCConAndClo.closeConnection(con); 132 } 133 } 134 } 135
|