11package com .nordstrom .common .jdbc ;
22
33import java .sql .Connection ;
4+ import java .sql .Driver ;
45import java .sql .DriverManager ;
56import java .sql .ResultSet ;
67import java .sql .SQLException ;
78import java .util .Arrays ;
9+ import java .util .Iterator ;
10+ import java .util .ServiceLoader ;
811
912import com .nordstrom .common .base .UncheckedThrow ;
1013
1114import java .sql .PreparedStatement ;
1215
1316/**
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
1518 * execute them easily. Query collections are defined as Java enumeration that implement the {@link QueryAPI}
1619 * interface: <ul>
1720 * <li>{@link QueryAPI#getQueryStr() getQueryStr} - Get the query string for this constant. This is the actual query
1821 * that's sent to the database.</li>
1922 * <li>{@link QueryAPI#getArgNames() getArgNames} - Get the names of the arguments for this query. This provides
2023 * diagnostic information if the incorrect number of arguments is specified by the client.</li>
2124 * <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
2326 * specified by the client.</li>
2427 * <li>{@link QueryAPI#getConnection() getConnection} - Get the connection string associated with this query. This
2528 * eliminates the need for the client to provide this information.</li>
2629 * <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
2831 * diagnostic messages.</li>
2932 * </ul>
3033 *
@@ -173,10 +176,9 @@ private DatabaseUtils() {
173176 }
174177
175178 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 ();
180182 }
181183 }
182184
@@ -188,7 +190,7 @@ private DatabaseUtils() {
188190 * @return count of records updated
189191 */
190192 public static int update (QueryAPI query , Object ... queryArgs ) {
191- Integer result = (Integer ) executeOracleQuery (null , query , queryArgs );
193+ Integer result = (Integer ) executeQuery (null , query , queryArgs );
192194 return (result != null ) ? result .intValue () : -1 ;
193195 }
194196
@@ -200,7 +202,7 @@ public static int update(QueryAPI query, Object... queryArgs) {
200202 * @return row 1 / column 1 as integer; -1 if no rows were returned
201203 */
202204 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 );
204206 return (result != null ) ? result .intValue () : -1 ;
205207 }
206208
@@ -212,7 +214,7 @@ public static int getInt(QueryAPI query, Object... queryArgs) {
212214 * @return row 1 / column 1 as string; 'null' if no rows were returned
213215 */
214216 public static String getString (QueryAPI query , Object ... queryArgs ) {
215- return (String ) executeOracleQuery (String .class , query , queryArgs );
217+ return (String ) executeQuery (String .class , query , queryArgs );
216218 }
217219
218220 /**
@@ -223,7 +225,7 @@ public static String getString(QueryAPI query, Object... queryArgs) {
223225 * @return {@link ResultPackage} object
224226 */
225227 public static ResultPackage getResultPackage (QueryAPI query , Object ... queryArgs ) {
226- return (ResultPackage ) executeOracleQuery (ResultPackage .class , query , queryArgs );
228+ return (ResultPackage ) executeQuery (ResultPackage .class , query , queryArgs );
227229 }
228230
229231 /**
@@ -243,7 +245,7 @@ public static ResultPackage getResultPackage(QueryAPI query, Object... queryArgs
243245 * <b>NOTE</b>: If you specify {@link ResultPackage} as the result type, it's recommended that you close this object
244246 * when you're done with it to free up database and JDBC resources that were allocated for it.
245247 */
246- private static Object executeOracleQuery (Class <?> resultType , QueryAPI query , Object ... queryArgs ) {
248+ private static Object executeQuery (Class <?> resultType , QueryAPI query , Object ... queryArgs ) {
247249 int expectCount = query .getArgCount ();
248250 int actualCount = queryArgs .length ;
249251
@@ -260,7 +262,7 @@ private static Object executeOracleQuery(Class<?> resultType, QueryAPI query, Ob
260262 throw new IllegalArgumentException (message );
261263 }
262264
263- return executeOracleQuery (resultType , query .getConnection (), query .getQueryStr (), queryArgs );
265+ return executeQuery (resultType , query .getConnection (), query .getQueryStr (), queryArgs );
264266 }
265267
266268 /**
@@ -274,14 +276,14 @@ private static Object executeOracleQuery(Class<?> resultType, QueryAPI query, Ob
274276 * <li>For other types, {@link ResultSet#getObject(int, Class)} to return row 1 / column 1 as that type</li></ul>
275277 *
276278 * @param resultType desired result type (see TYPES above)
277- * @param connectionStr Oracle database connection string
279+ * @param connectionStr database connection string
278280 * @param queryStr a SQL statement that may contain one or more '?' IN parameter placeholders
279281 * @param param an array of objects containing the input parameter values
280282 * @return for update operations, the number of rows affected; for query operations, an object of the indicated type<br>
281283 * <b>NOTE</b>: If you specify {@link ResultPackage} as the result type, it's recommended that you close this object
282284 * when you're done with it to free up database and JDBC resources that were allocated for it.
283285 */
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 ) {
285287 Object result = null ;
286288 boolean failed = false ;
287289
@@ -290,7 +292,7 @@ public static Object executeOracleQuery(Class<?> resultType, String connectionSt
290292 ResultSet resultSet = null ;
291293
292294 try {
293- connection = getOracleConnection (connectionStr );
295+ connection = getConnection (connectionStr );
294296 statement = connection .prepareStatement (queryStr ); //NOSONAR
295297
296298 for (int i = 0 ; i < param .length ; i ++) {
@@ -321,18 +323,24 @@ public static Object executeOracleQuery(Class<?> resultType, String connectionSt
321323 if (resultSet != null ) {
322324 try {
323325 resultSet .close ();
324- } catch (SQLException e ) { }
326+ } catch (SQLException e ) {
327+ // Suppress shutdown failures
328+ }
325329 }
326330 if (statement != null ) {
327331 try {
328332 statement .close ();
329- } catch (SQLException e ) { }
333+ } catch (SQLException e ) {
334+ // Suppress shutdown failures
335+ }
330336 }
331337 if (connection != null ) {
332338 try {
333339 connection .commit ();
334340 connection .close ();
335- } catch (SQLException e ) { }
341+ } catch (SQLException e ) {
342+ // Suppress shutdown failures
343+ }
336344 }
337345 }
338346 }
@@ -341,43 +349,19 @@ public static Object executeOracleQuery(Class<?> resultType, String connectionSt
341349 }
342350
343351 /**
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.
345353 *
346- * @param connectionString Oracle database connection string
347- * @return Oracle database connection object
354+ * @param connectionString database connection string
355+ * @return database connection object
348356 */
349- private static Connection getOracleConnection (String connectionString ) {
357+ private static Connection getConnection (String connectionString ) {
350358 try {
351- QueryCreds creds = new QueryCreds (connectionString );
352- return DriverManager .getConnection (creds .url , creds .userId , creds .password );
359+ return DriverManager .getConnection (connectionString );
353360 } catch (SQLException e ) {
354361 throw UncheckedThrow .throwUnchecked (e );
355362 }
356363 }
357364
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-
381365 /**
382366 * This interface defines the API supported by database query collections
383367 */
@@ -416,7 +400,7 @@ public interface QueryAPI {
416400 *
417401 * @return query object enumerated constant
418402 */
419- Enum <?> getEnum ();
403+ Enum <? extends QueryAPI > getEnum (); //NOSONAR
420404 }
421405
422406 /**
0 commit comments