|
@ -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;
|
|
|
}
|
|
|
}
|