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

118 lines
6.1 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 return values" />
<meta name="abstract" content="This example shows how to access a database and then create a procedure with return values." />
<meta name="description" content="This example shows how to access a database and then create a procedure with return values." />
<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="callexample1.htm" />
<meta name="DC.Relation" scheme="URI" content="callexample2.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="callexample3" />
<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 return values</title>
</head>
<body id="callexample3"><a name="callexample3"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Example: Create a procedure with return values</h1>
<div><p>This example shows how to access a database and then create a procedure
with return values.</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 CallableStatementExample3 {
public static void main(java.lang.String[] args) {
// Register the native JDBC driver. If the driver cannot
// be registered, 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 a return value.
String sql = "CREATE PROCEDURE MYLIBRARY.SQLSPEX3 " +
" LANGUAGE SQL SPECIFIC MYLIBRARY.SQLSPEX3 " +
" EX3: BEGIN " +
" RETURN 1976; " +
" END EX3 ";
try {
s.executeUpdate(sql);
} catch (SQLException e) {
// NOTE: The error is ignored here. The assumptions is
// made 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();
// Prepare a callable statement used to run the procedure.
CallableStatement cs = c.prepareCall("? = CALL MYLIBRARY.SQLSPEX3");
// You still need to register the output parameter.
cs.registerOutParameter(1, Types.INTEGER);
// Run the procedure.
cs.executeUpdate();
// Show that the correct value is returned.
System.out.println("The return value
should always be 1976 for this example:
--&gt; " + cs.getInt(1));
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="callexample1.htm" title="This example shows how to access a database and then create a procedure with multiple ResultSets.">Example: Create a procedure with multiple ResultSets</a></div>
<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>
</div>
</body>
</html>