【JDBC】使用Spring提供的JDBCTemplate通过Statement向MySql数据库插入千万条数据,耗时4m55s,使用insert语句批量插入方式二
这回依然是使用 insert批量插入这种方式
insert into emp(name,age,cdate)
values
('A' , 20, '2019-10-13 00:00:00'),
('B' , 21, '2019-10-13 01:00:00'),
('C' , 22, '2019-10-13 05:00:00')
只是执行SQL的方式由stmt.executeBatch换成了stmt.execute,结果发现速度上几乎一样。
代码如下:
package com.hy.action.jdbc;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
public class BatchJDBCStmtInsert2 {
private static Logger logger = Logger.getLogger(BatchJDBCStmtInsert2.class);
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
//把beans.xml的类加载到容器
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
JdbcTemplate jt=(JdbcTemplate)applicationContext.getBean("jdbcTemplate");
// Initialize conn&stmt
Connection conn=null;
Statement stmt=null;
try {
conn =jt.getDataSource().getConnection();
conn.setAutoCommit(false);
stmt = conn.createStatement();
String ctime="2017-11-01 00:00:01";
int index=0;
for(int i=0;i<10000;i++) {
String insertSql="insert into emp(name,age,cdate) values ";
List<String> list=new ArrayList<String>();
for(int j=0;j<1000;j++) {
index++;
Object arr[]={"'E:"+index+"'",index % 100,"'"+ctime+"'"};
String valueSql=MessageFormat.format("({0},{1},{2})", arr);
list.add(valueSql);
ctime=timePastOneSecond(ctime);
}
String sql=insertSql+String.join(",", list);
stmt.execute(sql);
conn.commit();
logger.info("#"+i+" 1000 records have been inserted to table:'emp'.");
}
} catch (SQLException e) {
logger.error("Error happened:"+e);
try {
conn.rollback();
} catch (SQLException e1) {
logger.error("Can not rollback because of the error:'"+e+"'.");
}
}finally {
try {
stmt.close();
conn.close();
long endTime = System.currentTimeMillis();
logger.info("Time elapsed:" + toDhmsStyle((endTime - startTime)/1000) + ".");
} catch (SQLException e1) {
logger.error("Can not close connection because of the error:'"+e1+"'.");
}
}
}
public static String timePastOneSecond(String otime) {
try {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dt=sdf.parse(otime);
Calendar newTime = Calendar.getInstance();
newTime.setTime(dt);
newTime.add(Calendar.SECOND,1);
Date dt1=newTime.getTime();
String retval = sdf.format(dt1);
return retval;
}
catch(Exception ex) {
ex.printStackTrace();
return null;
}
}
// format seconds to day hour minute seconds style
// Example 5000s will be formatted to 1h23m20s
public static String toDhmsStyle(long allSeconds) {
String DateTimes = null;
long days = allSeconds / (60 * 60 * 24);
long hours = (allSeconds % (60 * 60 * 24)) / (60 * 60);
long minutes = (allSeconds % (60 * 60)) / 60;
long seconds = allSeconds % 60;
if (days > 0) {
DateTimes = days + "d" + hours + "h" + minutes + "m" + seconds + "s";
} else if (hours > 0) {
DateTimes = hours + "h" + minutes + "m" + seconds + "s";
} else if (minutes > 0) {
DateTimes = minutes + "m" + seconds + "s";
} else {
DateTimes = seconds + "s";
}
return DateTimes;
}
}
控制台输出:
INFO [main] - #9990 1000 records have been inserted to table:'emp'. INFO [main] - #9991 1000 records have been inserted to table:'emp'. INFO [main] - #9992 1000 records have been inserted to table:'emp'. INFO [main] - #9993 1000 records have been inserted to table:'emp'. INFO [main] - #9994 1000 records have been inserted to table:'emp'. INFO [main] - #9995 1000 records have been inserted to table:'emp'. INFO [main] - #9996 1000 records have been inserted to table:'emp'. INFO [main] - #9997 1000 records have been inserted to table:'emp'. INFO [main] - #9998 1000 records have been inserted to table:'emp'. INFO [main] - #9999 1000 records have been inserted to table:'emp'. INFO [main] - Time elapsed:4m55s.
数据库的情况:


