• Java msyql批量插入 十万条数据


     //检查来源表的中间表是否有更新
            Connection conn = null;
            Statement stmt = null;
            PreparedStatement ps = null;
            // 注册 JDBC 驱动
            long begintime = System.currentTimeMillis();
            try {
                Class.forName("com.mysql.jdbc.Driver");
                // 打开链接
                System.out.println("----连接数据库...");
                 
                String url = "jdbc:mysql://127.0.0.1:3306/demo?characterEncoding=utf8&serverTimezone=UTC&useSSL=false&rewriteBatchedStatements=true";
    
                conn = DriverManager.getConnection(
                        url,
                        "root", "123456");
                // 执行查询
                stmt = conn.createStatement();
                conn.setAutoCommit(false);//不允许自动提交数据
    
                String temp = "";
                int tempNum = 3; //插入字段的数量
                for (int tempi = 0; tempi < tempNum; tempi++) {
                    temp += "?,";
                }
                temp = temp.substring(0, temp.lastIndexOf(","));
                String insertSql = "INSERT INTO a_test_syn(name,age,temp)  VALUES(" + temp + ")";
                ps = conn.prepareStatement(insertSql);
    
                int tNum = 0;
                for (int a = 1; a <= 100000; a++) {
                    long begintimea = System.currentTimeMillis();
                    ps.setObject(1,"name_"+tNum);
                    ps.setObject(2,"name_"+tNum);
                    ps.setObject(3,"name_"+tNum);
                    //1、“攒”sql
                    ps.addBatch();
                    tNum++;
                    if(a%500 == 0){
                        //2、执行batch
                        ps.executeBatch();
                        //3、清空batch
                        ps.clearBatch();
                    }
                    long begintimeb = System.currentTimeMillis();
                    System.out.println("-第" + a + "批插入--耗时-" + (begintimeb - begintimea) + "ms");
    
                }
                ps.executeBatch();
                ps.clearBatch();
                //2、执行batch
    //            ps.executeBatch();
    //            //3、清空batch
    //            ps.clearBatch();
                //提交数据
                conn.commit();
    
                if(ps != null){
                    ps.close();
                }
                // 完成后关闭
                stmt.close();
                conn.close();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            long endtime = System.currentTimeMillis();
            System.out.println("耗时-:" + (endtime - begintime)+"ms");

     

    耗时-:19331ms

    注意:使用批量提交数据,url一定要设置允许重写批量提交【rewriteBatchedStatements=true】

  • 相关阅读:
    如何在Spring Boot中记录用户系统操作流程?
    【工作规划】未来自我学习计划及发展注意事项
    vue3.0+wangEditor使用
    FIFO深度计算学习记录(汇总)
    C++下载器程序:如何使用cpprestsdk库下载www.ebay.com图片
    C++语法之function-try-block
    libcoap3对接华为云平台
    Dubbo基本用法-Dubbo Provider配置
    前端环境变量释义import.meta.env.xxx
    AgileConfig 1.8.0 已适配 .NET8
  • 原文地址:https://blog.csdn.net/qq_28312193/article/details/127706910