前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JDBC 批量处理(13)

JDBC 批量处理(13)

作者头像
桑鱼
发布2020-03-17 17:16:13
7250
发布2020-03-17 17:16:13
举报

批量处理JDBC语句提高处理速度

1)当需要成批插入或者更新记录时,可以采用Java的批量更新机制,这一机制允许多条语句一次性提交给数据批量处理。通常情况下比单独提交处理更有效率

2)JDBC的批量处理语句包括下面两个方法: addBatch(String)添加需要批量处理的SQL语句或参数 executeBatch()执行批量处理语句 clearBatch() 清空SQL

3)通常我们会遇到两种批量执行SQL语句的情况:1⃣️多条SQL语句的批量处理、2⃣️一个SQL语句的批量传参

多条SQL语句的批量处理

代码语言:javascript
复制
public class JDBCTest05 {

    // 使用Statement的addBatch()批处理
    public void testBatchWithStatement() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/mydb";
        String sql = null;
        Connection connection = DriverManager.getConnection(url,"user","password");
        Statement statement = connection.createStatement();
        long begin = System.currentTimeMillis();
        for(int i = 0;i < 100000;i++){
            sql = "INSERT INTO customers VALUES(" + (i + 1)
                    + ", ' name_" + i + "', '2010-Ô1-13')";
            statement.addBatch(sql); // statement的批量处理
        }
        long end = System.currentTimeMillis();
        System.out.println("Time: " + (end - begin));
        statement.executeUpdate(sql);
        statement.close();
        connection.close();
    }

    // PreparedStatement()的executeUpdate分条处理
    public void testBatchWithPreparedStatement() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/mydb";
        String sql = "INSERT INTO customers VALUES(?,?,?)";
        Connection connection = DriverManager.getConnection(url,"user","password");
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        Date date = new Date(new java.util.Date().getTime());

        long begin = System.currentTimeMillis();
        for(int i = 0;i < 100000;i++){
            preparedStatement.setInt(1,i+1);
            preparedStatement.setString(2,"name_" + i);
            preparedStatement.setDate(3,date);
            preparedStatement.executeUpdate();
        }
        long end = System.currentTimeMillis();
        System.out.println("Time: " + (end - begin));
        preparedStatement.close();
        connection.close();
    }
    

    // PreparedStatement()的executeBatch批量处理
    public void testBatch() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/mydb";
        String sql = "INSERT INTO customers VALUES(?,?,?)";
        Connection connection = DriverManager.getConnection(url,"user","password");
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        Date date = new Date(new java.util.Date().getTime());

        long begin = System.currentTimeMillis();
        for(int i = 0;i < 100000;i++){
            preparedStatement.setInt(1,i+1);
            preparedStatement.setString(2,"name_ " + i);
            preparedStatement.setDate(3,date);
            preparedStatement.addBatch();
            
            if((i + 1) % 300 == 0){
                preparedStatement.executeBatch();
                preparedStatement.clearBatch();
            }
        }
        
        if(100000 % 300 != 0){
            preparedStatement.executeBatch();
            preparedStatement.clearBatch();
        }

        long end = System.currentTimeMillis();
        System.out.println("Time: " + (end - begin));
        preparedStatement.close();
        connection.close();
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
批量计算
批量计算(BatchCompute,Batch)是为有大数据计算业务的企业、科研单位等提供高性价比且易用的计算服务。批量计算 Batch 可以根据用户提供的批处理规模,智能地管理作业和调动其所需的最佳资源。有了 Batch 的帮助,您可以将精力集中在如何分析和处理数据结果上。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档