Skip to content

Result Extraction

Philippe Marschall edited this page Oct 12, 2016 · 14 revisions

If your stored procedure returns a result there are three different way of how you can retrieve the result.

Out Parameter

For stored procedures that use out parameter use the @OutParameter annotation.

public interface TaxProcedures {

  @OutParameter
  BigDecimal salesTax(BigDecimal subtotal);

}

The out parameter is assumed to be the last parameter of the stored procedure. If the out parameter has a different index use the index attribute.

public interface TaxProcedures {

  @OutParameter(index = 1) // JDBC indices are 1-based
  BigDecimal salesTax(BigDecimal subtotal);

}

Return Value

For SQL functions rather than stored procedures use the @ReturnValue annotation

public interface TaxProcedures {

  @ReturnValue
  BigDecimal salesTax(BigDecimal subtotal);

}

ResultSet

Some JDBC drivers (e.g.. H2) don't support out parameter but return the values in a ResultSet, in this case add neither annotation.

public interface TaxProcedures {

  BigDecimal salesTax(BigDecimal subtotal);

}

Ref Cursor

If the stored procedure uses a ref cursor to return multiple values they can be collected into a List

public interface DateProcedures {

  @OutParameter
  List<Date> getLeapYears();

}

If the procedure uses out parameters then JDBC 4.2 is required to support this feature. If the driver does not support JDBC 4.2 vendor types are needed. E.g. for the OJDBC 12.1c driver or earlier the type of the out parameter has to be set to OracleTypes#CURSOR

MySQL implicitly supports ref cursors but not via out parameters. In this case simply leave out the @OutParameter and @ReturnValue annotations as the driver returns it as a ResultSet.

Clone this wiki locally