使用JDBC在MySQL数据库中如何快速批量插入数据

 更新时间:2016年11月05日 16:54:41   作者:PlusPlus1  
这篇文章主要介绍了使用JDBC在MySQL数据库中如何快速批量插入数据,可以有效的解决一次插入大数据的方法,

使用JDBC连接MySQL数据库进行数据插入的时候,特别是大批量数据连续插入(10W+),如何提高效率呢?

在JDBC编程接口中Statement 有两个方法特别值得注意:

void addBatch() throws SQLException

Adds a set of parameters to this PreparedStatement object's batch of commands.

int[] executeBatch() throws SQLException

Submits a batch of commands to the database for execution and if all commands execute successfully, returns an array of update counts. The int elements of the array that is returned are ordered to correspond to the commands in the batch, which are ordered according to the order in which they were added to the batch.

通过使用addBatch()和executeBatch()这一对方法可以实现批量处理数据。

不过值得注意的是,首先需要在数据库链接中设置手动提交,connection.setAutoCommit(false),然后在执行Statement之后执行connection.commit()。

package cyl.demo.ipsearcher; 
 
import java.io.BufferedReader; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.SQLException; 
 
public class DbStoreHelper { 
 
  private String insert_sql; 
  private String charset; 
  private boolean debug; 
 
  private String connectStr; 
  private String username; 
  private String password; 
 
  public DbStoreHelper() { 
    connectStr = "jdbc:mysql://localhost:3306/db_ip"; 
    // connectStr += "?useServerPrepStmts=false&rewriteBatchedStatements=true"; 
    insert_sql = "INSERT INTO tb_ipinfos (iplong1,iplong2,ipstr1,ipstr2,ipdesc) VALUES (?,?,?,?,?)"; 
    charset = "gbk"; 
    debug = true; 
    username = "root"; 
    password = "***"; 
  } 
 
  public void storeToDb(String srcFile) throws IOException { 
    BufferedReader bfr = new BufferedReader(new InputStreamReader(new FileInputStream(srcFile), charset)); 
    try { 
      doStore(bfr); 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } finally { 
      bfr.close(); 
    } 
  } 
 
  private void doStore(BufferedReader bfr) throws ClassNotFoundException, SQLException, IOException { 
    Class.forName("com.mysql.jdbc.Driver"); 
    Connection conn = DriverManager.getConnection(connectStr, username,password); 
    conn.setAutoCommit(false); // 设置手动提交 
    int count = 0; 
    PreparedStatement psts = conn.prepareStatement(insert_sql); 
    String line = null; 
    while (null != (line = bfr.readLine())) { 
      String[] infos = line.split(";"); 
      if (infos.length < 5)  continue; 
      if (debug) { 
        System.out.println(line); 
      } 
      psts.setLong(1, Long.valueOf(infos[0])); 
      psts.setLong(2, Long.valueOf(infos[1])); 
      psts.setString(3, infos[2]); 
      psts.setString(4, infos[3]); 
      psts.setString(5, infos[4]); 
      psts.addBatch();     // 加入批量处理 
      count++;       
    } 
    psts.executeBatch(); // 执行批量处理 
    conn.commit(); // 提交 
    System.out.println("All down : " + count); 
    conn.close(); 
  } 
 
} 

执行完成以后:

All down : 103498 
Convert finished. 
All spend time/s : 47 

一共10W+,执行时间一共花费 47 秒.

这个效率仍然不高,似乎没有达到想要的效果,需要进一步改进。

在MySQL JDBC连接字符串中还可以加入参数,

rewriteBatchedStatements=true,mysql默认关闭了batch处理,通过此参数进行打开,这个参数可以重写向数据库提交的SQL语句。

useServerPrepStmts=false,如果不开启(useServerPrepStmts=false),使用com.mysql.jdbc.PreparedStatement进行本地SQL拼装,最后送到db上就是已经替换了?后的最终SQL.

在此稍加改进,连接字符串中加入下面语句(代码构造方法中去掉注释):
connectStr += "?useServerPrepStmts=false&rewriteBatchedStatements=true";

再次执行如下:

All down : 103498 
Convert finished. 
All spend time/s : 10 

同样的数据量,这次执行只花费了10秒 ,处理效率大大提高.

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • MySQL如何设置某个字段的值自增

    MySQL如何设置某个字段的值自增

    这篇文章主要介绍了MySQL如何设置某个字段的值自增,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-08-08
  • MySQL中EXPLAIN的/基本使用及字段详解

    MySQL中EXPLAIN的/基本使用及字段详解

    EXPLAIN命令是MySQL中用于解析SQL查询语句的工具,帮助开发者理解MySQL如何执行SQL语句,通过EXPLAIN可以查看SQL执行的详细信息,如表的读取顺序、使用的索引、数据读取操作的类型等,这篇文章主要介绍了MySQL中EXPLAIN的/基本使用及字段详解,需要的朋友可以参考下
    2024-09-09
  • windows下安装MySQL详细教程

    windows下安装MySQL详细教程

    这篇文章主要介绍了windows下安装MySQL教程的相关资料,需要的朋友可以参考下
    2022-10-10
  • 如何用命令行进入mysql具体操作步骤

    如何用命令行进入mysql具体操作步骤

    逛论坛时无意发现有个伙计提出这样的问题,如何用命令行进入mysql,搜集整理了一些特意贴出来与大家分享,感兴趣的你可以参考下希望对你有所帮助
    2013-03-03
  • MySQL分组排序取每组第一条数据的实现

    MySQL分组排序取每组第一条数据的实现

    最近有个需求MySQL根据某一个字段分组,然后组内排序,最后每组取排序后的第一条数据,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-08-08
  • MySQL使用innobackupex备份连接服务器失败的解决方法

    MySQL使用innobackupex备份连接服务器失败的解决方法

    这篇文章主要为大家详细介绍了MySQL使用innobackupex备份连接服务器失败的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-02-02
  • 详解 MySQL的FreeList机制

    详解 MySQL的FreeList机制

    这篇文章主要介绍了MySQL的FreeList机制的相关资料,帮助大家更好的理解和使用MySQL 数据库,感兴趣的朋友可以了解下
    2020-11-11
  • mysql5.7.33误删除ibdata文件找回数据的方法

    mysql5.7.33误删除ibdata文件找回数据的方法

    这篇文章主要介绍了mysql5.7.33误删除ibdata文件找回数据的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • Windows10系统下MySQL(8.0.37)安装与配置教程

    Windows10系统下MySQL(8.0.37)安装与配置教程

    相信很多人都遇到过安装Mysql的时候出现各种各样的问题,下面这篇文章主要给大家介绍了关于Windows10系统下MySQL(8.0.37)安装与配置的相关资料,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2024-07-07
  • SQL中EXPLAIN命令的使用方法

    SQL中EXPLAIN命令的使用方法

    这篇文章主要给大家介绍了关于SQL中EXPLAIN命令的使用方法,文中介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03

最新评论