124 lines
6.6 KiB
HTML
124 lines
6.6 KiB
HTML
|
<?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 input and output parameters" />
|
||
|
<meta name="abstract" content="This example shows how to access a database and then create a procedure with input and output parameters." />
|
||
|
<meta name="description" content="This example shows how to access a database and then create a procedure with input and output parameters." />
|
||
|
<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="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="callexample2" />
|
||
|
<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 input and output parameters</title>
|
||
|
</head>
|
||
|
<body id="callexample2"><a name="callexample2"><!-- --></a>
|
||
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
||
|
<h1 class="topictitle1">Example: Create a procedure with input and output parameters</h1>
|
||
|
<div><p>This example shows how to access a database and then create a procedure
|
||
|
with input and output parameters.</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 CallableStatementExample2 {
|
||
|
|
||
|
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 in, out, and in/out parameters.
|
||
|
String sql = "CREATE PROCEDURE MYLIBRARY.SQLSPEX2 " +
|
||
|
"(IN P1 INTEGER, OUT P2 INTEGER, INOUT P3 INTEGER) " +
|
||
|
"LANGUAGE SQL SPECIFIC MYLIBRARY.SQLSPEX2 " +
|
||
|
"EX2: BEGIN " +
|
||
|
" SET P2 = P1 + 1; " +
|
||
|
" SET P3 = P3 + 1; " +
|
||
|
"END EX2 ";
|
||
|
|
||
|
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();
|
||
|
|
||
|
// Prepare a callable statement used to run the procedure.
|
||
|
CallableStatement cs = c.prepareCall("CALL MYLIBRARY.SQLSPEX2(?, ?, ?)");
|
||
|
|
||
|
// All input parameters must be set and all output parameters must
|
||
|
// be registered. Notice that this means we have two calls to make
|
||
|
// for an input output parameter.
|
||
|
cs.setInt(1, 5);
|
||
|
cs.setInt(3, 10);
|
||
|
cs.registerOutParameter(2, Types.INTEGER);
|
||
|
cs.registerOutParameter(3, Types.INTEGER);
|
||
|
|
||
|
// Run the procedure
|
||
|
cs.executeUpdate();
|
||
|
|
||
|
// Verify the output parameters have the desired values.
|
||
|
System.out.println("The value of P2 should be P1 (5) + 1 = 6. --> " + cs.getInt(2));
|
||
|
System.out.println("The value of P3 should be P3 (10) + 1 = 11. --> " + cs.getInt(3));
|
||
|
|
||
|
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="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>
|