11package 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 ;
49import java .lang .reflect .Constructor ;
510import java .lang .reflect .Field ;
611import java .lang .reflect .InvocationTargetException ;
712import 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 .*;
1028import java .util .HashMap ;
11-
29+
1230import com .google .gson .Gson ;
1331import com .jpaulmorrison .fbp .core .engine .Component ; // Using 'Connection', 'Statement' and 'ResultSet' classes in java.sql package
1432import 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)
0 commit comments