下面,就可以写总结了。
--END-- 2019年10月13日15:23:03
【JDBC】使用Spring提供的JDBCTemplate通过Statement向MySql数据库插入千万条数据,耗时4m55s,使用insert语句批量插入方式二的更多相关文章
- 【JDBC】使用Spring提供的JDBCTemplate通过PrepareStatement向MySql数据库插入千万条数据,耗时32m47s,速度提升有限
数据库环境还和原来一样,只是从Statement换成了PrepareStatement,都说PrepareStatement因为预编译比Statement快,但是实际运行真快不了多少. 代码如下: p ...
- 使用JDBC向数据库中插入一条数据
原谅我是初学者,这个方法写的很烂,以后不会改进,谢谢 /** * 通过JDBC向数据库中插入一条数据 1.Statement 用于执行SQL语句的对象 1.1 通过Connection 的 * cre ...
- Spring Boot入门(2)使用MySQL数据库
介绍 本文将介绍如何在Spring项目中连接.处理MySQL数据库. 该项目使用Spring Data JPA和Hibernate来连接.处理MySQL数据库,当然,这仅仅是其中一种方式,你也 ...
- Java使用JDBC连接数据库逐条插入数据、批量插入数据、以及通过SQL语句批量导入数据的效率对比
测试用的示例java代码: package com.zifeiy.test.normal; import java.io.File; import java.io.FileOutputStream; ...
- 使用JDBC(Dbutils工具包)来从数据库拿取map类型数据来动态生成insert语句
前言: 大家在使用JDBC来连接数据库时,我们通过Dbutils工具来拿取数据库中的数据,可以使用new BeanListHandler<>(所映射的实体类.class),这样得到的数据, ...
- JDBC快速入门(附Java通过jar包连接MySQL数据库)
•通过jar包连接mysql数据库 •下载jar包 Java 连接 MySQL 需要驱动包,官网下载地址为MySQL驱动包官网下载,选择适合的jar包版本进行安装 (记得安装的地址,下面导入包时会用到 ...
- spring mvc 插入一条数据 返回该数据的主键编号
import org.springframework.jdbc.core.PreparedStatementCreator; import org.springframework.jdbc.suppo ...
- 使用JDBC向数据库中插入一条数据(第一次修改版)
增加了一个Tools类,放了一些常用的工具 package com.JDBC.java; import java.io.IOException; import java.io.InputStream; ...
- spring boot使用log4j2将日志写入mysql数据库
log4j2官方例子在spring boot中报错而且还是用的是org.apache.commons.dbcp包 我给改了一下使用org.apache.commons.dbcp2包 1.log4j2. ...
随机推荐
- js 扁平化输出数组
1.使用数组的flat方法 [1,2,[3,[4,5]]].flat(Infinity) //[1, 2, 3, 4, 5] 2.实现方式二: var arr = [[1, 2, 23], [13, ...
- TCP的keepalive和应用层的heart
从长链接说起 TCP是长链接的,也就是说连接建立后,及时数年没有通信连接仍然存在.这样做的好处是:免去了DNS解析的时间,连接建立等时间,大大加快了请求的速度,同时也有利于接受服务器的实时消息.但前提 ...
- Django:常用字段、手动自动第三张表单、元信息
一.常用字段和非常用字段 二.手动,自动创建第三张表 三.元信息 四.defer和only 一.常用字段和非常用字段 -常用字段 AutoField int自增列,必须填入参数 primary_key ...
- JAVA-如何打包成jar包
主线:编译 - 打包 - 运行 准备工作: 1. 手动打可直接执行的jar包 1) 先使用javac编译java文件,得到class文件 2) 新建文件,名字任起,比如可以叫manifest,内容如下 ...
- 4.caffe资源汇总(更新中)
学习需要更新,网上有一些非常不错博客. 感谢这些博主,他们都很认真. 00.tornadomeet 0.denny的学习专栏 1.xizero00 2.lingerlanlan 3.iamzhangz ...
- bat 提示窗口,带换行
bat 提示窗口 各种窗口样式 mshta vbscript:msgbox("内容1",1,"标题1")(window.close) mshta vbscrip ...
- 【转】GO语言map类型interface{}转换踩坑小记
原文:https://www.az1314.cn/art/69 ------------------------------------------ mapA := make([string]inte ...
- HDU 6049 - Sdjpx Is Happy | 2017 Multi-University Training Contest 2
思路来源于 FXXL - - 一个比较奇怪的地方就是第三步可以不做,也就是ans至少为1,听说场内有提问的,然后 admin 说可以不做- - (wa的我心烦) /* HDU 6049 - Sdjpx ...
- luogu 4234 最小差值生成树 LCT
感觉码力严重下降~ #include <bits/stdc++.h> #define N 400006 #define inf 1000000000 #define setIO(s) fr ...
- 小米 oj 纯位数
纯位数 序号:#101难度:非常难时间限制:2000ms内存限制:20M 描述 在数学中,所谓"纯位数"是指由相同位元重复而组成的自然数.比如在十进制中,1,22,333,555 ...