JDBC 开启mysql批处理

通过MySQL批处理来提高效率

    @Test
    public void test01() throws SQLException {
        //使用批处理
        /*
        告诉mysqL服务器开启批处理的支持
        ?rewriteBatchedstatements=true
        addBatch();将任务赞到一起
        excuteBatch();执行批处理

         */
        String url="jdbc:mysql://localhost:3306/kanfang";
        String user = "root";
        String password="123456";
        DriverManager.registerDriver(new Driver());
        Connection connection = DriverManager.getConnection(url, user, password);
        String sql="insert into batch values(null,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 2000; i++) {
            preparedStatement.setObject(1,"李白"+i);
            preparedStatement.setObject(2,"password"+i);
            //将数据攒到一起sele
            preparedStatement.addBatch();
        }
        //将攒到一起的数据提交
        preparedStatement.executeBatch();
        long endTime = System.currentTimeMillis();
        System.out.println("耗时:"+(endTime - startTime));
        preparedStatement.close();
        connection.close();
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
回答: 引用中提到了使用jdbcTemplate的batchUpdate方法进行批量数据插入,但是插入50000条数据时的耗时较长。为了优化效率,可以考虑使用批处理功能。引用中提到,批处理可以帮助提高性能。通过将多个插入语句打包在一起,减少了与数据库的通信次数,从而提高了效率。在批处理中,可以一次性执行多个插入语句,而不是逐个执行。这样可以减少了与数据库的交互次数,提高了效率。具体实现是将多个插入语句的参数打包成一个列表,然后一次性执行这个列表。可以使用JdbcTemplate的batchUpdate方法来实现批处理。 在引用中的代码中,通过循环将每条数据的参数加入到批处理列表中,然后使用batchUpdate方法一次性执行这个列表。这样可以大大减少与数据库的交互次数,提高效率。需要注意的是,在批处理过程中,数据库的事务处理也是非常重要的。为了保证数据的一致性和完整性,可以在批处理执行之前开启一个事务,然后在批处理执行完毕后提交事务。 另外,引用提到了在数据库连接的url中添加rewriteBatchedStatements=true的设置。这个设置对于MySQL驱动可以有效提高批处理的效率。但是,根据引用的描述,该设置对于SQL Server驱动无效。所以,在SQL Server中使用rewriteBatchedStatements=true可能无法提高批处理的效率。 综上所述,通过使用批处理功能,并确保正确的事务处理,可以有效提高jdbc批处理的效率。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [关于使用jdbcTemplate的PreparedStatement进行批量插入速度缓慢问题记录](https://blog.csdn.net/weixin_40580131/article/details/124610468)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [Statement 和PreparedStatement 批量处理JDBC语句提高处理速度](https://blog.csdn.net/axu20/article/details/4005190)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

超会写BUG的小凌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值