ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzaha_5.4.0.1/callexample1.htm

164 lines
8.2 KiB
HTML
Raw Normal View History

2024-04-02 14:02:31 +00:00
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-us" xml:lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="security" content="public" />
<meta name="Robots" content="index,follow" />
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
<meta name="DC.Type" content="reference" />
<meta name="DC.Title" content="Example: Create a procedure with multiple ResultSets" />
<meta name="abstract" content="This example shows how to access a database and then create a procedure with multiple ResultSets." />
<meta name="description" content="This example shows how to access a database and then create a procedure with multiple ResultSets." />
<meta name="DC.Relation" scheme="URI" content="callable.htm" />
<meta name="DC.Relation" scheme="URI" content="callproc.htm" />
<meta name="DC.Relation" scheme="URI" content="callexample2.htm" />
<meta name="DC.Relation" scheme="URI" content="callexample3.htm" />
<meta name="copyright" content="(C) Copyright IBM Corporation 2006" />
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 2006" />
<meta name="DC.Format" content="XHTML" />
<meta name="DC.Identifier" content="callexample1" />
<meta name="DC.Language" content="en-us" />
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
<link rel="stylesheet" type="text/css" href="./ic.css" />
<title>Example: Create a procedure with multiple ResultSets</title>
</head>
<body id="callexample1"><a name="callexample1"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Example: Create a procedure with multiple ResultSets</h1>
<div><p>This example shows how to access a database and then create a procedure
with multiple ResultSets.</p>
<div class="section"><div class="note"><span class="notetitle">Note:</span> Read the <a href="codedisclaimer.htm">Code example disclaimer</a> for
important legal information.</div>
<pre>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();
}
}
}</pre>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="callable.htm" title="The CallableStatement interface extends PreparedStatement and provides support for output and input/output parameters. The CallableStatement interface also has support for input parameters that is provided by the PreparedStatement interface.">CallableStatements</a></div>
</div>
<div class="relconcepts"><strong>Related concepts</strong><br />
<div><a href="callproc.htm" title="Processing SQL stored procedure calls with a CallableStatement object is accomplished with the same methods that are used with a PreparedStatement object.">Process CallableStatements</a></div>
</div>
<div class="relref"><strong>Related reference</strong><br />
<div><a href="callexample2.htm" title="This example shows how to access a database and then create a procedure with input and output parameters.">Example: Create a procedure with input and output parameters</a></div>
<div><a href="callexample3.htm" title="This example shows how to access a database and then create a procedure with return values.">Example: Create a procedure with return values</a></div>
</div>
</div>
</body>
</html>