栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

sql批处理

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

sql批处理

配置文件中url后加?rewriteBatchedStatements=true

@Test
    public void test01() {
        Connection connection = JDBCUtils.getConnection();
        String sql = "insert into actor values(null ,? ,?)";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            System.out.println("开始!");
            long start = System.currentTimeMillis();
            for (int i = 0; i < 500; i++) {
                //preparedStatement.setInt(1, i+1);
                preparedStatement.setString(1, "李" + 1);
                preparedStatement.setInt(2, 777);
                preparedStatement.executeUpdate();
            }
            long end = System.currentTimeMillis();
            System.out.println("传统方式耗时:" + (end - start));//传统方式耗时:23866
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(null, preparedStatement, connection);
        }
    }

    //批处理新增500条
    @Test
    public void test02() {
        Connection connection = JDBCUtils.getConnection();
        String sql = "insert into actor values(null ,? ,?)";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            System.out.println("开始!");
            long start = System.currentTimeMillis();
            for (int i = 0; i < 500; i++) {
                preparedStatement.setString(1, "李" + 1);
                preparedStatement.setInt(2, 777);
                preparedStatement.addBatch();
                if ((i + 1) % 100 == 0) {
                    preparedStatement.executeBatch();
                    preparedStatement.clearBatch();
                }
            }
            long end = System.currentTimeMillis();
            System.out.println("传统方式耗时:" + (end - start));//传统方式耗时:361
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(null, preparedStatement, connection);
        }
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/678155.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号