When you code a Java™ stored procedure that uses the JAVA parameter style, you must use the following conventions:
Java stored procedures using the JAVA parameter style are public static 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.
If a parameter is passed in a Java type that permits the null value, a Java method can compare the parameter to null to determine if an input parameter is an SQL NULL.
The following Java types do not support the null value:
If a null value is passed to a Java type that does not support the null value, an SQL Exception with an error code of -20205 will be returned.
Output parameters are passed as arrays that contain one element. The Java stored procedure can set the first element of the array to set the output parameter.
A connection to the embedding application context is accessed using the following Java Database Connectivity (JDBC) call:
connection=DriverManager.getConnection("jdbc:default:connection");
This connection then runs SQL statements with JDBC APIs.
The following is a small stored procedure with one input and two outputs. It runs 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 java.sql.*; public class sample2 { public static void donut(String query, int[] rowCount, String[] sqlstate) throws Exception { try { Connection c=DriverManager.getConnection("jdbc:default:connection"); Statement s=c.createStatement(); ResultSet r=s.executeQuery(query); int counter=0; while(r.next()){ counter++; } r.close(); s.close(); rowCount[0] = counter; }catch(SQLException x){ sqlstate[0]= x.getSQLState(); } } }
In the SQLj standard, to return a result set in routines that use the JAVA parameter style, the result set must be set explicitly. When a procedure is created that returns result sets, additional result set parameters are added to the end of the parameter list. For example, the statement
CREATE PROCEDURE RETURNTWO() DYNAMIC RESULT SETS 2 LANGUAGE JAVA PARAMETER STYLE JAVA EXTERNAL NAME 'javaClass!returnTwoResultSets'
would call a Java method with the signature public static void returnTwoResultSets(ResultSet[] rs1, ResultSet[] rs2).
The output parameters of the result sets must be explicitly set as illustrated in the following example. As in the DB2GENERAL style, the result sets and corresponding statements should not be closed.
Example: Stored procedure that returns two result sets
import java.sql.*; public class javaClass { /** * Java stored procedure, with JAVA style parameters, * that processes two predefined sentences * and returns two result sets * * @param ResultSet[] rs1 first ResultSet * @param ResultSet[] rs2 second ResultSet */ public static void returnTwoResultSets (ResultSet[] rs1, ResultSet[] rs2) throws Exception { //get caller's connection to the database; inherited from StoredProc Connection con = DriverManager.getConnection("jdbc:default:connection"); //define and process the first select statement Statement stmt1 = con.createStatement(); String sql1 = "select value from table01 where index=1"; rs1[0] = stmt1.executeQuery(sql1); //define and process the second select statement Statement stmt2 = con.createStatement(); Stringsql2 = "select value from table01 where index=2"; rs2[0] = stmt2.executeQuery(sql2); } }
On the server, the additional result set parameters are not examined to determine the ordering of the results sets. The results sets on the server are returned in the order in which they were opened. To ensure compatibility with the SQLj standard, the result should be assigned in the order that they are opened, as previously shown.