JDBC 批处理
Posted --zz
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDBC 批处理相关的知识,希望对你有一定的参考价值。
主要是是
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); } }
以上是关于JDBC 批处理的主要内容,如果未能解决你的问题,请参考以下文章