婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > 使用JDBC在MySQL數據庫中如何快速批量插入數據

使用JDBC在MySQL數據庫中如何快速批量插入數據

熱門標簽:安陽手機自動外呼系統原理是什么 神行者百貨商場地圖標注 地圖標注百度競價 地圖標注什么軟件好用 如何辦理400客服電話 外呼系統線路經常出問題嗎 外呼系統怎樣才能不封號 地圖標注專員入駐 西藏地圖標注改進點

使用JDBC連接MySQL數據庫進行數據插入的時候,特別是大批量數據連續插入(10W+),如何提高效率呢?

在JDBC編程接口中Statement 有兩個方法特別值得注意:

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()這一對方法可以實現批量處理數據。

不過值得注意的是,首先需要在數據庫鏈接中設置手動提交,connection.setAutoCommit(false),然后在執行Statement之后執行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=falserewriteBatchedStatements=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 秒.

這個效率仍然不高,似乎沒有達到想要的效果,需要進一步改進。

在MySQL JDBC連接字符串中還可以加入參數,

rewriteBatchedStatements=true,mysql默認關閉了batch處理,通過此參數進行打開,這個參數可以重寫向數據庫提交的SQL語句。

useServerPrepStmts=false,如果不開啟(useServerPrepStmts=false),使用com.mysql.jdbc.PreparedStatement進行本地SQL拼裝,最后送到db上就是已經替換了?后的最終SQL.

在此稍加改進,連接字符串中加入下面語句(代碼構造方法中去掉注釋):
connectStr += "?useServerPrepStmts=falserewriteBatchedStatements=true";

再次執行如下:

All down : 103498 
Convert finished. 
All spend time/s : 10 

同樣的數據量,這次執行只花費了10秒 ,處理效率大大提高.

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • JDBC連接MySQL數據庫批量插入數據過程詳解
  • JDBC連接MySql數據庫步驟 以及查詢、插入、刪除、更新等
  • java中JDBC實現往MySQL插入百萬級數據的實例代碼
  • Java使用JDBC向MySQL數據庫批次插入10W條數據(測試效率)
  • Java使用JDBC或MyBatis框架向Oracle中插入XMLType數據
  • java實現jdbc批量插入數據
  • JDBC鏈接mysql插入數據后顯示問號的原因及解決辦法
  • JDBC插入數據返回數據主鍵代碼實例

標簽:貴港 阜陽 萍鄉 雞西 衡水 酒泉 張掖 AXB

巨人網絡通訊聲明:本文標題《使用JDBC在MySQL數據庫中如何快速批量插入數據》,本文關鍵詞  使用,JDBC,在,MySQL,數據庫,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《使用JDBC在MySQL數據庫中如何快速批量插入數據》相關的同類信息!
  • 本頁收集關于使用JDBC在MySQL數據庫中如何快速批量插入數據的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 宣恩县| 西藏| 清新县| 大洼县| 乐昌市| 浏阳市| 大庆市| 江油市| 密山市| 邓州市| 当雄县| 西宁市| 常德市| 卢氏县| 定远县| 三都| 新疆| 甘孜县| 万山特区| 鹿邑县| 枝江市| 东阳市| 武平县| 彭泽县| 项城市| 昆山市| 阳原县| 米林县| 宁德市| 绩溪县| 岳普湖县| 洛浦县| 北海市| 容城县| 永宁县| 卢氏县| 甘谷县| 荥阳市| 丹棱县| 正安县| 惠水县|