1
1
package com .nordstrom .common .jdbc ;
2
2
3
3
import java .sql .Connection ;
4
+ import java .sql .Driver ;
4
5
import java .sql .DriverManager ;
5
6
import java .sql .ResultSet ;
6
7
import java .sql .SQLException ;
7
8
import java .util .Arrays ;
9
+ import java .util .Iterator ;
10
+ import java .util .ServiceLoader ;
8
11
9
12
import com .nordstrom .common .base .UncheckedThrow ;
10
13
11
14
import java .sql .PreparedStatement ;
12
15
13
16
/**
14
- * This utility class provides facilities that enable you to define collections of Oracle database queries and
17
+ * This utility class provides facilities that enable you to define collections of database queries and
15
18
* execute them easily. Query collections are defined as Java enumeration that implement the {@link QueryAPI}
16
19
* interface: <ul>
17
20
* <li>{@link QueryAPI#getQueryStr() getQueryStr} - Get the query string for this constant. This is the actual query
18
21
* that's sent to the database.</li>
19
22
* <li>{@link QueryAPI#getArgNames() getArgNames} - Get the names of the arguments for this query. This provides
20
23
* diagnostic information if the incorrect number of arguments is specified by the client.</li>
21
24
* <li>{@link QueryAPI#getArgCount() getArgCount} - Get the number of arguments required by this query. This enables
22
- * {@link #executeOracleQuery (Class, QueryAPI, Object[])} to verify that the correct number of arguments has been
25
+ * {@link #executeQuery (Class, QueryAPI, Object[])} to verify that the correct number of arguments has been
23
26
* specified by the client.</li>
24
27
* <li>{@link QueryAPI#getConnection() getConnection} - Get the connection string associated with this query. This
25
28
* eliminates the need for the client to provide this information.</li>
26
29
* <li>{@link QueryAPI#getEnum() getEnum} - Get the enumeration to which this query belongs. This enables {@link
27
- * #executeOracleQuery (Class, QueryAPI, Object[])} to retrieve the name of the query's enumerated constant for
30
+ * #executeQuery (Class, QueryAPI, Object[])} to retrieve the name of the query's enumerated constant for
28
31
* diagnostic messages.</li>
29
32
* </ul>
30
33
*
@@ -173,10 +176,9 @@ private DatabaseUtils() {
173
176
}
174
177
175
178
static {
176
- try {
177
- Class .forName ("oracle.jdbc.driver.OracleDriver" );
178
- } catch (ClassNotFoundException e ) {
179
- throw new RuntimeException ("Unable to load the Oracle JDBC driver" , e );
179
+ Iterator <Driver > iterator = ServiceLoader .load (Driver .class ).iterator ();
180
+ while (iterator .hasNext ()) {
181
+ iterator .next ();
180
182
}
181
183
}
182
184
@@ -188,7 +190,7 @@ private DatabaseUtils() {
188
190
* @return count of records updated
189
191
*/
190
192
public static int update (QueryAPI query , Object ... queryArgs ) {
191
- Integer result = (Integer ) executeOracleQuery (null , query , queryArgs );
193
+ Integer result = (Integer ) executeQuery (null , query , queryArgs );
192
194
return (result != null ) ? result .intValue () : -1 ;
193
195
}
194
196
@@ -200,7 +202,7 @@ public static int update(QueryAPI query, Object... queryArgs) {
200
202
* @return row 1 / column 1 as integer; -1 if no rows were returned
201
203
*/
202
204
public static int getInt (QueryAPI query , Object ... queryArgs ) {
203
- Integer result = (Integer ) executeOracleQuery (Integer .class , query , queryArgs );
205
+ Integer result = (Integer ) executeQuery (Integer .class , query , queryArgs );
204
206
return (result != null ) ? result .intValue () : -1 ;
205
207
}
206
208
@@ -212,7 +214,7 @@ public static int getInt(QueryAPI query, Object... queryArgs) {
212
214
* @return row 1 / column 1 as string; 'null' if no rows were returned
213
215
*/
214
216
public static String getString (QueryAPI query , Object ... queryArgs ) {
215
- return (String ) executeOracleQuery (String .class , query , queryArgs );
217
+ return (String ) executeQuery (String .class , query , queryArgs );
216
218
}
217
219
218
220
/**
@@ -223,7 +225,7 @@ public static String getString(QueryAPI query, Object... queryArgs) {
223
225
* @return {@link ResultPackage} object
224
226
*/
225
227
public static ResultPackage getResultPackage (QueryAPI query , Object ... queryArgs ) {
226
- return (ResultPackage ) executeOracleQuery (ResultPackage .class , query , queryArgs );
228
+ return (ResultPackage ) executeQuery (ResultPackage .class , query , queryArgs );
227
229
}
228
230
229
231
/**
@@ -243,7 +245,7 @@ public static ResultPackage getResultPackage(QueryAPI query, Object... queryArgs
243
245
* <b>NOTE</b>: If you specify {@link ResultPackage} as the result type, it's recommended that you close this object
244
246
* when you're done with it to free up database and JDBC resources that were allocated for it.
245
247
*/
246
- private static Object executeOracleQuery (Class <?> resultType , QueryAPI query , Object ... queryArgs ) {
248
+ private static Object executeQuery (Class <?> resultType , QueryAPI query , Object ... queryArgs ) {
247
249
int expectCount = query .getArgCount ();
248
250
int actualCount = queryArgs .length ;
249
251
@@ -260,7 +262,7 @@ private static Object executeOracleQuery(Class<?> resultType, QueryAPI query, Ob
260
262
throw new IllegalArgumentException (message );
261
263
}
262
264
263
- return executeOracleQuery (resultType , query .getConnection (), query .getQueryStr (), queryArgs );
265
+ return executeQuery (resultType , query .getConnection (), query .getQueryStr (), queryArgs );
264
266
}
265
267
266
268
/**
@@ -274,14 +276,14 @@ private static Object executeOracleQuery(Class<?> resultType, QueryAPI query, Ob
274
276
* <li>For other types, {@link ResultSet#getObject(int, Class)} to return row 1 / column 1 as that type</li></ul>
275
277
*
276
278
* @param resultType desired result type (see TYPES above)
277
- * @param connectionStr Oracle database connection string
279
+ * @param connectionStr database connection string
278
280
* @param queryStr a SQL statement that may contain one or more '?' IN parameter placeholders
279
281
* @param param an array of objects containing the input parameter values
280
282
* @return for update operations, the number of rows affected; for query operations, an object of the indicated type<br>
281
283
* <b>NOTE</b>: If you specify {@link ResultPackage} as the result type, it's recommended that you close this object
282
284
* when you're done with it to free up database and JDBC resources that were allocated for it.
283
285
*/
284
- public static Object executeOracleQuery (Class <?> resultType , String connectionStr , String queryStr , Object ... param ) {
286
+ public static Object executeQuery (Class <?> resultType , String connectionStr , String queryStr , Object ... param ) {
285
287
Object result = null ;
286
288
boolean failed = false ;
287
289
@@ -290,7 +292,7 @@ public static Object executeOracleQuery(Class<?> resultType, String connectionSt
290
292
ResultSet resultSet = null ;
291
293
292
294
try {
293
- connection = getOracleConnection (connectionStr );
295
+ connection = getConnection (connectionStr );
294
296
statement = connection .prepareStatement (queryStr ); //NOSONAR
295
297
296
298
for (int i = 0 ; i < param .length ; i ++) {
@@ -321,18 +323,24 @@ public static Object executeOracleQuery(Class<?> resultType, String connectionSt
321
323
if (resultSet != null ) {
322
324
try {
323
325
resultSet .close ();
324
- } catch (SQLException e ) { }
326
+ } catch (SQLException e ) {
327
+ // Suppress shutdown failures
328
+ }
325
329
}
326
330
if (statement != null ) {
327
331
try {
328
332
statement .close ();
329
- } catch (SQLException e ) { }
333
+ } catch (SQLException e ) {
334
+ // Suppress shutdown failures
335
+ }
330
336
}
331
337
if (connection != null ) {
332
338
try {
333
339
connection .commit ();
334
340
connection .close ();
335
- } catch (SQLException e ) { }
341
+ } catch (SQLException e ) {
342
+ // Suppress shutdown failures
343
+ }
336
344
}
337
345
}
338
346
}
@@ -341,43 +349,19 @@ public static Object executeOracleQuery(Class<?> resultType, String connectionSt
341
349
}
342
350
343
351
/**
344
- * Get a connection to the Oracle database associated with the specified connection string.
352
+ * Get a connection to the database associated with the specified connection string.
345
353
*
346
- * @param connectionString Oracle database connection string
347
- * @return Oracle database connection object
354
+ * @param connectionString database connection string
355
+ * @return database connection object
348
356
*/
349
- private static Connection getOracleConnection (String connectionString ) {
357
+ private static Connection getConnection (String connectionString ) {
350
358
try {
351
- QueryCreds creds = new QueryCreds (connectionString );
352
- return DriverManager .getConnection (creds .url , creds .userId , creds .password );
359
+ return DriverManager .getConnection (connectionString );
353
360
} catch (SQLException e ) {
354
361
throw UncheckedThrow .throwUnchecked (e );
355
362
}
356
363
}
357
364
358
- /**
359
- * This class encapsulated database query credentials.
360
- */
361
- private static class QueryCreds {
362
-
363
- private String url ;
364
- private String userId ;
365
- private String password ;
366
-
367
- /**
368
- * Constructor for database query credentials.
369
- *
370
- * @param connectionString database connection string
371
- */
372
- private QueryCreds (String connectionString ) {
373
- String [] bits = connectionString .split (";" );
374
-
375
- url = bits [0 ].trim ();
376
- userId = bits [1 ].split ("=" )[1 ].trim ();
377
- password = bits [2 ].split ("=" )[1 ].trim ();
378
- }
379
- }
380
-
381
365
/**
382
366
* This interface defines the API supported by database query collections
383
367
*/
@@ -416,7 +400,7 @@ public interface QueryAPI {
416
400
*
417
401
* @return query object enumerated constant
418
402
*/
419
- Enum <?> getEnum ();
403
+ Enum <? extends QueryAPI > getEnum (); //NOSONAR
420
404
}
421
405
422
406
/**
0 commit comments