码迷,mamicode.com
首页 > 数据库 > 详细

JDBC 批处理

时间:2018-10-18 20:17:03      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:关闭   lex   stat   count   处理   try   add   row   执行   

主要是是

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 批处理

标签:关闭   lex   stat   count   处理   try   add   row   执行   

原文地址:https://www.cnblogs.com/--zz/p/9812775.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!