0

0

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

php中文网

php中文网

发布时间:2016-06-01 13:18:25

|

1160人浏览过

|

来源于php中文网

原创

bitsCN.com

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

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

SmartB2B行业电子商务
SmartB2B行业电子商务

SmartB2B 是一款基于PHP、MySQL、Smarty的B2B行业电子商务网站管理系统,系统提供了供求模型、企业模型、产品模型、人才招聘模型、资讯模型等模块,适用于想在行业里取得领先地位的企业快速假设B2B网站,可以运行于Linux与Windows等多重服务器环境,安装方便,使用灵活。 系统使用当前流行的PHP语言开发,以MySQL为数据库,采用B/S架构,MVC模式开发。融入了模型化、模板

下载
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 : 103498Convert finished.All spend time/s : 47
一共10W+,执行时间一共花费 47 秒.
这个效率仍然不高,似乎没有达到想要的效果,需要进一步改进。在MySQL JDBC连接字符串中还可以加入参数, rewriteBatchedStatements=true,mysql默认关闭了batch处理,通过此参数进行打开,这个参数可以重写向数据库提交的SQL语句,具体参见:http://www.cnblogs.com/chenjianjx/archive/2012/08/14/2637914.html useServerPrepStmts=false,如果不开启(useServerPrepStmts=false),使用com.mysql.jdbc.PreparedStatement进行本地SQL拼装,最后送到db上就是已经替换了?后的最终SQL.

在此稍加改进,连接字符串中加入下面语句(代码构造方法中去掉注释): connectStr += "?useServerPrepStmts=false&rewriteBatchedStatements=true";
再次执行如下:
All down : 103498Convert finished.All spend time/s : 10
同样的数据量,这次执行只花费了10秒 ,处理效率大大提高. bitsCN.com

相关专题

更多
ip地址修改教程大全
ip地址修改教程大全

本专题整合了ip地址修改教程大全,阅读下面的文章自行寻找合适的解决教程。

86

2025.12.26

压缩文件加密教程汇总
压缩文件加密教程汇总

本专题整合了压缩文件加密教程,阅读专题下面的文章了解更多详细教程。

50

2025.12.26

wifi无ip分配
wifi无ip分配

本专题整合了wifi无ip分配相关教程,阅读专题下面的文章了解更多详细教程。

100

2025.12.26

漫蛙漫画入口网址
漫蛙漫画入口网址

本专题整合了漫蛙入口网址大全,阅读下面的文章领取更多入口。

293

2025.12.26

b站看视频入口合集
b站看视频入口合集

本专题整合了b站哔哩哔哩相关入口合集,阅读下面的文章查看更多入口。

589

2025.12.26

俄罗斯搜索引擎yandex入口汇总
俄罗斯搜索引擎yandex入口汇总

本专题整合了俄罗斯搜索引擎yandex相关入口合集,阅读下面的文章查看更多入口。

725

2025.12.26

虚拟号码教程汇总
虚拟号码教程汇总

本专题整合了虚拟号码接收验证码相关教程,阅读下面的文章了解更多详细操作。

63

2025.12.25

错误代码dns_probe_possible
错误代码dns_probe_possible

本专题整合了电脑无法打开网页显示错误代码dns_probe_possible解决方法,阅读专题下面的文章了解更多处理方案。

30

2025.12.25

网页undefined啥意思
网页undefined啥意思

本专题整合了undefined相关内容,阅读下面的文章了解更多详细内容。后续继续更新。

94

2025.12.25

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PostgreSQL 教程
PostgreSQL 教程

共48课时 | 6.2万人学习

Django 教程
Django 教程

共28课时 | 2.5万人学习

Excel 教程
Excel 教程

共162课时 | 9.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号