JDBC executeBatch批量插入数据

本文介绍了一种使用JDBC executeBatch方法进行批量插入数据的优化技巧,通过关闭事务自动提交,设置合适的批处理大小,显著提升了10万条数据从5小时到3分钟的处理速度。主要涉及数据库操作、事务管理和性能优化。
摘要由CSDN通过智能技术生成

JDBC executeBatch批量插入数据

JDBC executeBatch批量插入数据

con.setAutoCommit(false);
项目中遇到上传报盘批量处理数据,通过从jpa保存–>改成如下批量处理,10万条数据时间从5个小时–>3分钟;

    /**
     * 插入数据
     *
     * @param apply
     */
    @Override
//    @Transactional(readOnly = false, rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void insertDetailForBatch(EmpSalaryApply apply) {
        Long startTime = 0L;
        Long endTime = 0L;
        Connection con = null;
        PreparedStatement pst = null;
        try {
            String url = gfurl; // orcl为数据库的SID
            String user = gfusername;
            String password = gfpassword;
            StringBuffer sql = new StringBuffer("insert into apply_com_salary_emps (ID, name)");
            sql.append("VALUES(?,?)");
            Class.forName(gfdriverClassName);
            con = (Connection) DriverManager.getConnection(url, user, password);
            // 关闭事务自动提交
            con.setAutoCommit(false);
            final int batchSize = 5000;
            int count = 0;
            int batchNumber=0;
            startTime = System.currentTimeMillis();
            pst = (PreparedStatement) con.prepareStatement(sql.toString());
            List <EmpSalaryDetail> list = apply.getEmpSalaryDetails();
            /**
             * 第一条执行时已经保存,直接从第二条执行
             * 即i=1
             */
            for (int i = 1; i < list.size(); i++) {
                EmpSalaryDetail empSalaryDetail = list.get(i);
                pst.setString(1, apply.getId());
                pst.setString(2, empSalaryDetail.getName());
                // 把一个SQL命令加入命令列表
                pst.addBatch();
                if (++count % batchSize == 0) {
                    ++batchNumber;
                    System.out.println("apply.getId()" +  apply.getId()+"遍历"+batchNumber);
                    pst.executeBatch();
                    count = 0;
                    pst.clearBatch();
                }
            }
            // 执行批量更新
            pst.executeBatch();
            // 语句执行完毕,提交本事务
            con.commit();
            endTime = System.currentTimeMillis();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            throw new ResourceNotFoundException("整体数据保存失败!");
        } catch (SQLException e) {
            e.printStackTrace();
            throw new ResourceNotFoundException("整体数据保存失败!");
        } finally {
            System.out.println("用时:" + (endTime - startTime));
            try {
                pst.close();
                con.close();
                System.out.println("连接关闭" );
            } catch (SQLException e) {
                e.printStackTrace();
                throw new ResourceNotFoundException("整体数据保存失败!");
            }
        }

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

lwd2307997664

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

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

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

打赏作者

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

抵扣说明:

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

余额充值