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

不过值得注意的是,首先需要在数据库链接中设置手动提交,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; i...
使用JDBC在MySQL数据库中如何快速批量插入数据
使用JDBC连接MySQL数据库进行数据插入的时候,特别是大批量数据连续插入(10W+),如何提高效率呢?在JDBC编程接口中Statement 有两个方法特别值得注意:Adds a set of parameters to this PreparedStatement object's batch of commands.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秒 ,处理效率大大提高.2023-07-27
mengvlog 阅读 8 次 更新于 2025-07-19 08:27:21 我来答关注问题0
  •  翡希信息咨询 使用JDBC连接MySQL数据库的5种方式

    步骤:首先通过Class.forName加载MySQL驱动,然后使用DriverManager.getConnection方法获取数据库连接。优点:简单直接,适合快速入门和测试。使用DriverManager直接连接:步骤:省略显式加载驱动的步骤,直接使用DriverManager.getConnection方法获取连接。注意:这种方式要求JDBC 4.0及以上版本,且驱动jar包在类路径中。...

  •  海南加宸 请问用jdbc连接数据库时url是个什么意思,请详解

    在使用JDBC(Java Database Connectivity)连接MySQL数据库时,URL(统一资源定位符)扮演着至关重要的角色。URL的格式如下:jdbc:mysql://服务器名或地址:端口号/数据库名。例如,示例中的URL为:jdbc:mysql://localhost:3306/school。其中,jdbc:mysql:// 是固定的前缀,表示使用JDBC连接MySQL数据库。

  • JDBC连接MySQL时使用的URL格式为:jdbc:mysql://localhost:3306/test。这个URL可以拆分为几个部分进行解析:jdbc:mysql://这部分是指使用JDBC连接MySQL数据库的方式。localhost:这部分代表连接的主机地址,即本机地址。如果你连接的是远程服务器,可以将localhost:替换为服务器的实际IP地址或域名。3306是MyS...

  • 在Java中使用JDBC连接数据库的过程可以分为几个步骤。首先,需要注册数据库驱动,例如对于MySQL数据库,可以使用以下代码:Class.forName("com.mysql.jdbc.Driver")。这一步是为了让JVM知道要加载哪个数据库驱动。接下来,需要获取数据库的连接。这可以通过java.sql.DriverManager.getConnection()方法实现,其...

  •  翡希信息咨询 jdbc 与odbc傻傻分不清楚,pycharm通过jdbc,spss通过odbc连接本机mysql实战

    步骤:安装MySQL服务器和JDBC连接器:确保MySQL服务器已安装并运行,同时下载并安装适用于Java的JDBC连接器。配置数据库连接:在PyCharm中,通过数据库工具窗口或配置文件,设置JDBC连接参数,包括数据库URL、用户名、密码等。测试连接:尝试连接数据库,确保配置正确无误。三、在SPSS中使用ODBC连接本地MySQL ...

檬味博客在线解答立即免费咨询

mySQL相关话题

Copyright © 2023 WWW.MENGVLOG.COM - 檬味博客
返回顶部