JDBC:批量处理(Statement方法)

1. 什么是批处理?

批量处理执行SQL语句,调用数据库一次来提交多组数据,一次执行完成与数据库之间的交互。

2. 如何实现批处理?

  1. 不使用 JDBC 驱动程序来实现此功能。应使用 DatabaseMetaData.supportsBatchUpdates() 方法来确定目标数据库是否支持批量更新处理。
  2. 如果 JDBC 驱动程序支持此功能,该方法将返回 trueStatementPreparedStatementCallableStatement的addBatch() 方法用于将单个语句添加到批处理。
  3. executeBatch() 用于执行组成批量的所有语句。executeBatch() 返回一个整数数组,数组的每个元素表示相应更新语句的更新计数。就像将批处理语句添加到处理中一样,可以使用clearBatch() 方法删除它们。此方法将删除所有使用 addBatch() 方法添加的语句。 但是,无法指定选择某个要删除的语句。

3. 使用Statement对象进行批处理

1. 使用createStatement()方法创建Statement对象。
2. 使用setAutoCommit()将自动提交设置为false。
3. 使用addBatch()方法在创建的Statement对象上添加SQL语句到批处理中。
4. 在创建的Statement对象上使用executeBatch()方法执行所有SQL语句。
5. 最后,使用commit()方法提交所有更改。
public class BatchingWithStatement {
    // 定义驱动/URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost:3306/school";

    //  定义用户信息
    static final String USER = "root";
    static final String PASS = "root";

    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        try{
            // 1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");

            // 2.连接数据库
            conn = DriverManager.getConnection(DB_URL,USER,PASS);

            // 3.创建数据库连接对象
            stmt = conn.createStatement();

            // 4.设置auto-commit 为 false
            conn.setAutoCommit(false);

            // 5. 输出所有记录
            System.out.println(stmt);

            // 创建sql语句
            String SQL = "INSERT INTO employees (id, first, last, age) VALUES (199,'Cindy', 'Spence', 20)";
            // batch
            stmt.addBatch(SQL);

            // 再创造一条sql语句
            SQL = "INSERT INTO employees (id, first, last, age) VALUES (202,'Sonia', 'Baby', 35)";
            // batch
            stmt.addBatch(SQL);

            // 再创造一条sql语句
            SQL = "UPDATE employees SET age = 25 WHERE id = 201";
            // batch
            stmt.addBatch(SQL);

            // 创建数组去接收值
            int[] count = stmt.executeBatch();

            // 提交
            conn.commit();

            // 再次输出所有记录
            System.out.println(stmt);

            // 释放资源
            stmt.close();
            conn.close();
        }catch(SQLException se){
            se.printStackTrace();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try{
                if(stmt!=null)
                    stmt.close();
            }catch(SQLException se2){
            }
            try{
                if(conn!=null)
                    conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
        }
        System.out.println("程序结束 !");
    }
public static void printRows(Statement stmt) throws SQLException{
        // 输出所有记录
        String sql = "SELECT id, First, Last, age FROM employees";
        ResultSet rs = stmt.executeQuery(sql);

        while(rs.next()){
            //通过列名检索
            int id  = rs.getInt("id");
            int age = rs.getInt("age");
            String First = rs.getString("First");
            String Last = rs.getString("Last");

            // 输出值
            System.out.print("ID: " + id);
            System.out.print(", Age: " + age);
            System.out.print(", First: " + First);
            System.out.println(", Last: " + Last);
        }
        System.out.println();
        rs.close();
    }
}

输出:
数据库表
控制台输出

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值