copy code from old project

This commit is contained in:
Paul Trowbridge 2022-10-20 11:18:05 -04:00
parent 4ccaf7b5a8
commit 1cb878bdb2
3 changed files with 288 additions and 10 deletions

View File

@ -22,11 +22,15 @@ dependencies {
// This dependency is used by the application. // This dependency is used by the application.
implementation 'com.google.guava:guava:31.0.1-jre' implementation 'com.google.guava:guava:31.0.1-jre'
//jdbc drivers
implementation 'org.postgresql:postgresql:42.5.0'
implementation 'net.sf.jt400:jt400:11.0'
} }
application { application {
// Define the main class for the application. // Define the main class for the application.
mainClass = 'jrunner.App' mainClass = 'jrunner.jrunner'
} }
tasks.named('test') { tasks.named('test') {

View File

@ -1,14 +1,288 @@
/*
* This Java source file was generated by the Gradle 'init' task.
*/
package jrunner; package jrunner;
import java.sql.*;
import java.util.*;
import java.nio.file.Files;
import java.nio.file.Path ;
import java.nio.file.Paths;
public class jrunner { public class jrunner {
public String getGreeting() { //static final String QUERY = "SELECT * from rlarp.osm LIMIT 100";
return "Hello World!";
}
public static void main(String[] args) { public static void main(String[] args) {
System.out.println(new jrunner().getGreeting());
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;
msg = "Help:";
msg = msg + nl + "jrunner version 0.28";
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
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;
}
}
System.out.println(scu);
System.out.println(scn);
System.out.println(dcu);
System.out.println(dcn);
System.out.println(sq);
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
try {
scon = DriverManager.getConnection(scu, scn, scp);
} catch (SQLException e) {
System.out.println("issue connecting to source:");
e.printStackTrace();
System.exit(0);
}
//destination database
try {
dcon = DriverManager.getConnection(dcu, dcn, dcp);
} catch (SQLException e) {
System.out.println("issue connecting to desctination:");
e.printStackTrace();
System.exit(0);
}
//----------------------------------------open resultset------------------------------------------------------------
try {
stmt = scon.createStatement();
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("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(dtn[i]);
}
} catch (SQLException e) {
e.printStackTrace();
System.exit(0);
}
try {
while (rs.next()) {
r++;
t++;
nr = "";
for (int i = 1; i <= cols; i++){
nc = rs.getString(i);
Boolean dnull = false;
if (rs.wasNull()) {
nc = "NULL";
dnull = true;
} else {
switch (dtn[i]){
case "VARCHAR":
nc = rs.getString(i).replace("'","''");
if (trim) { nc = nc.trim();}
nc = "'" + nc + "'";
break;
case "CLOB":
nc = rs.getString(i).replace("'","''");
if (trim) { nc = nc.trim();}
nc = "'" + nc + "'";
break;
case "CHAR":
nc = rs.getString(i).replace("'","''");
if (trim) { nc = nc.trim();}
nc = "'" + nc + "'";
break;
case "DATE":
nc = "'" + rs.getString(i) + "'";
if (nc == "'1/1/0001 12:00:00 AM'") {
nc = "NULL";
}
break;
case "TIME":
nc = "'" + rs.getString(i).replace("'","''") + "'";
break;
case "BIGINT":
nc = rs.getString(i);
default:
if (rs.getString(i) != "") {
nc = rs.getString(i);
}
else {
nc = "NULL";
}
break;
}
}
if (i != 1){
nr = nr + ",";
}
nr = nr + nc;
}
//System.out.println(nr);
if (sql!="") {
sql = sql + ",";
}
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.println(t);
} catch (SQLException e) {
e.printStackTrace();
System.exit(0);
}
sql = "";
}
}
sql = "INSERT INTO " + dt + " VALUES " + "\n" + sql;
//System.out.println(sql);
try {
stmtd = dcon.createStatement();
stmtd.executeUpdate(sql);
System.out.println(t);
} catch (SQLException e) {
e.printStackTrace();
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();
}
} }
} }

View File

@ -9,6 +9,6 @@ import static org.junit.jupiter.api.Assertions.*;
class AppTest { class AppTest {
@Test void appHasAGreeting() { @Test void appHasAGreeting() {
jrunner classUnderTest = new jrunner(); jrunner classUnderTest = new jrunner();
assertNotNull(classUnderTest.getGreeting(), "app should have a greeting"); //assertNotNull(classUnderTest.getGreeting(), "app should have a greeting");
} }
} }