package jrunner; import java.sql.*; import java.util.*; import java.nio.file.Files; import java.nio.file.Path ; import java.nio.file.Paths; import java.time.*; public class jrunner { //static final String QUERY = "SELECT * from rlarp.osm LIMIT 100"; public static void main(String[] args) { String scu = ""; String scn = ""; String scp = ""; String dcu = ""; String dcn = ""; String dcp = ""; String sq = ""; String dt = ""; Boolean trim = true; Boolean clear = 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; Timestamp tsStart = null; Timestamp tsEnd = null; msg = "jrunner version 0.41"; 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 + "-c clear target table"; 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 //import java.time.*; 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 "-c": clear = 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; } } 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------------------------------------"); System.out.println(sq); System.out.println("------------destination table-----------------------------"); 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 System.out.println("------------open database connections---------------------"); try { scon = DriverManager.getConnection(scu, scn, scp); } catch (SQLException e) { System.out.println("issue connecting to source:"); e.printStackTrace(); System.exit(0); } System.out.println(" ✅ source database"); //destination database try { dcon = DriverManager.getConnection(dcu, dcn, dcp); } catch (SQLException e) { System.out.println("issue connecting to destination:"); e.printStackTrace(); System.exit(0); } System.out.println(" ✅ destination database"); //----------------------------------------open resultset------------------------------------------------------------ try { stmt = scon.createStatement(); stmt.setFetchSize(10000); tsStart = Timestamp.from(Instant.now()); System.out.println(tsStart); 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(); System.out.println("------------source metadata-------------------------------"); 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); System.out.println(" * " + rs.getMetaData().getColumnName(i) + ": " + dtn[i]); } } catch (SQLException e) { e.printStackTrace(); System.exit(0); } //-------------------------clear the target table if requeted---------------------------------------------------- if (clear) { System.out.println("------------clear target table----------------------------"); sql = "DELETE FROM " + dt; try { stmtd = dcon.createStatement(); System.out.println(" " + sql); stmtd.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); System.out.println(sql); System.exit(0); } } System.out.println("------------row count-------------------------------------"); //-------------------------------build & execute sql------------------------------------------------------------- try { sql = ""; while (rs.next()) { r++; t++; nr = ""; for (int i = 1; i <= cols; i++){ switch (dtn[i].toUpperCase()){ case "VARCHAR": nc = rs.getString(i); if (rs.wasNull() || nc == null) { nc = "NULL"; break; } nc = nc.replaceAll("'","''"); if (trim) { nc = nc.trim();} nc = "'" + nc + "'"; break; case "TEXT": nc = rs.getString(i); if (rs.wasNull() || nc == null) { nc = "NULL"; break; } nc = nc.replaceAll("'","''"); if (trim) { nc = nc.trim();} nc = "'" + nc + "'"; break; case "CHAR": nc = rs.getString(i); if (rs.wasNull() || nc == null) { nc = "NULL"; break; } nc = nc.replaceAll("'","''"); if (trim) { nc = nc.trim();} nc = "'" + nc + "'"; break; case "CLOB": nc = rs.getString(i); if (rs.wasNull() || nc == null) { nc = "NULL"; break; } nc = nc.replaceAll("'","''"); if (trim) { nc = nc.trim();} nc = "'" + nc + "'"; break; case "DATE": nc = rs.getString(i); if (rs.wasNull() || nc == null) { nc = "NULL"; break; } nc = "'" + nc + "'"; if (nc == "'1/1/0001 12:00:00 AM'") { nc = "NULL"; } break; case "TIME": nc = rs.getString(i); if (rs.wasNull() || nc == null) { nc = "NULL"; break; } nc.replaceAll("'","''"); nc = "'" + nc + "'"; break; case "TIMESTAMP": nc = rs.getString(i); if (rs.wasNull() || nc == null) { nc = "NULL"; break; } nc.replaceAll("'","''"); nc = "'" + nc + "'"; 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; } break; } if (i != 1){ nr = nr + ","; } nr = nr + nc; } //add a comma to the end of the VALUES block to accomodate a new row if (sql!="") { sql = sql + ","; } //add the new row to the VALUES block 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); System.out.print("\r" + t); } catch (SQLException e) { e.printStackTrace(); System.out.println(sql); System.exit(0); } sql = ""; } } //if the sql is not empty, execute if (sql != "") { sql = "INSERT INTO " + dt + " VALUES " + "\n" + sql; try { stmtd = dcon.createStatement(); stmtd.executeUpdate(sql); System.out.print("\r" + t); } catch (SQLException e) { e.printStackTrace(); System.out.println(sql); System.exit(0); } } } 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(); } System.out.println(" rows written"); 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(); } }