When coding a Java™ stored procedure that uses the DB2GENERAL parameter style, you must use these conventions.
A class that includes a Java stored procedure must extend the class, com.ibm.db2.app.StoredProc. Java stored procedures are public instance methods. Within the classes, the stored procedures are identified by their method name and signature. When you call a stored procedure, its signature is generated dynamically, based on the variable types defined by the CREATE PROCEDURE statement.
The com.ibm.db2.app.StoredProc class provides the isNull method, which permits a Java method to determine if an input parameter is an SQL NULL. The com.ibm.db2.app.StoredProc class also provides set...( ) methods that set output parameters. You must use these methods to set output parameters. If you do not set an output parameter, then the output parameter returns the SQL NULL value.
The com.ibm.db2.app.StoredProc class provides the following routine to fetch a JDBC connection to the embedding application context. A connection to the embedding application context is accessed using the following JDBC call:
public Java.sql.Connection getConnection( )
This connection then runs SQL statements with JDBC APIs.
The following is a small stored procedure with one input and two outputs. It processes the given SQL query, and returns both the number of rows in the result and the SQLSTATE.
Example: Stored procedure with one input and two outputs
package mystuff; import com.ibm.db2.app.*; import java.sql.*; public class sample2 extends StoredProc { public void donut(String query, int rowCount, String sqlstate) throws Exception { try { Statement s=getConnection().createStatement(); ResultSet r=s.executeQuery(query); int counter=0; while(r.next()){ counter++; } r.close(); s.close(); set(2, counter); }catch(SQLException x){ set(3, x.getSQLState()); } } }
To return a result set in procedures that use the DB2GENERAL parameter style, the result set and the responding statement must be left open at the end of the procedure. The result set that is returned must be closed by the client application. If multiple results sets are returned, they are returned in the order in which they were opened. For example, the following stored procedure returns two results sets.
Example: Stored procedure that returns two results sets
public void returnTwoResultSets() throws Exception { // get caller's connection to the database; inherited from StoredProc Connection con = getConnection (); Statement stmt1 = con.createStatement (); String sql1 = "select value from table01 where index=1"; ResultSet rs1 = stmt1.executeQuery(sql1); Statement stmt2 = con.createStatement(); String sql2 = "select value from table01 where index=2"; ResultSet rs2 = stmt2.executeQuery(sql2); }