hzy пре 7 година
родитељ
комит
b18df385b0

BIN
lib/mysql-connector-java-5.1.42.jar


BIN
lib/ojdbc14-10.2.0.3.0.jar


BIN
lib/ojdbc6-11.2.0.3.0.jar



BIN
lib/sqljdbc42.jar


BIN
lib/sqljdbc_auth.dll


+ 38 - 0
src/com/yihu/util/mysql2Oracle/MyThread2.java

@ -0,0 +1,38 @@
package com.yihu.util.mysql2Oracle;
public class MyThread2 extends Thread {
    private String[] tables;
    private String  index;
    private int total;
    private int maxId;
    private int rows;
    public MyThread2(String[] tables, String index,int maxId,  int row,int total)  throws Exception{
        this.tables = tables;
        this.maxId = maxId;
        this.rows = row;
        this.total = total;
        this.index = index;
    }
    public void run() {
        int count = total/rows + 1;
        long start = System.currentTimeMillis();
        try {
            for (int i= 0;i<count ;i++){
                TranslateMysqlToOracle.exportSql(tables,index,maxId,i,rows); //查询mysql 导入oracle
            }
            long end = System.currentTimeMillis();
            long coout = end - start;
            int num = total-maxId;
            System.out.println("表"+tables[0] + " ,总导入数据"+ num + " ,耗时:"+coout);
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.run();
    }
}

+ 38 - 0
src/com/yihu/util/mysql2Oracle/MysqlToOracleApplication.java

@ -0,0 +1,38 @@
package com.yihu.util.mysql2Oracle;
public class MysqlToOracleApplication {
    public static void main(String[] args) throws Exception {
        String[] tables1 = {"OutPatient"};
        String[] tables2 = {"Outpatient_Diag"};
        String[] tables3 = {"Outpatient_Drug"};
        String[] tables4 = {"Outpatient_Fee"};
        String[] tables5 = {"Inpatient_Drug"};
        String[] tables6 = {"Inpatient_Outdiag"};
        String[] tables7 = {"Inpatient_Longorder"};
        String[] tables8 = {"Inpatient_Fee"};
        String[] tables9 = {"Eds_Disp_Pres_Master"};
        MyThread2 MyThread21 = new MyThread2(tables1,"PK_Outpatient",210001,10000,566520);
        MyThread2 MyThread22 = new MyThread2(tables2,"PK_Outpatient_Diag ",390001,10000,1147364);
        MyThread2 MyThread23 = new MyThread2(tables3,"PK_Outpatient_Drug",130001,10000,3486629);
        MyThread2 MyThread24 = new MyThread2(tables4,"PK_Outpatient_Fee",150001,10000,1417369);
        MyThread2 MyThread25 = new MyThread2(tables5,"PK_Inpatient_Drug",170001,10000,3605361);
        MyThread2 MyThread26 = new MyThread2(tables6,"PK_Inpatient_Outdiag",430001,10000,1374793);
        MyThread2 MyThread27 = new MyThread2(tables7,"PK_Inpatient_Longorder",170001,10000,406293);
        MyThread2 MyThread28 = new MyThread2(tables8,"PK_Inpatient_Fee",170001,10000,1417369);
        MyThread2 MyThread29 = new MyThread2(tables9,"PK_Eds_Disp_Pres_Master",140001,10000,770550);
        MyThread21.start();
        MyThread22.start();
        MyThread23.start();
        MyThread24.start();
        MyThread25.start();
        MyThread26.start();
        MyThread27.start();
        MyThread28.start();
        MyThread29.start();
    }
}

+ 149 - 0
src/com/yihu/util/mysql2Oracle/TranslateMysqlToOracle.java

@ -0,0 +1,149 @@
package com.yihu.util.mysql2Oracle;
import java.io.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
 *    查询mysql 导入执行 到oracle
 */
public class TranslateMysqlToOracle {
    /**
     *
     * @param tables
     * @param startId
     * @param page
     * @param rows
     * @return
     * @throws Exception
     */
    public static List<String> exportSql(String[] tables, String index,  int startId, int page, int rows) throws Exception {
        List<String> sqlList = new ArrayList<>();
        String[] tableNames = tables;
        String indexName = index;
        //TODO 修改链接
        String driver = "com.mysql.jdbc.Driver"; //mysql
        String url = "jdbc:mysql://10.176.97.9:3310/srareatempfinal?useUnicode=true&characterEncoding=UTF-8&useSSL=false";
        String user = "chenweishan";
        String password = "GP8Qz4qU";
        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        int maxId = startId + (page * rows);
        for (String tableName : tableNames) {
            String sql = "select * from " + tableName + " where inner_id> "+ maxId + " limit "+ rows ;
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            ResultSetMetaData metaData = resultSet.getMetaData();
            while (resultSet.next()) {
                String insertSql = "";
                if (!indexName.isEmpty()) {
                    insertSql = "INSERT /*+ IGNORE_ROW_ON_DUPKEY_INDEX(" + tableName + "," + indexName + ")*/ INTO " + tableName + " ";
                } else {
                    insertSql = "INSERT INTO " + tableName + " ";
                }
                StringBuilder namesStr = new StringBuilder();
                StringBuffer valuesStrBuf = new StringBuffer();
                for (int i = 1; i <= metaData.getColumnCount(); i++) {
                    String columnName = metaData.getColumnName(i);
                    if (!"inner_id".equals(columnName)) {
                        String columnTypeName = metaData.getColumnTypeName(i);
                        Object value = resultSet.getObject(i);
                        //System.out.println(columnName + " - " + columnTypeName + " - " + value);
                        namesStr.append(columnName).append(",");
                        valuesStrBuf = joinValuesFragment(valuesStrBuf, columnTypeName, value);
                    }
                }
                String valuesStr = valuesStrBuf.toString();
                namesStr = new StringBuilder(namesStr.substring(0, namesStr.length() - 1));
                valuesStr = valuesStr.substring(0, valuesStr.length() - 1);
                insertSql += "(" + namesStr + ") VALUES (" + valuesStr + ")";
//                String sqlFilePath = directoryPath + tableName + ".sql";
//                writeFileContent(sqlFilePath, insertSql);
                sqlList.add(insertSql);
            }
            resultSet.close();
            preparedStatement.close();
        }
        connection.close();
        exportSqlToOracle(sqlList);//执行sql到oracle
        return sqlList;
    }
    //导入oracle
    public static void exportSqlToOracle(List<String> sqlList) throws SQLException {
        Connection connection = null;
        try {
            //TODO 修改链接
            String driver = "oracle.jdbc.driver.OracleDriver"; //Oracle
            String url = "jdbc:oracle:thin:@10.16.11.9:1521/sjpt_p4";
            String user = "shangrao";
            String password = "shangrao_2017";
//            String driver = "oracle.jdbc.driver.OracleDriver"; //Oracle
//            String url = "jdbc:oracle:thin:@172.19.103.71:1521/neworcl";
//            String user = "cp_hos";
//            String password = "cp_hos";
            Class.forName(driver);
            connection = DriverManager.getConnection(url, user, password);
            int index = 0;
            Statement preparedStatement = connection.createStatement();
            for (String sql : sqlList) {
                preparedStatement.addBatch(sql);
                //执行批量执行
                index++;
                if (index % 1000 == 0) {
                    System.out.println("index:"+ index);
                    preparedStatement.executeBatch();
                }
            }
            preparedStatement.executeBatch();
            preparedStatement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (connection!=null) {
                connection.close();
            }
        }
    }
    public static StringBuffer joinValuesFragment(StringBuffer valueSqlStr, String columnType, Object value) {
        if (value == null) {
            valueSqlStr.append("NULL,");
        } else {
            columnType = columnType.toLowerCase();
            switch (columnType) {
                case "varchar":
                    valueSqlStr.append("'" + value + "',");
                    break;
                case "datetime":
                    String s = value.toString();
                    s = s.substring(0, s.indexOf("."));
                    valueSqlStr.append("to_date('" + s + "','yyyy-mm-dd hh24:mi:ss')" + ",");
                    break;
                case "date":
                    valueSqlStr.append("to_date('" + value + "','yyyy-mm-dd')" + ",");
                    break;
                default:
                    valueSqlStr.append(value + ",");
                    break;
            }
        }
        return valueSqlStr;
    }
}

+ 36 - 0
src/com/yihu/util/sqlServer2Oracle/MainApplication.java

@ -0,0 +1,36 @@
package com.yihu.util.sqlServer2Oracle;
public class MainApplication {
    public static void main(String[] args) throws Exception {
        String[] tables1 = {"OutPatient"};
        String[] tables2 = {"Outpatient_Diag"};
        String[] tables3 = {"Outpatient_Drug"};
        String[] tables4 = {"Outpatient_Fee"};
        String[] tables5 = {"Inpatient_Drug"};
        String[] tables6 = {"Inpatient_Outdiag"};
        String[] tables7 = {"Inpatient_Longorder"};
        String[] tables8 = {"Inpatient_Fee"};
        String[] tables9 = {"Eds_Disp_Pres_Master"};
        MyThread myThread1 = new MyThread(tables1,"PK_Outpatient",22,100,67);
        MyThread myThread2 = new MyThread(tables2,"PK_Outpatient_Diag ",40,100,115);
        MyThread myThread3 = new MyThread(tables3,"PK_Outpatient_Drug",14,100,349);
        MyThread myThread4 = new MyThread(tables4,"PK_Outpatient_Fee",16,100,142);
        MyThread myThread5 = new MyThread(tables5,"PK_Inpatient_Drug",18,100,349);
        MyThread myThread6 = new MyThread(tables6,"PK_Inpatient_Outdiag",44,100,138);
        MyThread myThread7 = new MyThread(tables7,"PK_Inpatient_Longorder",18,100,41);
        MyThread myThread8 = new MyThread(tables8,"PK_Inpatient_Fee",18,100,123);
        MyThread myThread9 = new MyThread(tables9,"PK_Eds_Disp_Pres_Master",15,100,78);
        //�߳�����
        myThread1.start();
        myThread2.start();
        myThread3.start();
        myThread4.start();
        myThread5.start();
        myThread6.start();
        myThread7.start();
        myThread8.start();
        myThread9.start();
    }
}

+ 33 - 0
src/com/yihu/util/sqlServer2Oracle/MyThread.java

@ -0,0 +1,33 @@
package com.yihu.util.sqlServer2Oracle;
import com.yihu.util.mysql2Oracle.TranslateMysqlToOracle;
public class MyThread extends Thread {
    private String[] tables;
    private String index;
    private int pages;
    private int rows;
    private int total;
    public MyThread(String[] tables, String index, int page, int row,int totalCount)  throws Exception{
        this.tables = tables;
        this.index = index;
        this.pages = page;
        this.rows = row;
        this.total = totalCount;
    }
    public void run() {
        try {
            for(int i = pages ; i< total; i++){
                TranslateInsertSqlUtil.exportSql(tables,index,i,rows);
                System.out.println( tables[0].toString() + "  is export =  " + i + "  page total = " + i*rows + " rows" );
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.run();
    }
}

+ 158 - 0
src/com/yihu/util/sqlServer2Oracle/TranslateInsertSqlUtil.java

@ -0,0 +1,158 @@
package com.yihu.util.sqlServer2Oracle;
import com.sun.xml.internal.bind.v2.model.core.ID;
import java.io.*;
import java.sql.*;
public class TranslateInsertSqlUtil {
    public static void exportSql(String[] tables,String index,int page ,int rows) throws Exception{
        String[] tableNames = tables;
        String indexName = index;
        String directoryPath = page+"date";
        File dir = new File(directoryPath);
        if (!dir.exists()) {
            dir.mkdir();
        }
//        String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //SQLServerDriver
//        String url = "jdbc:sqlserver://localhost:1433;databaseName=SRAreaTempFinal;";
//        String user = "sa";
//        String password = "jkzl_2015";
        String driver = "com.mysql.jdbc.Driver"; //mysql
        String url = "jdbc:sqlserver://171.35.109.39:3310;databaseName=srareatempfinal;";
        String user = "chenweishan";
        String password = "GP8Qz4qU";
        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        int count = (page*rows)+rows;
        for (String tableName : tableNames) {
            String sql = "select * from " + tableName + " w1" +
                    " WHERE inner_id  in \n" +
                    "( \n" +
                    "SELECT top "+ rows + " inner_id  FROM \n" +
                    "( \n" +
                    "SELECT top "+ count +" inner_id  FROM " + tableName + " ORDER BY inner_id  ASC \n" +
                    ") w ORDER BY w.inner_id  DESC \n" +
                    ") \n" +
                    "ORDER BY w1.inner_id  ASC";
            PreparedStatement preparedStatement = connection.prepareStatement(sql );
            ResultSet resultSet = preparedStatement.executeQuery();
            ResultSetMetaData metaData = resultSet.getMetaData();
            while (resultSet.next()) {
                String insertSql = "";
                if(!indexName.isEmpty()){
                    insertSql = "INSERT /*+ IGNORE_ROW_ON_DUPKEY_INDEX(" + tableName + "," + indexName +")*/ INTO " + tableName + " ";
                }else{
                    insertSql = "INSERT INTO " + tableName + " ";
                }
                StringBuilder namesStr = new StringBuilder();
                StringBuffer valuesStrBuf = new StringBuffer();
                for (int i = 1; i <= metaData.getColumnCount(); i++) {
                    String columnName = metaData.getColumnName(i);
                    if (!"inner_id".equals(columnName)) {
                        String columnTypeName = metaData.getColumnTypeName(i);
                        Object value = resultSet.getObject(i);
                        //System.out.println(columnName + " - " + columnTypeName + " - " + value);
                        namesStr.append(columnName).append(",");
                        valuesStrBuf = joinValuesFragment(valuesStrBuf, columnTypeName, value);
                    }
                }
                String valuesStr = valuesStrBuf.toString();
                namesStr = new StringBuilder(namesStr.substring(0, namesStr.length() - 1));
                valuesStr = valuesStr.substring(0, valuesStr.length() - 1);
                insertSql += "(" + namesStr + ") VALUES (" + valuesStr + ");";
                String sqlFilePath = directoryPath + tableName + ".sql";
                writeFileContent(sqlFilePath, insertSql);
            }
            resultSet.close();
            preparedStatement.close();
        }
        connection.close();
    }
    public static StringBuffer joinValuesFragment(StringBuffer valueSqlStr, String columnType, Object value) {
        if (value == null) {
            valueSqlStr.append("NULL,");
        } else {
            switch (columnType) {
                case "varchar":
                    valueSqlStr.append("'" + value + "',");
                    break;
                case "datetime":
                    String s = value.toString();
                    s = s.substring(0, s.indexOf("."));
                    valueSqlStr.append("to_date('" + s + "','yyyy-mm-dd hh24:mi:ss')" + ",");
                    break;
                case "date":
                    valueSqlStr.append("to_date('" + value + "','yyyy-mm-dd')" + ",");
                    break;
                default:
                    valueSqlStr.append(value + ",");
                    break;
            }
        }
        return valueSqlStr;
    }
    public static boolean writeFileContent(String filepath, String content) throws IOException {
        Boolean bool = false;
        String temp = "";
        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        FileOutputStream fos = null;
        PrintWriter pw = null;
        try {
            File file = new File(filepath);
            if (!file.exists()) {
                file.createNewFile();
            }
            fis = new FileInputStream(file);
            isr = new InputStreamReader(fis);
            br = new BufferedReader(isr);
            StringBuffer buffer = new StringBuffer();
            for (int i = 0; (temp = br.readLine()) != null; i++) {
                buffer.append(temp);
                // 行与行之间的分隔符 相当于“\n”
                buffer = buffer.append(System.getProperty("line.separator"));
            }
            buffer.append(content);
            fos = new FileOutputStream(file);
            pw = new PrintWriter(fos);
            pw.write(buffer.toString().toCharArray());
            pw.flush();
            bool = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (pw != null) {
                pw.close();
            }
            if (fos != null) {
                fos.close();
            }
            if (br != null) {
                br.close();
            }
            if (isr != null) {
                isr.close();
            }
            if (fis != null) {
                fis.close();
            }
        }
        return bool;
    }
}