使用JDBC在MySQL數(shù)據(jù)庫中如何快速批量插入數(shù)據(jù)
使用JDBC連接MySQL數(shù)據(jù)庫進(jìn)行數(shù)據(jù)插入的時(shí)候,特別是大批量數(shù)據(jù)連續(xù)插入(10W+),如何提高效率呢?
在JDBC編程接口中Statement 有兩個(gè)方法特別值得注意:
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()這一對方法可以實(shí)現(xiàn)批量處理數(shù)據(jù)。
不過值得注意的是,首先需要在數(shù)據(jù)庫鏈接中設(shè)置手動(dòng)提交,connection.setAutoCommit(false),然后在執(zhí)行Statement之后執(zhí)行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); // 設(shè)置手動(dòng)提交
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(); // 執(zhí)行批量處理
conn.commit(); // 提交
System.out.println("All down : " + count);
conn.close();
}
}
執(zhí)行完成以后:
All down : 103498
Convert finished.
All spend time/s : 47
一共10W+,執(zhí)行時(shí)間一共花費(fèi) 47 秒.
這個(gè)效率仍然不高,似乎沒有達(dá)到想要的效果,需要進(jìn)一步改進(jìn)。
在MySQL JDBC連接字符串中還可以加入?yún)?shù),
rewriteBatchedStatements=true,mysql默認(rèn)關(guān)閉了batch處理,通過此參數(shù)進(jìn)行打開,這個(gè)參數(shù)可以重寫向數(shù)據(jù)庫提交的SQL語句。
useServerPrepStmts=false,如果不開啟(useServerPrepStmts=false),使用com.mysql.jdbc.PreparedStatement進(jìn)行本地SQL拼裝,最后送到db上就是已經(jīng)替換了?后的最終SQL.
在此稍加改進(jìn),連接字符串中加入下面語句(代碼構(gòu)造方法中去掉注釋):
connectStr += "?useServerPrepStmts=false&rewriteBatchedStatements=true";
再次執(zhí)行如下:
All down : 103498
Convert finished.
All spend time/s : 10
同樣的數(shù)據(jù)量,這次執(zhí)行只花費(fèi)了10秒 ,處理效率大大提高.
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JDBC連接MySQL數(shù)據(jù)庫批量插入數(shù)據(jù)過程詳解
- JDBC連接MySql數(shù)據(jù)庫步驟 以及查詢、插入、刪除、更新等
- java中JDBC實(shí)現(xiàn)往MySQL插入百萬級(jí)數(shù)據(jù)的實(shí)例代碼
- Java使用JDBC向MySQL數(shù)據(jù)庫批次插入10W條數(shù)據(jù)(測試效率)
- Java使用JDBC或MyBatis框架向Oracle中插入XMLType數(shù)據(jù)
- java實(shí)現(xiàn)jdbc批量插入數(shù)據(jù)
- JDBC鏈接mysql插入數(shù)據(jù)后顯示問號(hào)的原因及解決辦法
- JDBC插入數(shù)據(jù)返回?cái)?shù)據(jù)主鍵代碼實(shí)例
相關(guān)文章
MySQL分組排序取每組第一條數(shù)據(jù)的實(shí)現(xiàn)
最近有個(gè)需求MySQL根據(jù)某一個(gè)字段分組,然后組內(nèi)排序,最后每組取排序后的第一條數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08
MySQL使用innobackupex備份連接服務(wù)器失敗的解決方法
這篇文章主要為大家詳細(xì)介紹了MySQL使用innobackupex備份連接服務(wù)器失敗的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
mysql5.7.33誤刪除ibdata文件找回?cái)?shù)據(jù)的方法
這篇文章主要介紹了mysql5.7.33誤刪除ibdata文件找回?cái)?shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Windows10系統(tǒng)下MySQL(8.0.37)安裝與配置教程
相信很多人都遇到過安裝Mysql的時(shí)候出現(xiàn)各種各樣的問題,下面這篇文章主要給大家介紹了關(guān)于Windows10系統(tǒng)下MySQL(8.0.37)安裝與配置的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07

