JDBC 笔记05(插入批量数据)

一、方式一

普通地、直接地、循环执行 sql语句

    /**
     * 方式一
     * 插入20000数据需要:61374 毫秒
     */
    @Test
    public void testInsert01() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            long startTime = System.currentTimeMillis();

            connection = JDBCUtils.getConnection();
            String sql = "insert into goods(name)values(?)";
            preparedStatement = connection.prepareStatement(sql);
            //循环执行sql语句
            for (int i = 1; i <= 20000; i++) {
                preparedStatement.setObject(1, "name_" + i);
                preparedStatement.execute();
            }

            long endTime = System.currentTimeMillis();
            System.out.println("花费的时间为:" + (endTime - startTime) + " 毫秒");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResoure(preparedStatement, connection);
        }
    }

二、方式二(批处理)

使用批处理

    /**
     * 方式二(批处理)
     * 1、使用:addBatch,executeBatch,clearBatch
     * 2、MySql默认关闭批处理,需要在配置文件jdbc.properties中的url后面加上:
     *   ?rewriteBatchedStatements=true
     * 3、要使用支持批处理的MySql驱动
     *
     * 插入20000数据需要:2063 毫秒
     * 插入1000000数据需要:15528 毫秒
     */
    @Test
    public void testInsert02() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            long startTime = System.currentTimeMillis();

            connection = JDBCUtils.getConnection();
            String sql = "insert into goods(name)values(?)";
            preparedStatement = connection.prepareStatement(sql);

            for (int i = 1; i <= 1000000; i++) {
                preparedStatement.setObject(1, "name_" + i);

                //1、攒sql
                preparedStatement.addBatch();
                if (i % 500 == 0) {
                    //2、执行batch
                    preparedStatement.executeBatch();
                    //3、清空batch
                    preparedStatement.clearBatch();
                }
            }

            long endTime = System.currentTimeMillis();
            System.out.println("花费的时间为:" + (endTime - startTime) + " 毫秒");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResoure(preparedStatement, connection);
        }
    }

三、方式三(控制数据的提交)

    /**
     * 方式三(控制数据的提交)
     * 插入1000000数据需要:8107 毫秒
     */
    @Test
    public void testInsert03() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            long startTime = System.currentTimeMillis();

            connection = JDBCUtils.getConnection();

            //设置不允许自动提交
            connection.setAutoCommit(false);

            String sql = "insert into goods(name)values(?)";
            preparedStatement = connection.prepareStatement(sql);

            for (int i = 1; i <= 1000000; i++) {
                preparedStatement.setObject(1, "name_" + i);

                //1、攒sql
                preparedStatement.addBatch();
                if (i % 500 == 0) {
                    //2、执行batch
                    preparedStatement.executeBatch();
                    //3、清空batch
                    preparedStatement.clearBatch();
                }
            }

            //统一现在提交
            connection.commit();

            long endTime = System.currentTimeMillis();
            System.out.println("花费的时间为:" + (endTime - startTime) + " 毫秒");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResoure(preparedStatement, connection);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值