資料庫之間批量插入資料
阿新 • • 發佈:2018-11-22
package comnf147Package; import java.sql.*; public class DateMigrationLagou { //連線SQLite private Connection getSqlite() throws Exception { Class.forName("org.sqlite.JDBC"); return DriverManager.getConnection("jdbc:sqlite:E:\\data\\lagou.db"); } //連線MariaDB privateConnection getMariaDb() throws Exception { Class.forName("com.mysql.jdbc.Driver"); return DriverManager.getConnection("jdbc:mysql://localhost:3306/lagouDB?rewriteBatchedStatements=true", "root", "666666"); } //釋放資源 private void release(Connection coon, Statement st, ResultSet rs) {if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (st != null) { try { st.close(); } catch (SQLException e) { e.printStackTrace(); } }if (coon != null) { try { coon.close(); } catch (SQLException e) { e.printStackTrace(); } } } public void OperatingControl() { //從SQLite中取資料 Connection SQliteConn = null; Statement SQliteSt = null; ResultSet SQliteRS = null; //新增到MaiiaDb Connection MariaDbConn = null; PreparedStatement MariaDbPs = null; try { //獲取資料 SQliteConn = this.getSqlite(); SQliteSt = SQliteConn.createStatement(); SQliteRS = SQliteSt.executeQuery("select * from lagou_position"); //新增資料 MariaDbConn = this.getMariaDb(); MariaDbPs = MariaDbConn.prepareStatement("insert into lagou_position values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); // 關閉事務自動提交 ,這一行必須加上,否則每插入一條資料會向log插入一條日誌 MariaDbConn.setAutoCommit(false); int i = 0; //計時開始 long startime = System.currentTimeMillis(); //設定批量處理的數量 while (SQliteRS.next()) { for (int j = 1; j < 20; j++) { MariaDbPs.setObject(j, SQliteRS.getObject(j)); } MariaDbPs.addBatch(); //把若干sql語句裝載到一起,然後一次送到資料庫執行,執行需要很短的時間 // 每 10000 條,向資料庫傳送一次執行請求 if (++i % 10000 == 0) { MariaDbPs.executeBatch(); } } //執行批量處理語句; MariaDbPs.executeBatch(); //// 提交事務 MariaDbConn.commit(); //結束時間 long stoptime = System.currentTimeMillis(); //輸出結果 System.out.println("總資料" + i); System.out.println("插入用時" + (stoptime - startime) / 1000.0 + " 秒 "); } catch (Exception e) { try { if (MariaDbConn != null) { MariaDbConn.rollback(); } } catch (SQLException e1) { } } finally { this.release(SQliteConn, SQliteSt, SQliteRS); this.release(MariaDbConn, MariaDbPs, null); } } }
public static void main(String[] args) { //呼叫方式 DateMigrationLagou dateMigrationLagou = new DateMigrationLagou(); dateMigrationLagou.OperatingControl(); }