IBM DB2 Manual page 64

Table of Contents

Advertisement

}
rs.close();
cstmt.close();
Retrieving an unknown number of result sets from a stored procedure in a
JDBC application:
Retrieving an unknown number of result sets from a stored procedure is a more
complicated procedure than retrieving a known number of result sets.
To retrieve result sets when you do not know the number of result sets or their
contents, you need to retrieve ResultSets, until no more ResultSets are returned.
For each ResultSet, use ResultSetMetaData methods to determine its contents.
After you call a stored procedure, follow these basic steps to retrieve the contents
of an unknown number of result sets.
1. Check the value that was returned from the execute statement that called the
2. Repeat the following steps in a loop:
Example: The following code illustrates retrieving result sets when you do not
know the number of result sets or their contents. The numbers to the right of
selected statements correspond to the previously described steps.
CallableStatement cstmt;
ResultSet rs;
...
boolean resultsAvailable = cstmt.execute(); // Call the stored procedure
while (resultsAvailable) {
}
Keeping result sets open when retrieving multiple result sets from a stored
procedure in a JDBC application:
The getMoreResults method has a form that lets you leave the current ResultSet
open when you open the next ResultSet.
To specify whether result sets stay open, follow this process:
When you call getMoreResults to check for the next ResultSet, use this form:
CallableStatement.getMoreResults(int current);
48
Application Programming Guide and Reference for Java
stored procedure.
If the returned value is true, there is at least one result set, so you need to go
to the next step.
a. Invoke the getResultSet method to obtain a result set, which is in a
ResultSet object. Invoking this method closes the previous result set.
b. Use ResultSetMetaData methods to determine the contents of the ResultSet,
and retrieve data from the ResultSet.
c. Invoke the getMoreResults method to determine whether there is another
result set. If getMoreResults returns true, go to step 1 to get the next result
set.
ResultSet rs = cstmt.getResultSet();
...
resultsAvailable = cstmt.getMoreResults(); // Check for next result set 2c
// Close the result set
// Close the statement
// Test for result sets
// Get a result set
// Process the ResultSet
// as you would process
// a ResultSet from a table
// (Also closes the
// previous result set)
1
2a

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents