美文网首页
Java之JDBC用事务和批处理插入大量数据

Java之JDBC用事务和批处理插入大量数据

作者: 不积小流_无以成江海 | 来源:发表于2019-03-22 10:13 被阅读0次

普通方式插入

  • 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);
        }
      }
    }
  }

批量操作

事务提交+批量操作

  • 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);
        }
      }
    }
  }

相关文章

  • Java之JDBC用事务和批处理插入大量数据

    普通方式插入 10万条数据,耗时13秒 事务提交 设置conn.setAutoCommit(false); 最后在...

  • 2019-05-23 Mybatis批量新增数据

    1.使用传统jdbc对数据进行插入 传统的jdbc插入包括2种方式,使用for循环和使用jdbc的批处理 (一)f...

  • JavaWeb之JDBC(复习)

    十二、JDBC(复习) 目录:JDBC、事务 1.JDBC 什么是JDBC:Java连接数据库。 : ①java....

  • java事务

    1、java事务介绍 2、JDBC事务 3、JTA事务 1、java事务介绍 java事务分类:JDBC事务、...

  • 数据库(四)2018-08-27

    1. JDBC操作事务 2. JDBC进行批处理 3. DBCP的简介: 概述: DataBase Connect...

  • JDBC回顾(二)

    上一篇文章简单总结了JDBC的基本用法,这篇文章将继续介绍JDBC的更多用法,包括使用批处理,事务以及数据源等。 ...

  • JDBC

    jdbc简介 JDBC(Java DataBase Connectivity)就是Java数据库连接,说白了就是用...

  • 数据库事务及其隔离级别

    数据库事务 在计算机中,通常的事务都是指数据库事务。除此之外还包括java中的事务,jdbc事务,spri...

  • 五十三、JDBC

    一、 JDBC概述 JDBC(Java Data Base Connectivity,java数据库连接)是一种用...

  • JDBC从入门到放弃(三)

    本节,我们为大家介绍下JDBC的批处理问题。 向student表中插入一万条数据 运行结果为:2556 ms 采用...

网友评论

      本文标题:Java之JDBC用事务和批处理插入大量数据

      本文链接:https://www.haomeiwen.com/subject/xutlvqtx.html