Skip to content

Commit d96599d

Browse files
committed
Rel 1.7: ReadJDBC converted to use reflection - 1st stage (hard-wired jar file name)
1 parent 404afa5 commit d96599d

File tree

11 files changed

+110
-49
lines changed

11 files changed

+110
-49
lines changed

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ apply plugin: 'java-library'
3333

3434
}
3535

36-
version = '4.1.6'
36+
version = '4.1.7'
3737

3838
group = 'com.jpaulmorrison'
3939

@@ -63,7 +63,7 @@ repositories {
6363

6464
dependencies {
6565
implementation ('com.google.code.gson:gson:2.8.6')
66-
implementation ('mysql:mysql-connector-java:8.0.21')
66+
//implementation ('mysql:mysql-connector-java:8.0.21')
6767

6868
}
6969

@@ -137,7 +137,7 @@ description = "Create default maven directory structure"
137137

138138
groupId = 'com.jpaulmorrison'
139139
artifactId = 'javafbp'
140-
version = '4.1.6'
140+
version = '4.1.7'
141141

142142
from components.java
143143

750 KB
Binary file not shown.
660 KB
Binary file not shown.
669 KB
Binary file not shown.
251 KB
Binary file not shown.

build/libs/javafbp-4.1.7.jar

498 KB
Binary file not shown.

build/scripts/javafbp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ case "`uname`" in
8080
;;
8181
esac
8282

83-
CLASSPATH=$APP_HOME/lib/javafbp-4.1.6.jar:$APP_HOME/lib/gson-2.8.6.jar:$APP_HOME/lib/mysql-connector-java-8.0.21.jar:$APP_HOME/lib/protobuf-java-3.11.4.jar
83+
CLASSPATH=$APP_HOME/lib/javafbp-4.1.7.jar:$APP_HOME/lib/gson-2.8.6.jar
8484

8585

8686
# Determine the Java command to use to start the JVM.

build/scripts/javafbp.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ set CMD_LINE_ARGS=%*
8282
:execute
8383
@rem Setup the command line
8484

85-
set CLASSPATH=%APP_HOME%\lib\javafbp-4.1.6.jar;%APP_HOME%\lib\gson-2.8.6.jar;%APP_HOME%\lib\mysql-connector-java-8.0.21.jar;%APP_HOME%\lib\protobuf-java-3.11.4.jar
85+
set CLASSPATH=%APP_HOME%\lib\javafbp-4.1.7.jar;%APP_HOME%\lib\gson-2.8.6.jar
8686

8787

8888
@rem Execute javafbp

src/main/java/com/jpaulmorrison/fbp/core/components/jdbc/ReadJDBC.java

Lines changed: 101 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
11
package com.jpaulmorrison.fbp.core.components.jdbc;
22

33

4+
import java.io.BufferedReader;
5+
import java.io.BufferedWriter;
6+
import java.io.File;
7+
import java.io.FileWriter;
8+
import java.io.InputStreamReader;
49
import java.lang.reflect.Constructor;
510
import java.lang.reflect.Field;
611
import java.lang.reflect.InvocationTargetException;
712
import java.lang.reflect.Method;
13+
import java.net.URL;
14+
import java.net.URLClassLoader;
15+
import java.util.Arrays;
16+
import java.util.Collections;
17+
import java.util.Comparator;
18+
19+
/*
20+
import java.sql.Connection;
21+
import java.sql.DriverManager;
22+
import java.sql.ResultSet;
23+
import java.sql.ResultSetMetaData;
24+
import java.sql.SQLException;
25+
import java.sql.Statement;
26+
*/
827

9-
import java.sql.*;
1028
import java.util.HashMap;
11-
29+
1230
import com.google.gson.Gson;
1331
import com.jpaulmorrison.fbp.core.engine.Component; // Using 'Connection', 'Statement' and 'ResultSet' classes in java.sql package
1432
import com.jpaulmorrison.fbp.core.engine.ComponentDescription;
@@ -72,7 +90,7 @@ protected void execute() throws Exception {
7290

7391

7492
//Class<?> curClass = cls;
75-
Class<?> curClass = Class.forName(objClass);
93+
Class<?> dataClass = Class.forName(objClass);
7694

7795
String[] iipContents = dbTable.split("!", 2);
7896

@@ -100,64 +118,107 @@ protected void execute() throws Exception {
100118
}
101119

102120

103-
try (
121+
String userName = System.getProperty("user.name");
122+
File f = new File("C:\\Users\\" + userName + "\\.m2\\repository\\mysql\\mysql-connector-java\\8.0.21\\mysql-connector-java-8.0.21.jar");
123+
124+
URL[] urls = {f.toURI().toURL()};
125+
URLClassLoader ucl = new URLClassLoader(urls);
126+
Class<?> conn_cls = ucl.loadClass("java.sql.Connection");
127+
Class<?> dm_cls = ucl.loadClass("java.sql.DriverManager");
128+
Class<?> stmt_cls = ucl.loadClass("java.sql.Statement");
129+
Class<?> rs_cls = ucl.loadClass("java.sql.ResultSet");
130+
Class<?> rsmd_cls = ucl.loadClass("java.sql.ResultSetMetaData");
131+
//Class<?> se_cls = ucl.loadClass("java.sql.SQLException");
132+
133+
Class<?>[] carr = {String.class, String.class, String.class};
134+
//Object conn = conn_cls.newInstance();
135+
136+
137+
138+
try
139+
{
140+
Method gc = dm_cls.getMethod("getConnection", carr);
141+
Object conn = gc.invoke(null, iipContents[0] +
142+
"?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC",
143+
user, pswd); // o1 is a Connection
144+
145+
Method cs = conn_cls.getMethod("createStatement");
146+
Object stmt = cs.invoke(conn); //o2 is a Statement
147+
//@SuppressWarnings("resource")
148+
//Statement stmt = (Statement) o2;
104149

105150
// Step 1: Allocate a database 'Connection' object
106-
Connection conn = DriverManager.getConnection(
151+
//Connection conn = DriverManager.getConnection(
107152
// "jdbc:mysql://localhost:3306/ebookshop?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC",
108153
// "root", pswd); // For MySQL only
109154

110-
iipContents[0] + "?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC", user, pswd);
155+
// iipContents[0] + "?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC", user, pswd);
111156

112-
Statement stmt = conn.createStatement();
157+
//Statement stmt = conn.createStatement();
113158

114-
) {
159+
// )
160+
115161

116162
String strSelect = "select * from " + iipContents[1];
117-
System.out.println("The SQL statement is: \"" + strSelect + "\"\n"); // Echo
118-
// For
119-
// debugging
163+
System.out.println("The SQL statement is: \"" + strSelect + "\"\n");
164+
165+
Class<?>[] carr2 = {String.class};
120166

121-
ResultSet rset = stmt.executeQuery(strSelect);
167+
Method eq = stmt_cls.getMethod("executeQuery", carr2);
168+
169+
Object rset2 = eq.invoke(stmt, strSelect); // o3 is ResultSet
170+
171+
//ResultSet rset = stmt.executeQuery(strSelect);
122172

123-
ResultSetMetaData rsmd;
173+
Object rsmd2;
124174
int numberOfColumns = 0;
125175
HashMap<String, String> hmColumns = new HashMap<String, String>();
126176
try {
127-
rsmd = rset.getMetaData();
128-
numberOfColumns = rsmd.getColumnCount();
177+
Method gmd = rs_cls.getMethod("getMetaData");
178+
rsmd2 = gmd.invoke(rset2);
179+
//rsmd = rset.getMetaData();
180+
Method gcc = rsmd_cls.getMethod("getColumnCount");
181+
numberOfColumns = (int) gcc.invoke(rsmd2);
182+
//numberOfColumns = rsmd.getColumnCount();
183+
Method gcn = rsmd_cls.getMethod("getColumnName", int.class);
184+
Method gct = rsmd_cls.getMethod("getColumnTypeName", int.class);
185+
129186
for (int i = 1; i <= numberOfColumns; i++) {
130-
hmColumns.put(rsmd.getColumnName(i), rsmd.getColumnTypeName(i));
187+
String colName = (String) gcn.invoke(rsmd2, i);
188+
String colTypeName = (String) gct.invoke(rsmd2, i);
189+
hmColumns.put(colName, colTypeName);
131190
}
132-
} catch (SQLException e) {
191+
} catch (Exception e) {
133192
// TODO Auto-generated catch block
134193
e.printStackTrace();
135194
}
136195

137196
// number of columns should match number of fields in curClass - so
138197
// let's test...
139198

140-
int n = curClass.getFields().length;
141-
if (n != numberOfColumns) {
199+
int n2= dataClass.getFields().length;
200+
if (n2 != numberOfColumns) {
142201
System.out.println(
143-
"Number of class fields - " + n + " does not match number of columns - " + numberOfColumns);
202+
"Number of class fields - " + n2 + " does not match number of columns - " + numberOfColumns);
144203
return;
145204
}
146205

147206
HashMap<String, Class<?>> hmFields = new HashMap<String, Class<?>>();
148207

149-
for (Field fd: curClass.getFields()) {
208+
for (Field fd: dataClass.getFields()) {
150209
hmFields.put(fd.getName(), fd.getType());
151210
}
152211

153212
// iterate through rows
154213

155214
int rowCount = 0;
156-
while (rset.next()) { // Move the cursor to the next row, return
215+
Method next = rs_cls.getMethod("next");
216+
boolean nxt = (boolean) next.invoke(rset2);
217+
while (nxt) { // Move the cursor to the next row, return
157218
// false if no more row
158219
Object obj = null;
159220
try {
160-
obj = curClass.newInstance();
221+
obj = dataClass.newInstance();
161222
} catch (InstantiationException e) {
162223
// handle 1
163224
} catch (IllegalAccessException e) {
@@ -188,12 +249,12 @@ protected void execute() throws Exception {
188249

189250
if (objFType.startsWith("class ")) {
190251
int k = objFType.lastIndexOf(".");
191-
String s = objFType.substring(k + 1);
192-
if (s.equals("BigDecimal"))
193-
s = "Decimal";
194-
getMethodName = "get" + s;
252+
String s2 = objFType.substring(k + 1);
253+
if (s2.equals("BigDecimal"))
254+
s2 = "Decimal";
255+
getMethodName = "get" + s2;
195256
try {
196-
ResultSet.class.getMethod(getMethodName, cArg);
257+
rs_cls.getMethod(getMethodName, cArg);
197258
} catch (NoSuchMethodException e)
198259
{
199260
getMethodName = "getString";
@@ -212,29 +273,29 @@ protected void execute() throws Exception {
212273
Object o = null;
213274

214275
try {
215-
meth = ResultSet.class.getMethod(getMethodName, cArg);
276+
meth = rs_cls.getMethod(getMethodName, cArg);
216277
} catch (NoSuchMethodException e) {
217278
System.out.println("Missing method - trying method: '" + getMethodName + "()' on '" + colName
218279
+ "' " + hmColumns.get(colName) + " (target: '" + objField + "' "
219280
+ hmFields.get(objField) + ")");
220281
}
221282

222283
try {
223-
o = meth.invoke(rset, colName);
284+
o = meth.invoke(rset2, colName);
224285
} catch (InvocationTargetException e) {
225286
e.printStackTrace();
226287
}
227288

228-
Field fd = curClass.getField(objField);
289+
Field fd = dataClass.getField(objField);
229290

230291
try {
231292
fd.set(obj, o);
232293
} catch (Exception e) {
233-
String s = hmFields.get(objField).toString();
294+
String s2 = hmFields.get(objField).toString();
234295
//System.out.println("class is " + s);
235-
s = s.substring(6);
296+
s2 = s2.substring(6);
236297
//Object oo = Class.forName(s).newInstance();
237-
Constructor<?> cons = Class.forName(s).getConstructor(cArg);
298+
Constructor<?> cons = Class.forName(s2).getConstructor(cArg);
238299
Object oo = cons.newInstance((String) o);
239300
try {
240301
fd.set(obj, oo);
@@ -246,22 +307,22 @@ protected void execute() throws Exception {
246307
}
247308
outPort.send(create(obj));
248309
++rowCount;
310+
nxt = (boolean) next.invoke(rset2);
249311
}
250312

251313
System.out.println("Total number of records = " + rowCount);
252314
// outPort.send(create("Total number of records = " + rowCount));
253315

254-
} catch (SQLException ex) {
255-
//System.out.println("SQL Exception");
256-
ex.printStackTrace();
257316
} catch (ClassNotFoundException ex) {
258317
//System.out.println("Class Not Found Exception");
259318
ex.printStackTrace();
260-
}
261-
catch (InvocationTargetException ex) {
319+
} catch (InvocationTargetException ex) {
262320
//System.out.println("Class Not Found Exception");
263321
ex.printStackTrace();
264-
}
322+
} catch (Exception ex) {
323+
//System.out.println("SQL Exception");
324+
ex.printStackTrace();
325+
}
265326

266327
// Step 5: Close conn and stmt - Done automatically by
267328
// try-with-resources (JDK 7)

src/main/java/com/jpaulmorrison/fbp/core/engine/VersionAndTimestamp.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public final class VersionAndTimestamp {
2525
*
2626
*/
2727

28-
private static String version = "JavaFBP - version 4.1.6";
28+
private static String version = "JavaFBP - version 4.1.7";
2929

30-
private static String date = "Sept. 7, 2020";
30+
private static String date = "Oct. 19, 2020";
3131

3232
static String getVersion() {
3333
return version;

0 commit comments

Comments
 (0)