时间:2021-07-01 10:21:17 帮助过:4人阅读
官方示例代码:
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);
}
}
JDBC 批处理
标签:关闭 lex stat count 处理 try add row 执行