JDBC的批处理功能

批处理,可以大幅度提升大量增、删、改的速度。

PreparedStatement.addBatch();//批量添加

PreparedStatement.executeBatch();//批量更新

 

public class BatchTest {

 

    public static void main(String[] args) throws SQLException {

       long start = System.currentTimeMillis();

       for (int i = 0; i < 100; i++)

           create(i);

       long end = System.currentTimeMillis();

       System.out.println("create:" + (end - start));

 

       start = System.currentTimeMillis();

       createBatch();

       end = System.currentTimeMillis();

       System.out.println("createBatch:" + (end - start));

    }

    //普通的添加方法

    static void create(int i) throws SQLException {

       Connection conn = null;

       PreparedStatement ps = null;

       ResultSet rs = null;

       try {

           conn = JdbcUtils.getConnection();

           String sql = "insert into user(name,birthday, money) values (?, ?, ?) ";

           ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);

           ps.setString(1, "no batch name" + i);

           ps.setDate(2, new Date(System.currentTimeMillis()));

           ps.setFloat(3, 100f + i);

           ps.executeUpdate();

       } finally {

           JdbcUtils.free(rs, ps, conn);

       }

    }

    //批量添加

    static void createBatch() throws SQLException {

       Connection conn = null;

       PreparedStatement ps = null;

       ResultSet rs = null;

       try {

           conn = JdbcUtils.getConnection();

           String sql = "insert into user(name,birthday, money) values (?, ?, ?) ";

           ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);

           for (int i = 0; i < 100; i++) {

              ps.setString(1, "batch name" + i);

              ps.setDate(2, new Date(System.currentTimeMillis()));

              ps.setFloat(3, 100f + i);

              ps.addBatch();

           }

           int[] is = ps.executeBatch();//int[] is是执行后自动生成的主键

       } finally {

           JdbcUtils.free(rs, ps, conn);

       }

    }

}

 

  • 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值