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


 

 

 

-- 批量插入
INSERT INTO TABLE (col1, col2, col3)
VALUES
    (val1, val2, val3),
    (val1, val2, val3),
    (val1, val2, val3)
;

-- 單條插入
INSERT INTO TABLE (col1, col2, col3) VALUES (val1, val2, val3);
INSERT INTO TABLE (col1, col2, col3) VALUES (val1, val2, val3);
INSERT INTO TABLE (col1, col2, col3) VALUES (val1, val2, val3);

-- 批量插入的效率比單條插入高N倍。

 

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); // 設置手動提交
        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();
    }

}

 

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

rewriteBatchedStatements=true,mysql默認關閉了batch處理,通過此參數進行打開,這個參數可以重寫向數據庫提交的SQL語句,具體參見:http://www.cnblogs.com/chenjianjx/archive/2012/08/14/2637914.html
useServerPrepStmts=false,如果不開啟(useServerPrepStmts=false),使用com.mysql.jdbc.PreparedStatement進行本地SQL拼裝,最后送到db上就是已經替換了?后的最終SQL.
 
在此稍加改進,連接字符串中加入下面語句(代碼構造方法中去掉注釋):
connectStr += "?useServerPrepStmts=false&rewriteBatchedStatements=true";
 
ref: https://www.cnblogs.com/hhthtt/p/11008307.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM