Java – JDBC: check stored procedure return value

javajdbcpostgresqlsqlstored-procedures

I have a stored procedure on my postgresql database that return integer value. When i call my procedure form jdbc client using CallableStatement, when i call execute() method on CallableStatement object, it returns only a boolean. How can i read the effective return value of function?

Best Answer

You need to get a ResultSet via:

ResultSet rs = stmt.executeQuery("SELECT * FROM setoffunc()");
while (rs.next()) {
   // read results
}
rs.close();
stmt.close();

Or:

// Procedure call.
CallableStatement proc = conn.prepareCall("{ ? = call refcursorfunc() }");
proc.registerOutParameter(1, Types.OTHER);
proc.execute();
ResultSet results = (ResultSet) proc.getObject(1);
while (results.next()) {
    // read results
}
results.close();
proc.close();

See the documentation for more details: http://jdbc.postgresql.org/documentation/80/callproc.html