主要是是
addBatch ,executeBatch ,clearBatch 三个方法.
官方示例代码:
public void batchUpdate() throws SQLException { Statement stmt = null; try { this.con.setAutoCommit(false);//关闭commit stmt = this.con.createStatement();//创建statement stmt.addBatch( "INSERT INTO COFFEES " + "VALUES(‘Amaretto‘, 49, 9.99, 0, 0)"); stmt.addBatch( "INSERT INTO COFFEES " + "VALUES(‘Hazelnut‘, 49, 9.99, 0, 0)"); stmt.addBatch( "INSERT INTO COFFEES " + "VALUES(‘Amaretto_decaf‘, 49, " + "10.99, 0, 0)"); stmt.addBatch( "INSERT INTO COFFEES " + "VALUES(‘Hazelnut_decaf‘, 49, " + "10.99, 0, 0)"); int [] updateCounts = stmt.executeBatch();//编译多条sql语句 this.con.commit();//commit,之后sql语句去哪不执行 } catch(BatchUpdateException b) { JDBCTutorialUtilities.printBatchUpdateException(b); } catch(SQLException ex) { JDBCTutorialUtilities.printSQLException(ex); } finally { if (stmt != null) { stmt.close(); } this.con.setAutoCommit(true); } }
原文地址:https://www.cnblogs.com/--zz/p/9812775.html
时间: 2024-10-30 18:51:45