2022-10-20 11:12:19 -04:00
|
|
|
package jrunner;
|
2022-10-20 11:18:05 -04:00
|
|
|
import java.sql.*;
|
|
|
|
import java.util.*;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path ;
|
|
|
|
import java.nio.file.Paths;
|
2022-10-27 07:16:42 -04:00
|
|
|
import java.time.*;
|
2022-10-20 11:12:19 -04:00
|
|
|
|
|
|
|
public class jrunner {
|
2022-10-20 11:18:05 -04:00
|
|
|
//static final String QUERY = "SELECT * from rlarp.osm LIMIT 100";
|
|
|
|
|
2022-10-20 11:12:19 -04:00
|
|
|
public static void main(String[] args) {
|
2022-10-20 11:18:05 -04:00
|
|
|
|
|
|
|
String scu = "";
|
|
|
|
String scn = "";
|
|
|
|
String scp = "";
|
|
|
|
String dcu = "";
|
|
|
|
String dcn = "";
|
|
|
|
String dcp = "";
|
|
|
|
String sq = "";
|
|
|
|
String dt = "";
|
|
|
|
Boolean trim = true;
|
|
|
|
Integer r = 0;
|
|
|
|
Integer t = 0;
|
|
|
|
String sql = "";
|
|
|
|
String nr = "";
|
|
|
|
String nc = "";
|
|
|
|
String nl = "\n";
|
|
|
|
String msg = "";
|
|
|
|
Connection scon = null;
|
|
|
|
Connection dcon = null;
|
|
|
|
Statement stmt = null;
|
|
|
|
Statement stmtd = null;
|
|
|
|
ResultSet rs = null;
|
|
|
|
String[] getv = null;
|
|
|
|
Integer cols = null;
|
|
|
|
String[] dtn = null;
|
2022-10-27 07:16:42 -04:00
|
|
|
Timestamp tsStart = null;
|
|
|
|
Timestamp tsEnd = null;
|
2022-10-20 11:18:05 -04:00
|
|
|
|
2022-10-27 07:17:13 -04:00
|
|
|
msg = "jrunner version 0.38";
|
2022-10-20 11:18:05 -04:00
|
|
|
msg = msg + nl + "-scu source jdbc url";
|
|
|
|
msg = msg + nl + "-scn source username";
|
|
|
|
msg = msg + nl + "-scp source passowrd";
|
|
|
|
msg = msg + nl + "-dcu destination jdbc url";
|
|
|
|
msg = msg + nl + "-dcn destination username";
|
|
|
|
msg = msg + nl + "-dcp destination passowrd";
|
|
|
|
msg = msg + nl + "-sq path to source query";
|
|
|
|
msg = msg + nl + "-dt fully qualified name of destination table";
|
|
|
|
msg = msg + nl + "-t trim text";
|
|
|
|
msg = msg + nl + "--help info";
|
|
|
|
|
|
|
|
//---------------------------------------parse args into variables-------------------------------------------------
|
|
|
|
|
|
|
|
for (int i = 0; i < args.length; i = i +1 ){
|
|
|
|
switch (args[i]) {
|
|
|
|
//source connection string
|
|
|
|
case "-scu":
|
|
|
|
scu = args[i+1];
|
|
|
|
break;
|
|
|
|
//source username
|
|
|
|
case "-scn":
|
|
|
|
scn = args[i+1];
|
|
|
|
break;
|
|
|
|
//source password
|
2022-10-27 07:16:42 -04:00
|
|
|
//import java.time.*;
|
2022-10-20 11:18:05 -04:00
|
|
|
case "-scp":
|
|
|
|
scp = args[i+1];
|
|
|
|
break;
|
|
|
|
//destination connection string
|
|
|
|
case "-dcu":
|
|
|
|
dcu = args[i+1];
|
|
|
|
break;
|
|
|
|
//destination username
|
|
|
|
case "-dcn":
|
|
|
|
dcn = args[i+1];
|
|
|
|
break;
|
|
|
|
//destination password
|
|
|
|
case "-dcp":
|
|
|
|
dcp = args[i+1];
|
|
|
|
break;
|
|
|
|
//source query path
|
|
|
|
case "-sq":
|
|
|
|
try {
|
|
|
|
//sq = Files.readAllLines(Paths.get(args[i+1]));
|
|
|
|
sq = Files.readString(Paths.get(args[i+1]));
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
//System.out.println(nl + "error reasing source sql file: " + printStackTrace());
|
|
|
|
e.printStackTrace();
|
|
|
|
System.exit(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
//destination table name
|
|
|
|
case "-dt":
|
|
|
|
dt = args[i+1];
|
|
|
|
break;
|
|
|
|
case "-t":
|
|
|
|
trim = true;
|
|
|
|
break;
|
|
|
|
case "-v":
|
|
|
|
System.out.println(msg);
|
|
|
|
return;
|
|
|
|
case "--version":
|
|
|
|
System.out.println(msg);
|
|
|
|
return;
|
|
|
|
case "--help":
|
|
|
|
System.out.println(msg);
|
|
|
|
return;
|
|
|
|
case "-help":
|
|
|
|
System.out.println(msg);
|
|
|
|
return;
|
|
|
|
case "-h":
|
|
|
|
System.out.println(msg);
|
|
|
|
return;
|
|
|
|
case "\\?":
|
|
|
|
System.out.println(msg);
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-24 09:28:26 -04:00
|
|
|
System.out.println("------------db info---------------------------------------");
|
|
|
|
System.out.println("source db uri: " + scu);
|
|
|
|
System.out.println("source db username: " + scn);
|
|
|
|
System.out.println("destination db uri: " + dcu);
|
|
|
|
System.out.println("destination username: " + dcn);
|
|
|
|
System.out.println("------------source sql------------------------------------");
|
2022-10-20 11:18:05 -04:00
|
|
|
System.out.println(sq);
|
2022-10-24 09:28:26 -04:00
|
|
|
System.out.println("------------destination table-----------------------------");
|
2022-10-20 11:18:05 -04:00
|
|
|
System.out.println(dt);
|
|
|
|
|
|
|
|
//return;
|
|
|
|
|
|
|
|
//force regstration
|
|
|
|
try {
|
|
|
|
Class.forName("com.ibm.as400.access.AS400JDBCDriver");
|
|
|
|
} catch (ClassNotFoundException cnf) {
|
|
|
|
System.out.println("The AS400 JDBC driver did not load");
|
|
|
|
System.exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------establish connections-------------------------------------------------
|
|
|
|
//source database
|
2022-10-24 11:17:29 -04:00
|
|
|
System.out.println("------------open database connections---------------------");
|
2022-10-20 11:18:05 -04:00
|
|
|
try {
|
|
|
|
scon = DriverManager.getConnection(scu, scn, scp);
|
|
|
|
} catch (SQLException e) {
|
|
|
|
System.out.println("issue connecting to source:");
|
|
|
|
e.printStackTrace();
|
|
|
|
System.exit(0);
|
|
|
|
}
|
2022-10-24 11:17:29 -04:00
|
|
|
System.out.println(" ✅ source database");
|
2022-10-20 11:18:05 -04:00
|
|
|
//destination database
|
|
|
|
try {
|
|
|
|
dcon = DriverManager.getConnection(dcu, dcn, dcp);
|
|
|
|
} catch (SQLException e) {
|
2022-10-24 11:17:29 -04:00
|
|
|
System.out.println("issue connecting to destination:");
|
2022-10-20 11:18:05 -04:00
|
|
|
e.printStackTrace();
|
|
|
|
System.exit(0);
|
|
|
|
}
|
2022-10-24 11:17:29 -04:00
|
|
|
System.out.println(" ✅ destination database");
|
2022-10-20 11:18:05 -04:00
|
|
|
//----------------------------------------open resultset------------------------------------------------------------
|
|
|
|
try {
|
|
|
|
stmt = scon.createStatement();
|
2022-10-25 14:43:30 -04:00
|
|
|
stmt.setFetchSize(10000);
|
2022-10-27 07:16:42 -04:00
|
|
|
tsStart = Timestamp.from(Instant.now());
|
|
|
|
System.out.println(tsStart);
|
2022-10-20 11:18:05 -04:00
|
|
|
rs = stmt.executeQuery(sq);
|
|
|
|
//while (rs.next()) {
|
|
|
|
// System.out.println(rs.getString("x"));
|
|
|
|
//}
|
|
|
|
} catch (SQLException e) {
|
|
|
|
System.out.println("issue retrieving rows from source:");
|
|
|
|
e.printStackTrace();
|
|
|
|
System.exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------build meta---------------------------------------------------------------
|
|
|
|
try {
|
|
|
|
cols = rs.getMetaData().getColumnCount();
|
2022-10-24 09:28:26 -04:00
|
|
|
System.out.println("------------source metadata-------------------------------");
|
2022-10-20 11:18:05 -04:00
|
|
|
System.out.println("number of cols: " + cols);
|
|
|
|
getv = new String[cols + 1];
|
|
|
|
dtn = new String[cols + 1];
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
System.exit(0);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
for (int i = 1; i <= cols; i++){
|
|
|
|
dtn[i] = rs.getMetaData().getColumnTypeName(i);
|
2022-10-24 11:17:29 -04:00
|
|
|
System.out.println(" * " + rs.getMetaData().getColumnName(i) + ": " + dtn[i]);
|
2022-10-20 11:18:05 -04:00
|
|
|
}
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
System.exit(0);
|
|
|
|
}
|
2022-10-24 09:28:26 -04:00
|
|
|
System.out.println("------------row count-------------------------------------");
|
2022-10-20 12:12:41 -04:00
|
|
|
//-------------------------------build & execute sql-------------------------------------------------------------
|
2022-10-20 11:18:05 -04:00
|
|
|
try {
|
|
|
|
while (rs.next()) {
|
|
|
|
r++;
|
|
|
|
t++;
|
|
|
|
nr = "";
|
|
|
|
for (int i = 1; i <= cols; i++){
|
2022-10-25 13:24:15 -04:00
|
|
|
switch (dtn[i]){
|
|
|
|
case "VARCHAR":
|
2022-10-25 14:43:30 -04:00
|
|
|
nc = rs.getString(i);
|
2022-10-25 13:24:15 -04:00
|
|
|
if (rs.wasNull() || nc == null) {
|
|
|
|
nc = "NULL";
|
|
|
|
break;
|
|
|
|
}
|
2022-10-27 06:58:18 -04:00
|
|
|
nc = nc.replaceAll("'","''");
|
2022-10-25 13:24:15 -04:00
|
|
|
if (trim) { nc = nc.trim();}
|
|
|
|
nc = "'" + nc + "'";
|
|
|
|
break;
|
2022-10-27 06:58:18 -04:00
|
|
|
case "CHAR":
|
2022-10-25 14:43:30 -04:00
|
|
|
nc = rs.getString(i);
|
2022-10-25 13:24:15 -04:00
|
|
|
if (rs.wasNull() || nc == null) {
|
|
|
|
nc = "NULL";
|
|
|
|
break;
|
|
|
|
}
|
2022-10-27 06:58:18 -04:00
|
|
|
nc = nc.replaceAll("'","''");
|
2022-10-25 13:24:15 -04:00
|
|
|
if (trim) { nc = nc.trim();}
|
|
|
|
nc = "'" + nc + "'";
|
|
|
|
break;
|
2022-10-27 06:58:18 -04:00
|
|
|
case "CLOB":
|
2022-10-25 14:43:30 -04:00
|
|
|
nc = rs.getString(i);
|
2022-10-25 13:24:15 -04:00
|
|
|
if (rs.wasNull() || nc == null) {
|
|
|
|
nc = "NULL";
|
|
|
|
break;
|
|
|
|
}
|
2022-10-27 06:58:18 -04:00
|
|
|
nc = nc.replaceAll("'","''");
|
2022-10-25 13:24:15 -04:00
|
|
|
if (trim) { nc = nc.trim();}
|
|
|
|
nc = "'" + nc + "'";
|
|
|
|
break;
|
|
|
|
case "DATE":
|
2022-10-25 13:49:26 -04:00
|
|
|
nc = rs.getString(i);
|
2022-10-25 13:24:15 -04:00
|
|
|
if (rs.wasNull() || nc == null) {
|
|
|
|
nc = "NULL";
|
|
|
|
break;
|
|
|
|
}
|
2022-10-25 13:49:26 -04:00
|
|
|
nc = "'" + nc + "'";
|
2022-10-25 13:24:15 -04:00
|
|
|
if (nc == "'1/1/0001 12:00:00 AM'") {
|
|
|
|
nc = "NULL";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "TIME":
|
2022-10-25 14:43:30 -04:00
|
|
|
nc = rs.getString(i);
|
2022-10-25 13:24:15 -04:00
|
|
|
if (rs.wasNull() || nc == null) {
|
|
|
|
nc = "NULL";
|
|
|
|
break;
|
|
|
|
}
|
2022-10-27 06:58:18 -04:00
|
|
|
nc.replaceAll("'","''");
|
2022-10-25 13:49:26 -04:00
|
|
|
nc = "'" + nc + "'";
|
2022-10-25 13:24:15 -04:00
|
|
|
break;
|
|
|
|
case "TIMESTAMP":
|
2022-10-25 14:43:30 -04:00
|
|
|
nc = rs.getString(i);
|
2022-10-25 13:24:15 -04:00
|
|
|
if (rs.wasNull() || nc == null) {
|
|
|
|
nc = "NULL";
|
|
|
|
break;
|
|
|
|
}
|
2022-10-27 06:58:18 -04:00
|
|
|
nc.replaceAll("'","''");
|
2022-10-25 13:49:26 -04:00
|
|
|
nc = "'" + nc + "'";
|
2022-10-25 13:24:15 -04:00
|
|
|
break;
|
|
|
|
case "BIGINT":
|
|
|
|
nc = rs.getString(i);
|
|
|
|
if (rs.wasNull() || nc == null) {
|
|
|
|
nc = "NULL";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
nc = rs.getString(i);
|
|
|
|
if (rs.wasNull() || nc == null) {
|
|
|
|
nc = "NULL";
|
|
|
|
break;
|
2022-10-20 22:03:54 -04:00
|
|
|
}
|
2022-10-25 13:24:15 -04:00
|
|
|
break;
|
2022-10-20 11:18:05 -04:00
|
|
|
}
|
|
|
|
if (i != 1){
|
|
|
|
nr = nr + ",";
|
|
|
|
}
|
|
|
|
nr = nr + nc;
|
|
|
|
}
|
2022-10-20 12:12:41 -04:00
|
|
|
//add a comma to the end of the VALUES block to accomodate a new row
|
2022-10-20 11:18:05 -04:00
|
|
|
if (sql!="") {
|
|
|
|
sql = sql + ",";
|
|
|
|
}
|
2022-10-20 12:12:41 -04:00
|
|
|
//add the new row to the VALUES block
|
2022-10-20 11:18:05 -04:00
|
|
|
sql = sql + "(" + nr + ")";
|
|
|
|
if (r == 250){
|
|
|
|
r = 0;
|
|
|
|
sql = "INSERT INTO " + dt + " VALUES " + "\n" + sql;
|
|
|
|
//System.out.println(sql);
|
|
|
|
try {
|
|
|
|
stmtd = dcon.createStatement();
|
|
|
|
stmtd.executeUpdate(sql);
|
2022-10-20 14:19:03 -04:00
|
|
|
System.out.print("\r" + t);
|
2022-10-20 11:18:05 -04:00
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
2022-10-20 12:12:41 -04:00
|
|
|
System.out.println(sql);
|
2022-10-20 11:18:05 -04:00
|
|
|
System.exit(0);
|
|
|
|
}
|
|
|
|
sql = "";
|
|
|
|
}
|
|
|
|
}
|
2022-10-20 12:12:41 -04:00
|
|
|
//if the sql is not empty, execute
|
|
|
|
if (sql != "") {
|
|
|
|
sql = "INSERT INTO " + dt + " VALUES " + "\n" + sql;
|
|
|
|
try {
|
|
|
|
stmtd = dcon.createStatement();
|
|
|
|
stmtd.executeUpdate(sql);
|
2022-10-20 14:19:03 -04:00
|
|
|
System.out.print("\r" + t);
|
2022-10-20 12:12:41 -04:00
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
System.out.println(sql);
|
|
|
|
System.exit(0);
|
|
|
|
}
|
2022-10-20 11:18:05 -04:00
|
|
|
}
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
System.exit(0);
|
|
|
|
}
|
|
|
|
//System.out.println(sql);
|
|
|
|
|
|
|
|
//---------------------------------------close connections--------------------------------------------------------
|
|
|
|
try {
|
|
|
|
scon.close();
|
|
|
|
dcon.close();
|
|
|
|
} catch (SQLException e) {
|
|
|
|
System.out.println("issue closing connections");
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2022-10-24 11:17:29 -04:00
|
|
|
System.out.println(" rows written");
|
2022-10-27 07:16:42 -04:00
|
|
|
tsEnd = Timestamp.from(Instant.now());
|
|
|
|
System.out.println(tsStart);
|
|
|
|
System.out.println(tsEnd);
|
|
|
|
//long time = Duration.between(tsStart, tsEnd).toMillis();
|
|
|
|
//System.out.println("time elapsed: " + time);
|
|
|
|
System.out.println();
|
2022-10-20 11:12:19 -04:00
|
|
|
}
|
|
|
|
}
|