3

JDBC executeQuery () Returns ResultSet and Output Results

 3 years ago
source link: https://www.codesd.com/item/jdbc-executequery-returns-resultset-and-output-results.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

JDBC executeQuery () Returns ResultSet and Output Results

advertisements

I am calling one sp with few out params and as per my requirement,I need to use ResultSet on some condition and out params on other conditions. But using executeQuery(), I am getting, JZ0R0: ResultSet has already been closed error.(I am using Sybase 15)

Here is the example:

Connection conn = ~~;
CallableStatement cstmt = conn.prepareCall("{call my_sp(?)");
cstmt.registerOutParameter(1,java.sql.Types.INTEGER);
ResultSet rs = cstmt.executeQuery();

If i try to do, below code now,

int res = cstmt.getInt(1);

Then try to iterate the ResultSet object, as below, then I get above ResultSet close error.

ResultSetMetaData rsmd = rs.getMetaData();

Is there any way I can get the output param value and then iterate on ResultSet object or vice-versa?


According to the JDBC spec you must retrieve the result set before output parameters

Here is the original spec

7.1.7 Retrieve OUT Parameters after Results

Because of limitations imposed by some DBMSs, it is recommended that for maximum portability, all of the results in a ResultSet object generated by the execution of a CallableStatement object should be retrieved before OUT parameters are retrieved. When all values have been retrieved from a result set, the method ResultSet.next will return false.

If a CallableStatement object returns multiple ResultSet objects (which is possible only if it is executed with a call to the method execute), all of the results should be retrieved before OUT parameters are retrieved. In this case, to be sure that all results have been accessed, the Statement methods getResultSet, getUpdateCount, and getMoreResults need to be called until there are no more results. When all results have been exhausted, the method getMoreResults returns false, and the method getUpdateCount returns -1.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK