使用JDBC在MySQL数据库中快速批量插入数据
使用JDBC连接MySQL数据库进行数据插入的时候,特别是大批量数据连续插入(10W+),如何提高效率呢?
在JDBC编程接口中Statement 有两个方法特别值得注意:
void addBatch() throws SQLException
PreparedStatement object's batch of commands.int[] executeBatch() throws SQLException
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. - 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 秒.
- All down : 103498
- Convert finished.
- All spend time/s : 10
使用JDBC在MySQL数据库中快速批量插入数据的更多相关文章
- 在 SQL 中 快速 批量 插入数据的方法
方法1:逐条执行,速度慢. INSERT INTO testimport (name, message) VALUES ('testname', 'jfksdfkdsfjksadljfkdsfjsdl ...
- 使用jdbc将mysql数据库中的内容封装为指定对象的list集合
使用jdbc将mysql数据库中的内容封装为指定对象的list集合 public List<User> findAll() { private JdbcTemplate template ...
- net core天马行空系列-各大数据库快速批量插入数据方法汇总
1.前言 hi,大家好,我是三合.我是怎么想起写一篇关于数据库快速批量插入的博客的呢?事情起源于我们工作中的一个需求,简单来说,就是有一个定时任务,从数据库里获取大量数据,在应用层面经过处理后再把结果 ...
- Java—JDBC向mysql数据库中给某个表添加数据时,会遇到的问题,如下
解析(jar包该放在那里,以及其它的操作): 把jar包(驱动)添加到自己的项目中,最好新建一个文件夹,再把jar包(驱动包)添加到这个所新建的文件中 1.先建好自己的项目,再新建一个文件夹,如下: ...
- 通过java代码往mysql数据库中写入日期相关数据少13个小时
通过show variables like '%time_zone%'; 查看时区: CST 时区 名为 CST 的时区是一个很混乱的时区,有四种含义: 美国中部时间 Central Standard ...
- 如何在MySQl数据库中给已有的数据表添加自增ID?
由于使用MySQL数据库还没有多久的缘故,在搭建后台往数据库导入数据的时候发现新增的表单是没有自增id的,因次就有了上面这个问题. 解决方法 1.给某一张表先增加一个字段,这里我们就以node_tab ...
- ThinPHP3.2中 addAll()批量插入数据
thinkphp中model类的addAll()方法可以将数据同时添加到数据库中. 1 2 3 4 5 6 // 批量添加数据 (only MySQL) $user = M('user'); //ar ...
- MySQL随机字符串函数批量插入数据
简单举个例子: drop table if exists demo1 create table demo1 ( id int primary key auto_increment, name ) ...
- MariaDB快速批量插入数据的几种办法
前言 当要向MariaDB中插入新的数据时,以下过程会影响插入所消耗的时间:(按时间消耗长短降序排序) 将数据sync到磁盘上(它是事务结束的一部分) 添加新的键值.索引越大,更新键值所消耗的时间就越 ...
随机推荐
- Laravel firstOrNew 与 firstOrCreate 的区别
例如: $item = App\Deployment::firstOrNew( ['name' => '问答小程序'], ['delayed' => 1] ); firstOrNew 需要 ...
- 对 Laravel 的 Controller 做 Unit Test
之前尝试过对 Laravel 的 Controller 做 Feature Test,但是在业务变得越来越复杂之后,我感觉对 controller 里的函数也没了自信,急需对功能函数做 Unit Te ...
- web----Tornado
安装: pip3 install tornado 源码安装 https://pypi.python.org/packages/source/t/tornado/tornado-4.3.tar.gz 简 ...
- hdu5443 ST表裸题:求区间最大
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #d ...
- python3的unittest中使用test suite(测试套件)执行指定测试用例
示例代码 module.py class baidumodule(): def __init__(self,driver,): self.dr = driver #不能在类中再次导入webdriver ...
- 步步为营-57-JQuery练习题
01 点谁谁哭 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head&g ...
- Asp.Net Core WebAPI入门整理(三)跨域处理
一.Core WebAPI中的跨域处理 1.在使用WebAPI项目的时候基本上都会用到跨域处理 2.Core WebAPI的项目中自带了跨域Cors的处理,不需要单独添加程序包 3.使用方法简单 ...
- java:根据利润表计算奖金所得
代码实现: public class Hello { public static void main(String srgs[]) { Lirun(100); Lirun2(100); } publi ...
- python--yield and generator(生成器)简述
1.想象一个场景: 设想,我想要100个素数,然后对它们累加求和. 通常的想法是,找一个一次性至少能提供100个素数的工具(函数),让它把这100个素数交给我(用return 一次性返回含 ...
- 解决linux下“XX不在 sudoers 文件中。此事将被报告"的问题
在使用sudo命令时,经常性会提示出“不在 sudoers 文件中.此事将被报告”的错误信息. 这是因为当前登录的账号不在sudo权限里面. sudo命令可以让你以root身份执行命令,来完成一些我们 ...