Java實現(xiàn)十秒向MySQL插入百萬條數(shù)據(jù)
mysql數(shù)據(jù)庫準備
private String Driver = "com.mysql.cj.jdbc.Driver";
private String url ="jdbc:mysql://localhost:3306/mp?serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true";
private String user = "root";
private String password = "root";
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
//封裝與數(shù)據(jù)庫建立連接的類
public void coon() throws Exception{
Class.forName(Driver);
connection = DriverManager.getConnection(url,user,password);
}
//封裝異常類
public void erro(){
try {
if (rs!=null){
rs.close();
}
if (ps!=null){
ps.close();
}
if (connection!=null){
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}方式一:普通插入
package com.wt;
import org.junit.Test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
/**
* @Author wt
* @Date 2022/11/14 21:17
* @PackageName:com.wt
* @ClassName: TestAddBatch01
* @Description: TODO
* @Version 1.0
*/
public class TestAddBatch01 {
private String Driver = "com.mysql.cj.jdbc.Driver";
private String url ="jdbc:mysql://localhost:3306/mp?serverTimezone=Asia/Shanghai";
private String user = "root";
private String password = "root";
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
public void coon() throws Exception{
Class.forName(Driver);
connection = DriverManager.getConnection(url,user,password);
}
public void erro(){
try {
if (rs!=null){
rs.close();
}
if (ps!=null){
ps.close();
}
if (connection!=null){
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void ccc(){
long start = System.currentTimeMillis();
String sql = "insert into a(id, name) VALUES (?,null)";
try {
coon();
ps = connection.prepareStatement(sql);
for (int i = 1; i <= 1000000; i++) {
ps.setObject(1, i);//填充sql語句種得占位符
ps.execute();//執(zhí)行sql語句
}
} catch (Exception e) {
e.printStackTrace();
} finally {
erro();
}
System.out.println("百萬條數(shù)據(jù)插入用時:" + (System.currentTimeMillis() - start)+"【單位:毫秒】");
}
}用時:62分鐘多

方式二:使用批處理插入
package com.wt;
import org.junit.Test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
/**
* @Author wt
* @Date 2022/11/14 20:25
* @PackageName:com.wt.util
* @ClassName: TestAddBatch
* @Description: TODO
* @Version 1.0
*/
public class TestAddBatch {
private String Driver = "com.mysql.cj.jdbc.Driver";
private String url ="jdbc:mysql://localhost:3306/mp?serverTimezone=Asia/Shanghai";
private String user = "root";
private String password = "root";
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
public void coon() throws Exception{
Class.forName(Driver);
connection = DriverManager.getConnection(url,user,password);
}
public void erro(){
try {
if (rs!=null){
rs.close();
}
if (ps!=null){
ps.close();
}
if (connection!=null){
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void ccc(){
long start = System.currentTimeMillis();
String sql = "insert into a(id, name) VALUES (?,null)";
try {
coon();
ps = connection.prepareStatement(sql);
// connection.setAutoCommit(false);//取消自動提交
for (int i = 1; i <= 1000000; i++) {
ps.setObject(1, i);
ps.addBatch();
if (i % 1000 == 0) {
ps.executeBatch();
ps.clearBatch();
}
}
ps.executeBatch();
ps.clearBatch();
// connection.commit();//所有語句都執(zhí)行完畢后才手動提交sql語句
} catch (Exception e) {
e.printStackTrace();
} finally {
erro();
}
System.out.println("百萬條數(shù)據(jù)插入用時:" + (System.currentTimeMillis() - start)+"【單位:毫秒】");
}
}
方式一、二總結:到此可以看出其實其處理程序及批處理是沒有起作用的,為此我們使用方式三
方式三:通過連接配置url設置【&rewriteBatchedStatements=true】(設置重寫批處理語句)
url地址后注意添加【&rewriteBatchedStatements=true】
private String url ="jdbc:mysql://localhost:3306/mp?serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true";
方法三較于方法二的改變是只是url地址上的改變,其它沒有任何修改
package com.wt;
import org.junit.Test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
/**
* @Author wt
* @Date 2022/11/14 20:25
* @PackageName:com.wt.util
* @ClassName: TestAddBatch
* @Description: TODO
* @Version 1.0
*/
public class TestAddBatch {
private String Driver = "com.mysql.cj.jdbc.Driver";
private String url ="jdbc:mysql://localhost:3306/mp?serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true";
private String user = "root";
private String password = "root";
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
public void coon() throws Exception{
Class.forName(Driver);
connection = DriverManager.getConnection(url,user,password);
}
public void erro(){
try {
if (rs!=null){
rs.close();
}
if (ps!=null){
ps.close();
}
if (connection!=null){
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void ccc(){
long start = System.currentTimeMillis();
String sql = "insert into a(id, name) VALUES (?,null)";
try {
coon();
ps = connection.prepareStatement(sql);
for (int i = 1; i <= 1000000; i++) {
ps.setObject(1, i);
ps.addBatch();
if (i % 1000 == 0) {
ps.executeBatch();
ps.clearBatch();
}
}
ps.executeBatch();
ps.clearBatch();
} catch (Exception e) {
e.printStackTrace();
} finally {
erro();
}
System.out.println("百萬條數(shù)據(jù)插入用時:" + (System.currentTimeMillis() - start)+"【單位:毫秒】");
}
}
用時:【10秒左右】

到此批處理語句才正是生效
注意
數(shù)據(jù)庫連接的url設置了【&rewriteBatchedStatements=true】時,java代碼種的sql語句不能有分號【;】號,否則批處理語句打包就會出現(xiàn)錯誤,導致后面的sql語句提交出現(xiàn)【BatchUpdateException】異常

方式四:通過數(shù)據(jù)庫連接取消自動提交,手動提交數(shù)據(jù)
package com.wt;
import org.junit.Test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
/**
* @Author wt
* @Date 2022/11/14 20:25
* @PackageName:com.wt.util
* @ClassName: TestAddBatch
* @Description: TODO
* @Version 1.0
*/
public class TestAddBatch {
private String Driver = "com.mysql.cj.jdbc.Driver";
private String url ="jdbc:mysql://localhost:3306/mp?serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true";
private String user = "root";
private String password = "root";
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
public void coon() throws Exception{
Class.forName(Driver);
connection = DriverManager.getConnection(url,user,password);
}
public void erro(){
try {
if (rs!=null){
rs.close();
}
if (ps!=null){
ps.close();
}
if (connection!=null){
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void ccc(){
long start = System.currentTimeMillis();
String sql = "insert into a(id, name) VALUES (?,null)";
try {
coon();
ps = connection.prepareStatement(sql);
connection.setAutoCommit(false);//取消自動提交
for (int i = 1; i <= 1000000; i++) {
ps.setObject(1, i);
ps.addBatch();
if (i % 1000 == 0) {
ps.executeBatch();
ps.clearBatch();
}
}
ps.executeBatch();
ps.clearBatch();
connection.commit();//所有語句都執(zhí)行完畢后才手動提交sql語句
} catch (Exception e) {
e.printStackTrace();
} finally {
erro();
}
System.out.println("百萬條數(shù)據(jù)插入用時:" + (System.currentTimeMillis() - start)+"【單位:毫秒】");
}
}
用時:【9秒左右】

總結:
1.使用批量提交數(shù)據(jù),url一定要設置允許重寫批量提交【rewriteBatchedStatements=true】,以及此時的sql語句一定不能有分號,否則有【BatchUpdateException】異常,
2.其他的就正常使用PreparedStatement ps;的以下三個方法即可
- ps.addBatch(); 將sql語句打包到一個容器中
- ps.executeBatch(); 將容器中的sql語句提交
- ps.clearBatch(); 清空容器,為下一次打包做準備
以上就是Java實現(xiàn)十秒向MySQL插入百萬條數(shù)據(jù)的詳細內(nèi)容,更多關于Java MySQL插入數(shù)據(jù)的資料請關注腳本之家其它相關文章!
相關文章
Spring?Boot?4.0對于Java開發(fā)的影響和前景
探索Spring?Boot?4.0如何徹底革新Java開發(fā),提升效率并開拓未來可能性!別錯過這篇緊湊的指南,它帶你領略Spring?Boot的強大魅力和潛力,準備好了嗎?2024-02-02
GateWay路由規(guī)則與動態(tài)路由詳細介紹
這篇文章主要介紹了GateWay路由規(guī)則與GateWay動態(tài)路由,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09
Spring Security OAuth2實現(xiàn)使用JWT的示例代碼
這篇文章主要介紹了Spring Security OAuth2實現(xiàn)使用JWT的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09
SpringBoot 利用MultipartFile上傳本地圖片生成圖片鏈接的實現(xiàn)方法
這篇文章主要介紹了SpringBoot 利用MultipartFile上傳本地圖片生成圖片鏈接的實現(xiàn)方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03

