zoukankan      html  css  js  c++  java
  • Java 实现两个数据库数据的迁移

      原料:mysql,sqlite3

      思想步骤:

        首先从一个数据库取出数据,每取一条就添加到另一个数据库。

      示例:

    
    
    import java.sql.*;

    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();
    }
    }
    }

    }
     

      调用:

    
    
        public static void main(String[] args) {
    SQLite_To_MySQL test = new SQLite_To_MySQL();
    try {
    test.deal();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
     
  • 相关阅读:
    Good Bye 2014 B. New Year Permutation(floyd )
    hdu 5147 Sequence II (树状数组 求逆序数)
    POJ 1696 Space Ant (极角排序)
    POJ 2398 Toy Storage (叉积判断点和线段的关系)
    hdu 2897 邂逅明下 (简单巴什博弈)
    poj 1410 Intersection (判断线段与矩形相交 判线段相交)
    HDU 3400 Line belt (三分嵌套)
    Codeforces Round #279 (Div. 2) C. Hacking Cypher (大数取余)
    Codeforces Round #179 (Div. 2) B. Yaroslav and Two Strings (容斥原理)
    hdu 1576 A/B (求逆元)
  • 原文地址:https://www.cnblogs.com/oukele/p/9626006.html
Copyright © 2011-2022 走看看