|
@ -1,28 +1,26 @@
|
|
|
package com.yihu.util.mysql2Oracle;
|
|
|
|
|
|
import java.io.*;
|
|
|
import java.sql.*;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* 查询mysql 导入执行 到oracle
|
|
|
* 查询mysql 导入执行 到oracle
|
|
|
*/
|
|
|
public class TranslateMysqlToOracle {
|
|
|
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
* @param tables
|
|
|
* @param tableName
|
|
|
* @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 {
|
|
|
public static List<String> exportSql(String tableName, String index, int startId, int page, int rows) throws Exception {
|
|
|
List<String> sqlList = new ArrayList<>();
|
|
|
String[] tableNames = tables;
|
|
|
// String[] tableNames = tables;
|
|
|
String indexName = index;
|
|
|
|
|
|
//TODO 修改链接
|
|
@ -34,44 +32,43 @@ public class TranslateMysqlToOracle {
|
|
|
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 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 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();
|
|
|
sqlList.add(insertSql);
|
|
|
}
|
|
|
resultSet.close();
|
|
|
preparedStatement.close();
|
|
|
|
|
|
connection.close();
|
|
|
exportSqlToOracle(sqlList);//执行sql到oracle
|
|
|
return sqlList;
|
|
@ -102,7 +99,7 @@ public class TranslateMysqlToOracle {
|
|
|
//执行批量执行
|
|
|
index++;
|
|
|
if (index % 1000 == 0) {
|
|
|
System.out.println("index:"+ index);
|
|
|
System.out.println("index:" + index);
|
|
|
preparedStatement.executeBatch();
|
|
|
}
|
|
|
}
|
|
@ -113,7 +110,7 @@ public class TranslateMysqlToOracle {
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
if (connection!=null) {
|
|
|
if (connection != null) {
|
|
|
connection.close();
|
|
|
}
|
|
|
}
|
|
@ -145,5 +142,4 @@ public class TranslateMysqlToOracle {
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|