使用JDBC连接MySQL数据库进行数据插入的时候,特别是大批量数据连续插入(10W+),如何提高效率呢?
在JDBC编程接口中Statement 有两个方法特别值得注意:
1
|
void addBatch() throws SQLException |
Adds a set of parameters to this PreparedStatement object's batch of commands.
1
|
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()。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
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 += "?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 ();
}
} |
相关推荐
Mybatis与JDBC批量插入MySQL数据库性能测试,资源包含文档、代码和数据库。
针对oracle中blob字段的操作,能批量快速的插入大字段,效率非常高
主要介绍了JDBC连接MySQL数据库批量插入数据过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
该资源主要实现如下功能,利用poi获取excel中大量数据,然后利用jdbc批量插入到mysql
spring jdbc Templatetest 访问mysql数据库,批量插入数据
MySQL批量插入问题 在开发项目时,因为有一些旧系统的基础数据需要提前导入,所以我在导入时做了批量导入操作 ,但是因为MySQL中的一次可接受的SQL语句大小受限制所以我每次批量虽然只有500条,但依然无法插入,这个...
使用JDBC连接MySQL数据库进行数据插入的时候,特别是大批量数据连续插入(100000),如何提高效率呢?今天小编通过本教程给大家介绍下
主要介绍了使用JDBC在MySQL数据库中如何快速批量插入数据,可以有效的解决一次插入大数据的方法,
主要介绍了java中JDBC实现往MySQL插入百万级数据的实例代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
利用poi获取excel中大量数据,然后利用jdbc批量插入到mysql,直接运行main方法
本实例采用c3p0作为线程池工具包,讲解了jdbc基本用法,同时给出了Oracle以及mysql增(单插入、批量插入)、删、查、改等功能,可以直接复制使用。
MySQL中插入一个记录需要的时间由下列因素组成,其中的数字表示大约比例: 连接:(3) 发送查询给服务器:(2) 分析查询:(2) 插入记录:(1x记录大小) 插入索引:(1x索引) 关闭:(1)...
2.rewriteBatchedStatements=true 开启批量插入,插入只执行一次,所有插入比较快。 二、 代码 package test0823.demo1; import java.sql.*; /** * @author : Bei-Zhen * @date : 2020-08-24 0:43 */ public ...
22.11 mysqlimport程序(文本导入、批量导入) 22.12 mysqlshow程序(查看信息) 22.13 myisamchk程序(修复MyISAM文件) 22.14 myisampack程序(压缩MyISAM文件) 第23章 MySQL API应用指南 23.1 ...
java快速插入千万级数据,亲测91秒插入1700万数据!!!
Hibernate hibernate.jdbc.batch_size 测试带有 MySQL JDBC 驱动程序的 Hibernate 批量插入示例。 에 관한 설명
22.11 mysqlimport程序(文本导入、批量导入) 22.12 mysqlshow程序(查看信息) 22.13 myisamchk程序(修复MyISAM文件) 22.14 myisampack程序(压缩MyISAM文件) 第23章 MySQL API应用指南 23.1 ...
- 没有用WebMagic的Pipeline来存DB,而是直接用JDBC存的,默认爬虫线程数为1,每翻页1页批量插入Mysql。 - 由于链家限制爬取页数为100页,所以一个城市下,先查出所有行政区,再遍历行政区下的所有街道,以每个街道...