Java代碼實現兩種數據庫的數據遷移


https://www.cnblogs.com/oukele/p/9626006.html

public class SQLite_To_MySQL {

    private Connection getIteconn(){
        try {
            Class.forName("org.sqlite.JDBC");
            return DriverManager.getConnection("jdbc:sqlite:E:\\MyDB\\lagou.db");
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    private Connection getMysqlconn(){
        try {
            Class.forName("org.mariadb.jdbc.Driver");
            return DriverManager.getConnection("jdbc:mariadb://localhost:3306/test","oukele","oukele");
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void deal() throws SQLException {
        //SQLite數據庫
        Connection iteconn = getIteconn();
        Statement itestmt =iteconn.createStatement();
        ResultSet iters = itestmt.executeQuery("select * from lagou_position");

        //結果集獲取到的長度
        int size = iters.getMetaData().getColumnCount();
        //比較懶,拼接insert into 語句
        StringBuffer sbf =new StringBuffer();
        sbf.append("insert into lagou values (");
        String link ="";
        for (int i = 0; i <size ; i++) {
            sbf.append(link).append("?");
            link=",";
        }
        sbf.append(")");
        //MySQL數據庫
        Connection mysqlconn = getMysqlconn();
        PreparedStatement mysqlpstmt = mysqlconn.prepareStatement(sbf.toString());

        //取出結果集並向MySQL數據庫插入數據 ( 使用批處理 )
        //完成條數
        int count =0;
        int num=0;
        //取消事務(不寫入日志)
        mysqlconn.setAutoCommit(false);
        long start = System.currentTimeMillis();
        while (iters.next()) {
            ++count;
            for (int i=1;i<= size;i++) {
                mysqlpstmt.setObject(i, iters.getObject(i));
            }

            //將預先語句存儲起來,這里還沒有向數據庫插入
            mysqlpstmt.addBatch();
            //當count 到達 20000條時 向數據庫提交
            if (count % 20000 ==0 ){
                ++num;
                mysqlpstmt.executeBatch();
                System.out.println("第"+num+"次提交,耗時:"+(System.currentTimeMillis()-start)/1000.0+"s");
            }
        }
        //防止有數據未提交
        mysqlpstmt.executeBatch();
        //提交
        mysqlconn.commit();
        System.out.println("完成 "+count+" 條數據,耗時:"+(System.currentTimeMillis()-start)/1000.0+"s");
        //恢復事務
       // mysqlconn.setAutoCommit(true);

        //關閉資源
        close(mysqlconn,mysqlpstmt,null);
        close(iteconn,itestmt,iters);

    }

    public  void  close(Connection conn,Statement stmt,ResultSet rs){

        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(stmt!=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}

 


免責聲明!

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



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