194194 * /** args: [ coffee_name, max_percent, new_price ] */
195195 * RAISE_PRICE("RAISE_PRICE(>, >, =)", Types.VARCHAR, Types.REAL, Types.NUMERIC),
196196 * /** args: [ str, val... ] */
197- * IN_VARARGS("IN_VARARGS(<, >... )", Types.VARCHAR, Types.INTEGER),
197+ * IN_VARARGS("IN_VARARGS(<, >: )", Types.VARCHAR, Types.INTEGER),
198198 * /** args: [ val, str... ] */
199- * OUT_VARARGS("OUT_VARARGS(>, <... )", Types.INTEGER, Types.VARCHAR);
199+ * OUT_VARARGS("OUT_VARARGS(>, <: )", Types.INTEGER, Types.VARCHAR);
200200 *
201201 * private int[] argTypes;
202202 * private String signature;
203203 *
204204 * SProcValues(String signature, int... argTypes) {
205205 * this.signature = signature;
206+ *
206207 * this.argTypes = argTypes;
207208 * }
208209 *
232233public class DatabaseUtils {
233234
234235 private static Pattern SPROC_PATTERN =
235- Pattern .compile ("([\\ p{Alpha}_][\\ p{Alpha}\\ p{Digit}@$#_]*)(?:\\ (([<>=](?:,\\ s*[<>=])*)?([.]{3} )?\\ ))?" );
236+ Pattern .compile ("([\\ p{Alpha}_][\\ p{Alpha}\\ p{Digit}@$#_]*)(?:\\ (([<>=](?:,\\ s*[<>=])*)?(: )?\\ ))?" );
236237
237238 private DatabaseUtils () {
238239 throw new AssertionError ("DatabaseUtils is a static utility class that cannot be instantiated" );
@@ -361,6 +362,40 @@ public static Object executeQuery(Class<?> resultType, String connectionStr, Str
361362 }
362363 }
363364
365+ /**
366+ * Execute the specified stored procedure object with supplied parameters
367+ *
368+ * @param sproc stored procedure object to execute
369+ * @param params an array of objects containing the input parameter values
370+ * @return row 1 / column 1 as integer; -1 if no rows were returned
371+ */
372+ public static int getInt (SProcAPI sproc , Object ... params ) {
373+ Integer result = (Integer ) executeStoredProcedure (Integer .class , sproc , params );
374+ return (result != null ) ? result .intValue () : -1 ;
375+ }
376+
377+ /**
378+ * Execute the specified stored procedure object with supplied parameters
379+ *
380+ * @param sproc stored procedure object to execute
381+ * @param params an array of objects containing the input parameter values
382+ * @return row 1 / column 1 as string; {@code null} if no rows were returned
383+ */
384+ public static String getString (SProcAPI sproc , Object ... params ) {
385+ return (String ) executeStoredProcedure (String .class , sproc , params );
386+ }
387+
388+ /**
389+ * Execute the specified stored procedure object with supplied parameters
390+ *
391+ * @param sproc stored procedure object to execute
392+ * @param params an array of objects containing the input parameter values
393+ * @return {@link ResultPackage} object
394+ */
395+ public static ResultPackage getResultPackage (SProcAPI sproc , Object ... params ) {
396+ return (ResultPackage ) executeStoredProcedure (ResultPackage .class , sproc , params );
397+ }
398+
364399 /**
365400 * Execute the specified stored procedure with the specified arguments, returning a result of the indicated type.
366401 * <p>
@@ -377,7 +412,7 @@ public static Object executeQuery(Class<?> resultType, String connectionStr, Str
377412 * <b>NOTE</b>: If you specify {@link ResultPackage} as the result type, it's recommended that you close this object
378413 * when you're done with it to free up database and JDBC resources that were allocated for it.
379414 */
380- public static Object executeStoredProcedure (Class <?> resultType , SProcAPI sproc , Object ... parms ) {
415+ public static Object executeStoredProcedure (Class <?> resultType , SProcAPI sproc , Object ... params ) {
381416 Objects .requireNonNull (resultType , "[resultType] argument must be non-null" );
382417
383418 String [] args = {};
@@ -410,7 +445,7 @@ public static Object executeStoredProcedure(Class<?> resultType, SProcAPI sproc,
410445
411446 int argsCount = args .length ;
412447 int typesCount = argTypes .length ;
413- int parmsCount = parms .length ;
448+ int parmsCount = params .length ;
414449
415450 int minCount = typesCount ;
416451
@@ -448,13 +483,13 @@ public static Object executeStoredProcedure(Class<?> resultType, SProcAPI sproc,
448483 // process declared parameters
449484 for (i = 0 ; i < minCount ; i ++) {
450485 Mode mode = Mode .fromChar (args [i ].charAt (0 ));
451- parmArray [i ] = Param .create (mode , argTypes [i ], parms [i ]);
486+ parmArray [i ] = Param .create (mode , argTypes [i ], params [i ]);
452487 }
453488
454489 // handle varargs parameters
455490 for (int j = i ; j < parmsCount ; j ++) {
456491 Mode mode = Mode .fromChar (args [i ].charAt (0 ));
457- parmArray [j ] = Param .create (mode , argTypes [i ], parms [j ]);
492+ parmArray [j ] = Param .create (mode , argTypes [i ], params [j ]);
458493 }
459494
460495 return executeStoredProcedure (resultType , sproc .getConnection (), sprocName , parmArray );
@@ -495,7 +530,7 @@ public static Object executeStoredProcedure(Class<?> resultType, String connecti
495530 CallableStatement statement = connection .prepareCall (sprocStr .toString ());
496531
497532 for (int i = 0 ; i < params .length ; i ++) {
498- params [i ].set (statement , i );
533+ params [i ].set (statement , i + 1 );
499534 }
500535
501536 return executeStatement (resultType , connection , statement );
@@ -534,16 +569,32 @@ private static Object executeStatement(Class<?> resultType, Connection connectio
534569 if (resultType == null ) {
535570 result = Integer .valueOf (statement .executeUpdate ());
536571 } else {
537- resultSet = statement .executeQuery (); //NOSONAR
538-
539- if (resultType == ResultPackage .class ) {
540- result = new ResultPackage (connection , statement , resultSet ); //NOSONAR
541- } else if (resultType == Integer .class ) {
542- result = Integer .valueOf ((resultSet .next ()) ? resultSet .getInt (1 ) : -1 );
543- } else if (resultType == String .class ) {
544- result = (resultSet .next ()) ? resultSet .getString (1 ) : null ;
572+ if (statement instanceof CallableStatement ) {
573+ if (statement .execute ()) {
574+ resultSet = statement .getResultSet (); //NOSONAR
575+ }
576+
577+ if (resultType == ResultPackage .class ) {
578+ result = new ResultPackage (connection , statement , resultSet ); //NOSONAR
579+ } else if (resultType == Integer .class ) {
580+ result = ((CallableStatement ) statement ).getInt (1 );
581+ } else if (resultType == String .class ) {
582+ result = ((CallableStatement ) statement ).getString (1 );
583+ } else {
584+ result = ((CallableStatement ) statement ).getObject (1 );
585+ }
545586 } else {
546- result = (resultSet .next ()) ? resultSet .getObject (1 , resultType ) : null ;
587+ resultSet = statement .executeQuery (); //NOSONAR
588+
589+ if (resultType == ResultPackage .class ) {
590+ result = new ResultPackage (connection , statement , resultSet ); //NOSONAR
591+ } else if (resultType == Integer .class ) {
592+ result = Integer .valueOf ((resultSet .next ()) ? resultSet .getInt (1 ) : -1 );
593+ } else if (resultType == String .class ) {
594+ result = (resultSet .next ()) ? resultSet .getString (1 ) : null ;
595+ } else {
596+ result = (resultSet .next ()) ? resultSet .getObject (1 , resultType ) : null ;
597+ }
547598 }
548599 }
549600
@@ -702,6 +753,14 @@ private ResultPackage(Connection connection, PreparedStatement statement, Result
702753 this .resultSet = resultSet ;
703754 }
704755
756+ public Connection getConnection () {
757+ return connection ;
758+ }
759+
760+ public PreparedStatement getStatement () {
761+ return statement ;
762+ }
763+
705764 /**
706765 * Get the result set object of this package.
707766 *
0 commit comments