当前位置: 首页 > 编程笔记 >

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

郦昆
2023-03-14
本文向大家介绍使用JDBC在MySQL数据库中如何快速批量插入数据,包括了使用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秒 ,处理效率大大提高.

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

 类似资料:
  • 本文向大家介绍C#/.Net 中快速批量给SQLite数据库插入测试数据,包括了C#/.Net 中快速批量给SQLite数据库插入测试数据的使用技巧和注意事项,需要的朋友参考一下 使用transaction:

  • 本文向大家介绍JDBC连接MySQL数据库批量插入数据过程详解,包括了JDBC连接MySQL数据库批量插入数据过程详解的使用技巧和注意事项,需要的朋友参考一下 这篇文章主要介绍了JDBC连接MySQL数据库批量插入数据过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1.读取本地json数据 2.jdbc理解数据库 3.批量插入 maven

  • 问题内容: 我有一些每月的天气数据,我想插入到Oracle数据库表中,但是我想成批插入相应的记录,以提高效率。谁能建议我如何用Python做到这一点? 例如,假设我的表有四个字段:工作站ID,日期和两个值字段。记录由工作站ID和日期字段(复合键)唯一标识。我将为每个站点插入的值将保存在一个列表中,其中包含X整整年的数据值,因此,例如,如果有两年的值,则值列表将包含24个值。 如果我想一次插入一条记

  • 我正在开发一个应用程序,它使用SpringDAO hibernate结构来处理数据库。 我想使用hibernate在oracle数据库中插入大量行(大约20000行),但是使用。save()非常慢。 我了解到使用无状态会话可以做到这一点,但因为所有会话都是通过BaseDaoImp类管理的,所以我不知道如何在这种设计模式中创建无状态会话。 如果有人知道如何实现这一点,请提供帮助。

  • 我创建了一个向MySql数据库插入数百万个值的程序。我读到过有关批插入的文章,它将优化我的程序并使其更快,但当我尝试这样做时,它以同样的方式工作。我没有将每个值插入数据库,而是每次将500个值保存在一个列表中,然后将它们插入一个大循环中,如下所示: 然后我删除列表中的所有值,并再次开始收集500个值。它不应该工作得更好吗? 我的插入代码是: 我有一些问题: 1。为什么当我批量插入时它不能更快地工作

  • 本文向大家介绍MySQL批量插入数据脚本,包括了MySQL批量插入数据脚本的使用技巧和注意事项,需要的朋友参考一下 MySQL批量插入数据脚本 新建表格就可以了 插入十万的数据为./jiaoben.sh 100000