Java之JDBC批量插入数据

Github个人博客:https://joeyos.github.io

普通插入方式

10万条数据,耗时13秒。。。

private String url = "jdbc:mysql://localhost:3306/test01";
  private String user = "root";
  private String password = "123456";
  @Test
  public void Test(){
    Connection conn = null;
    PreparedStatement pstm =null;
    ResultSet rt = null;
    try {
      Class.forName("com.mysql.jdbc.Driver");
      conn = DriverManager.getConnection(url, user, password);   
      String sql = "INSERT INTO myTable values(?,?)";
      pstm = conn.prepareStatement(sql);
      Long startTime = System.currentTimeMillis();
      for (int i = 1; i <= 100000; i++) {
          pstm.setInt(1, i);
          pstm.setInt(2, i);
          pstm.executeUpdate();
      }
      Long endTime = System.currentTimeMillis();
      System.out.println("用时:" + (endTime - startTime));
    } catch (Exception e) {
      e.printStackTrace();
      throw new RuntimeException(e);
    }finally{
      if(pstm!=null){
        try {
          pstm.close();
        } catch (SQLException e) {
          e.printStackTrace();
          throw new RuntimeException(e);
        }
      }
      if(conn!=null){
        try {
          conn.close();
        } catch (SQLException e) {
          e.printStackTrace();
          throw new RuntimeException(e);
        }
      }
    }
  }

事务提交

  • 设置conn.setAutoCommit(false);
  • 最后在所有命令执行完之后再提交事务conn.commit();
  • 10w条数据11秒。。。
private String url = "jdbc:mysql://localhost:3306/test01";
  private String user = "root";
  private String password = "123456";
  @Test
  public void Test(){
    Connection conn = null;
    PreparedStatement pstm =null;
    ResultSet rt = null;
    try {
      Class.forName("com.mysql.jdbc.Driver");
      conn = DriverManager.getConnection(url, user, password);   
      String sql = "INSERT INTO myTable values(?,?)";
      pstm = conn.prepareStatement(sql);
      Long startTime = System.currentTimeMillis();
      conn.setAutoCommit(false);
      for (int i = 1; i <= 100000; i++) {
          pstm.setInt(1, i);
          pstm.setInt(2, i);
          pstm.executeUpdate();
      }
      conn.commit();
      Long endTime = System.currentTimeMillis();
      System.out.println("用时:" + (endTime - startTime));
    } catch (Exception e) {
      e.printStackTrace();
      throw new RuntimeException(e);
    }finally{
      if(pstm!=null){
        try {
          pstm.close();
        } catch (SQLException e) {
          e.printStackTrace();
          throw new RuntimeException(e);
        }
      }
      if(conn!=null){
        try {
          conn.close();
        } catch (SQLException e) {
          e.printStackTrace();
          throw new RuntimeException(e);
        }
      }
    }
  }

批量操作

  • 注意更改url = “jdbc:mysql://localhost:3306/test01?rewriteBatchedStatements=true”;
  • pstm.addBatch();代替pstm.executeUpdate();
  • 最后批量操作pstm.executeBatch();
  • 10w条数据1.3秒!!!

事务提交+批量操作

10w条数据大概1.2秒。

private String url = "jdbc:mysql://localhost:3306/test01?rewriteBatchedStatements=true";
  private String user = "root";
  private String password = "123456";
  @Test
  public void Test(){
    Connection conn = null;
    PreparedStatement pstm =null;
    ResultSet rt = null;
    try {
      Class.forName("com.mysql.jdbc.Driver");
      conn = DriverManager.getConnection(url, user, password);   
      String sql = "INSERT INTO myTable values(?,?)";
      pstm = conn.prepareStatement(sql);
      Long startTime = System.currentTimeMillis();
      conn.setAutoCommit(false);
      for (int i = 1; i <= 100000; i++) {
          pstm.setInt(1, i);
          pstm.setInt(2, i);
          pstm.addBatch();
      }
      pstm.executeBatch();
      conn.commit();
      Long endTime = System.currentTimeMillis();
      System.out.println("用时:" + (endTime - startTime));
    } catch (Exception e) {
      e.printStackTrace();
      throw new RuntimeException(e);
    }finally{
      if(pstm!=null){
        try {
          pstm.close();
        } catch (SQLException e) {
          e.printStackTrace();
          throw new RuntimeException(e);
        }
      }
      if(conn!=null){
        try {
          conn.close();
        } catch (SQLException e) {
          e.printStackTrace();
          throw new RuntimeException(e);
        }
      }
    }
  }
  • 10
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以使用JDBC批量插入数据到ClickHouse数据库。下面是一个简单的示例代码来演示如何使用JDBC批量插入数据: ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class ClickHouseBatchInsert { public static void main(String[] args) { // JDBC连接信息 String url = "jdbc:clickhouse://localhost:8123/default"; String username = "your_username"; String password = "your_password"; // SQL插入语句 String sql = "INSERT INTO your_table (column1, column2, column3) VALUES (?, ?, ?)"; // 数据集 Object[][] data = { {"value1_1", "value1_2", "value1_3"}, {"value2_1", "value2_2", "value2_3"}, {"value3_1", "value3_2", "value3_3"} }; try (Connection conn = DriverManager.getConnection(url, username, password); PreparedStatement pstmt = conn.prepareStatement(sql)) { // 关闭自动提交 conn.setAutoCommit(false); // 批量插入数据 for (Object[] row : data) { for (int i = 0; i < row.length; i++) { pstmt.setObject(i + 1, row[i]); } pstmt.addBatch(); } // 执行批量插入 int[] result = pstmt.executeBatch(); // 提交事务 conn.commit(); System.out.println("成功插入 " + result.length + " 条数据"); } catch (SQLException e) { e.printStackTrace(); } } } ``` 在这个示例中,你需要将`url`、`username`和`password`替换为你实际的ClickHouse连接信息,将`your_table`替换为你要插入数据的目标表名。然后,根据你的数据集,调整`data`数组中的值和列数。 这个示例中使用了`PreparedStatement`来执行预编译的SQL语句,并使用`addBatch()`方法将批量插入的每一行添加到批处理中。最后,通过调用`executeBatch()`方法执行批处理操作,并使用`commit()`方法提交事务。 这样,你就可以使用JDBC批量插入数据到ClickHouse数据库了。希望对你有所帮助!如果有任何问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值