IBM DB2 Manual page 63

Table of Contents

Advertisement

"Learning about a ResultSet using ResultSetMetaData methods" on page 35
"Retrieving data from tables using the PreparedStatement.executeQuery
method" on page 33
"Retrieving data from tables using the Statement.executeQuery method" on
page 32
"Calling stored procedures in JDBC applications" on page 45
"Writing a Java stored procedure to return result sets" on page 178
Retrieving a known number of result sets from a stored procedure in a JDBC
application:
Retrieving a known number of result sets from a stored procedure is a simpler
procedure than retrieving an unknown number of result sets.
To retrieve result sets when you know the number of result sets and their contents,
follow these steps:
1. Invoke the Statement.execute method, the PreparedStatement.execute method,
or the CallableStatement.execute method to call the stored procedure.
Use PreparedStatement.execute if the stored procedure has input parameters.
2. Invoke the getResultSet method to obtain the first result set, which is in a
ResultSet object.
3. In a loop, position the cursor using the next method, and retrieve data from
each column of the current row of the ResultSet object using getXXX methods.
4. If there are n result sets, repeat the following steps n-1 times:
a. Invoke the getMoreResults method to close the current result set and point
to the next result set.
b. Invoke the getResultSet method to obtain the next result set, which is in a
ResultSet object.
c. In a loop, position the cursor using the next method, and retrieve data from
each column of the current row of the ResultSet object using getXXX
methods.
Example: The following code illustrates retrieving two result sets. The first result
set contains an INTEGER column, and the second result set contains a CHAR
column. The numbers to the right of selected statements correspond to the
previously described steps.
CallableStatement cstmt;
ResultSet rs;
int i;
String s;
...
cstmt.execute();
rs = cstmt.getResultSet();
while (rs.next()) {
i = rs.getInt(1);
System.out.println("Value from first result set = " + i);
}
cstmt.getMoreResults();
rs = cstmt.getResultSet();
while (rs.next()) {
s = rs.getString(1);
System.out.println("Value from second result set = " + s);
// Call the stored procedure
// Get the first result set
// Position the cursor
// Retrieve current result set value
// Print the value
// Point to the second result set 4a
// and close the first result set
// Get the second result set
// Position the cursor
// Retrieve current result set value
// Print the value
Chapter 3. JDBC application programming
1
2
3
4b
4c
47

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents