This example shows how to access a database and then create a procedure with multiple ResultSets.
import java.sql.*;
import java.util.Properties;
public class CallableStatementExample1 {
public static void main(java.lang.String[] args) {
// Register the Native JDBC driver. If we cannot
// register the driver, the test cannot continue.
try {
Class.forName("com.ibm.db2.jdbc.app.DB2Driver");
// Create the connection properties
Properties properties = new Properties ();
properties.put ("user", "userid");
properties.put ("password", "password");
// Connect to the local iSeries database
Connection c = DriverManager.getConnection("jdbc:db2://*local", properties);
Statement s = c.createStatement();
// Create a procedure with multiple ResultSets.
String sql = "CREATE PROCEDURE MYLIBRARY.SQLSPEX1 " +
"RESULT SET 2 LANGUAGE SQL READS SQL DATA SPECIFIC MYLIBRARY.SQLSPEX1 " +
"EX1: BEGIN " +
" DECLARE C1 CURSOR FOR SELECT * FROM QSYS2.SYSPROCS " +
" WHERE SPECIFIC_SCHEMA = 'MYLIBRARY'; " +
" DECLARE C2 CURSOR FOR SELECT * FROM QSYS2.SYSPARMS " +
" WHERE SPECIFIC_SCHEMA = 'MYLIBRARY'; " +
" OPEN C1; " +
" OPEN C2; " +
" SET RESULT SETS CURSOR C1, CURSOR C2; " +
"END EX1 ";
try {
s.executeUpdate(sql);
} catch (SQLException e) {
// NOTE: We are ignoring the error here. We are making
// the assumption that the only reason this fails
// is because the procedure already exists. Other
// reasons that it could fail are because the C compiler
// is not found to compile the procedure or because
// collection MYLIBRARY does not exist on the system.
}
s.close();
// Now use JDBC to run the procedure and get the results back. In
// this case we are going to get information about 'MYLIBRARY's stored
// procedures (which is also where we created this procedure, thereby
// ensuring that there is something to get.
CallableStatement cs = c.prepareCall("CALL MYLIBRARY.SQLSPEX1");
ResultSet rs = cs.executeQuery();
// We now have the first ResultSet object that the stored procedure
// left open. Use it.
int i = 1;
while (rs.next()) {
System.out.println("MYLIBRARY stored procedure
" + i + " is " + rs.getString(1) + "." +
rs.getString(2));
i++;
}
System.out.println("");
// Now get the next ResultSet object from the system - the previous
// one is automatically closed.
if (!cs.getMoreResults()) {
System.out.println("Something went wrong. There should have
been another ResultSet, exiting.");
System.exit(0);
}
rs = cs.getResultSet();
// We now have the second ResultSet object that the stored procedure
// left open. Use that one.
i = 1;
while (rs.next()) {
System.out.println("MYLIBRARY procedure " + rs.getString(1)
+ "." + rs.getString(2) +
" parameter: " + rs.getInt(3) + " direction:
" + rs.getString(4) +
" data type: " + rs.getString(5));
i++;
}
if (i == 1) {
System.out.println("None of the stored procedures have any parameters.");
}
if (cs.getMoreResults()) {
System.out.println("Something went wrong,
there should not be another ResultSet.");
System.exit(0);
}
cs.close(); // close the CallableStatement object
c.close(); // close the Connection object.
} catch (Exception e) {
System.out.println("Something failed..");
System.out.println("Reason: " + e.getMessage());
e.printStackTrace();
}
}
}