Writing A Java Stored Procedure To Return Result Sets - IBM DB2 Manual

Table of Contents

Advertisement

In certain cases, you need to declare variables as static and non-final. In those
cases, you can use the following technique to make your routines work correctly
with static variables.
To determine whether the values of static data in a routine have persisted across
routine invocations, define a static boolean variable in the class that contains the
routine. Initially set the variable to false, and then set it to true when you set the
value of static data. Check the value of the boolean variable at the beginning of the
routine. If the value is true, the static data has persisted. Otherwise, the data
values need to be set again. With this technique, static data values are not set for
most routine invocations, but are set more than once during the lifetime of the
JVM. Also, with this technique, it is not a problem for a routine to execute on
different JVMs for different invocations.

Writing a Java stored procedure to return result sets

You can write your Java stored procedures to return multiple query result sets to a
client program.
Your stored procedure can return multiple query result sets to a client program if
the following conditions are satisfied:
v The client supports the DRDA code points that are used to return query result
v The value of DYNAMIC RESULT SETS in the stored procedure definition is
For each result set that you want to be returned, your Java stored procedure must
perform the following actions:
1. For each result set, include an object of type java.sql.ResultSet[] or an array of
2. Execute a SELECT statement to obtain the contents of the result set.
3. Retrieve any rows that you do not want to return to the client.
4. Assign the contents of the result set to element 0 of the java.sql.ResultSet[]
5. Do not close the ResultSet, the statement that generated the ResultSet, or the
The following code shows an example of a Java stored procedure that uses an
SQLJ iterator to retrieve a result set.
178
Application Programming Guide and Reference for Java
sets.
greater than 0.
an SQLJ iterator class in the parameter list for the stored procedure method.
If the stored procedure definition includes a method signature, for each result
set, include java.sql.ResultSet[] or the fully-qualified name of an array of a class
that is declared as an SQLJ iterator in the method signature. These result set
parameters must be the last parameters in the parameter list or method
signature. Do not include a java.sql.ResultSet array or an iterator array in the
SQL parameter list of the stored procedure definition.
object or array of an SQLJ iterator class that you declared in step 1.
connection that is associated with the statement that generated the ResultSet.
DB2 does not return result sets for ResultSets that are closed before the stored
procedure terminates.

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents