如何利用jdbc快速插入百萬條資料

2021-07-24 15:04:16 字數 931 閱讀 5223

當須要向資料庫插入百萬條資料時,利用hibernate,mybatis等持久層框架時耗時較久,此時使用jdbc插入效率會更高。此種場景特別適用於讀取檔案匯入到資料庫。可以利用批處理來加快jdbc的插入效率。

string sql = "insert into person(id,name) values(?,?)"

;connection c = connectionutil.getconn();

preparedstatement ps = c.preparestatement(sql);

for (int i = 0

; i < 1000; i++)

ps.executebatch();

同時可以增長單條sql語句的長度,如:

string sql = "insert into person(id,name) values(?,?),(?,?)"

;connection c = connectionutil.getconn();

preparedstatement ps = c.preparestatement(sql);

ps.setint(1, 1);

ps.setstring(2, "a" + 1);

ps.setint(3, 2);

ps.setstring(4, "a" + 2);

ps.addbatch();

ps.executebatch();

快速插入百萬物件到資料庫,可以利用jdbc工具類,方便的插入資料。

listms = new arraylist<>();

msg m = new msg();

for (int i = 0; i < count; i++)

j.insertmillionobjects(ms, 10000, 500);

併發插入資料百萬條

1 新聞併發入庫資料 apioperation value 測試新增新聞動態 public void testcreate integer start,integer count 測試大批量新增新聞動態介面 param count param start void testto161 integer...

JDBC 百萬級別的批量插入

1,首先建立一張表 drop table employee create table employee first name varchar2 20 byte last name varchar2 20 byte emp no number,join date date tablespace sys...

如何瞬間在資料庫插入一百萬條記錄

這兩天發現了乙個東西很好玩 如何在你的資料庫中插入一百萬條記錄 要求三分鐘之內 hoo hoo 1 首先 create table news newsid int primary key auto increment,newstitle varchar 100 newscontent varchar...