Java实现数据批量导入mysql数据库
本文完全照搬别人的。
原文标题:Java实现数据批量导入数据库(优化速度-2种方法)
原文地址:https://blog.csdn.net/qy20115549/article/details/52699724
原文看着更清晰,请移步原文查看。
连接数据库
package db;
import java.sql.Connection;
import java.sql.DriverManager;
/*
* 合肥工业大学 管理学院 qianyang 1563178220@qq.com
*/
public class MySQLConnections {
private String driver = "";
private String dbURL = "";
private String user = "";
private String password = "";
private static MySQLConnections connection = null; private MySQLConnections() throws Exception {
driver = "com.mysql.jdbc.Driver";
dbURL = "jdbc:mysql://127.0.0.1:3306/test";
user = "root";
password = "112233";
System.out.println("dbURL:" + dbURL);
} public static Connection getConnection() {
Connection conn = null;
if (connection == null) {
try {
connection = new MySQLConnections();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
try {
Class.forName(connection.driver);
conn = DriverManager.getConnection(connection.dbURL,
connection.user, connection.password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}
批量处理的两种方式
第一种方式,是每5000条记录放入数据库,一次。也就是每次提交的记录都有5000条,当然最后一次可能不是。另外,一种方式,是使用默认的提交方式。两者的处理速度都不错。50000条记录,大概15秒左右。
package db;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/*
* 合肥工业大学 管理学院 qianyang 1563178220@qq.com
*/
public class MySQLUpdate {
static Connection con = MySQLConnections.getConnection();
static PreparedStatement stmt = null;
public static int executeInsert() throws SQLException {
int i = 0;
//设置批量处理的数量
int batchSize = 5000;
stmt = con.prepareStatement("insert into mysqltest (id,name) "
+ "values (?,?)");
// 关闭事务自动提交 ,这一行必须加上
con.setAutoCommit(false);
for (int j = 0; j < 50005; j++){
++i;
stmt.setInt(1, j);
stmt.setString(2, "name");
stmt.addBatch();
if ( i % batchSize == 0 ) {
stmt.executeBatch();
con.commit();
}
}
if ( i % batchSize != 0 ) {
stmt.executeBatch();
con.commit();
}
return i;
} public static void executeInsert2() throws SQLException {
// 关闭事务自动提交 ,这一行必须加上
con.setAutoCommit(false);
stmt = con.prepareStatement("insert into mysqltest1 (id,name) "
+ "values (?,?)");
for (int j = 0; j < 50002; j++){
stmt.setInt(1, j);
stmt.setString(2, "name");
stmt.addBatch();
}
stmt.executeBatch();
con.commit();
stmt.close();
con.close();
} }
main方法
package main;
import java.sql.SQLException;
import db.MySQLUpdate;
/*
* 合肥工业大学 管理学院 qianyang 1563178220@qq.com
*/
public class Test { public static void main(String[] args) throws SQLException {
// long begin1 = System.currentTimeMillis();
// MySQLUpdate.executeInsert();
// long end1 = System.currentTimeMillis();
// System.out.println("程序运行时间为:"+(end1-begin1)); long begin2 = System.currentTimeMillis();
MySQLUpdate.executeInsert1();
long end2 = System.currentTimeMillis();
System.out.println("程序运行时间为:"+(end2-begin2));
} }
Java实现数据批量导入mysql数据库的更多相关文章
- SpringMVC文件上传 Excle文件 Poi解析 验证 去重 并批量导入 MYSQL数据库
SpringMVC文件上传 Excle文件 Poi解析并批量导入 MYSQL数据库 /** * 业务需求说明: * 1 批量导入成员 并且 自主创建账号 * 2 校验数据格式 且 重复导入提示 已被 ...
- Weka里如何将arff文件或csv文件批量导入MySQL数据库(六)
这里不多说,直接上干货! 前提博客是 Weka中数据挖掘与机器学习系列之数据格式ARFF和CSV文件格式之间的转换(四) 1.将arff文件批量导入MySQL数据库 我在这里,arff文件以Weka安 ...
- .net core利用MySqlBulkLoader大数据批量导入MySQL
最近用core写了一个数据迁移小工具,从SQLServer读取数据,加工后导入MySQL,由于数据量太过庞大,数据表都过百万,常用的dapper已经无法满足.三大数据库都有自己的大数据批量导入数据的方 ...
- 使用PHPExcel实现数据批量导入到数据库
此例子只使用execel2003的.xls文档,若使用的是其他版本,可以保存格式为“Execel 97-2003 工作簿(*.xls)”即.xls文件类型即可! 功能说明:只能上传Excel2003类 ...
- 对大数据的批量导入MySQL数据库
自己的库里有索引在用insert导入数据时会变慢很多 使用事务+批量导入 可以配置使用spring+mybatis整合的方式关闭自动提交事务(地址),选择批量导入每一百条导入使用list存储值传入到m ...
- 使用solr批量导入mysql数据库,以及Unable to read: dataimport.properties等坑
折腾了一下午终于成功了!先放一张成功图: 成功把mysql的数据添加进去了,我这里是整合了tomcat9,整合步骤挺麻烦的,百度一大堆! 这里主要介绍批量导入数据,这里有些坑,所以记录一下: 步骤: ...
- Excel数据批量导入到数据库
1.今天做批量导入网上找了个例子,改了改,运行起来了.用POI实现Excel的读取,需要jar包. 2.ReadExcel.java读取数据 /** * */ package com.b510.exc ...
- sql文件批量导入mysql数据库
有一百多个sql文件肿么破?一行一行地导入数据库肯定是极其愚蠢的做法,但是我差点就这么做了... 网上首先找到的方法是:写一个xxx.sql文件,里边每一行都是source *.sql ...,之后再 ...
- Excel数据批量导入到数据库2
1.导包(共3个) 2.jsp <s:form action="ReadExcel.action" method="post" enctype=" ...
随机推荐
- Idea控制台中文乱码
在菜单栏找到”run->editconfigration” 找到”server”选项卡 设置 vm option为 -Dfile.encoding=utf-8
- LeetCode_409. Longest Palindrome
409. Longest Palindrome Easy Given a string which consists of lowercase or uppercase letters, find t ...
- C++运行出现"what(): std::bad_alloc"的解决办法
注:这里只是我的代码出现这种情况及对应的解决办法,你的代码不一定出现和我一样的情况.左移这篇随笔仅供参考. 运行程序出现如下结果: terminate called after throwing an ...
- [LeetCode] 71. Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- [LeetCode] 168. Excel Sheet Column Title 求Excel表列名称
Given a positive integer, return its corresponding column title as appear in an Excel sheet. For exa ...
- [LeetCode] 240. Search a 2D Matrix II 搜索一个二维矩阵 II
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [LeetCode] 349. Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...
- python:单例模式--使用__new__(cls)实现
单例模式:即一个类有且仅有一个实例. 那么通过python怎么实现一个类只能有一个实例呢. class Earth: """ 假如你是神,你可以创造地球 "&q ...
- pytest+allure测试框架搭建
https://blog.csdn.net/wust_lh/article/details/86685912 https://www.jianshu.com/p/9673b2aeb0d3 定制化展示数 ...
- TP5 模型CURD
ThinkPHP5的模型是一种对象-关系映射(Object / Relation Mapping ,简称 ORM)的封装,并且提供了简洁的ActiveRecord实现.一般来说,每个数据表会和一个“模 ...