148 lines
7.0 KiB
HTML
148 lines
7.0 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="concept" />
|
|
<meta name="DC.Title" content="JAVA parameter style" />
|
|
<meta name="DC.Relation" scheme="URI" content="javaproc.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="javaparm" />
|
|
<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>JAVA parameter style</title>
|
|
</head>
|
|
<body id="javaparm"><a name="javaparm"><!-- --></a>
|
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
|
<h1 class="topictitle1">JAVA parameter style</h1>
|
|
<div><p>When you code a Java™ stored procedure that uses the JAVA
|
|
parameter style, you must use the following conventions:</p>
|
|
<ul><li>The Java method must be a public void static (not instance)
|
|
method.</li>
|
|
<li>The parameters of the Java method must be SQL-compatible types.</li>
|
|
<li>A Java method
|
|
may test for an SQL NULL value when the parameter is a null-capable type (like
|
|
String).</li>
|
|
<li>Output parameters are returned by using single element arrays.</li>
|
|
<li>The Java method may access the current database using the
|
|
getConnection method.</li>
|
|
</ul>
|
|
<p>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.</p>
|
|
<p>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.</p>
|
|
<p>The following Java types do not support the null value:</p>
|
|
<ul><li>short</li>
|
|
<li>int</li>
|
|
<li>long</li>
|
|
<li>float</li>
|
|
<li>double</li>
|
|
</ul>
|
|
<p>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. </p>
|
|
<p>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.</p>
|
|
<p>A connection to the embedding application context is accessed using the
|
|
following Java Database Connectivity (JDBC) call:</p>
|
|
<blockquote><pre>connection=DriverManager.getConnection("jdbc:default:connection");</pre>
|
|
</blockquote>
|
|
<p>This connection then runs SQL statements with JDBC APIs.</p>
|
|
<p>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.</p>
|
|
<p><strong>Example:</strong> Stored procedure with one input and two outputs</p>
|
|
<div class="note"><span class="notetitle">Note:</span> By using the
|
|
code examples, you agree to the terms of the <a href="codedisclaimer.htm">Code license and disclaimer information</a>.</div>
|
|
<pre> 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();
|
|
}
|
|
}
|
|
}</pre>
|
|
<p>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</p>
|
|
<blockquote><pre>CREATE PROCEDURE RETURNTWO()
|
|
DYNAMIC RESULT SETS 2
|
|
LANGUAGE JAVA
|
|
PARAMETER STYLE JAVA
|
|
EXTERNAL NAME 'javaClass!returnTwoResultSets'</pre>
|
|
</blockquote>
|
|
<p>would call a Java method with the signature <samp class="codeph">public static
|
|
void returnTwoResultSets(ResultSet[] rs1, ResultSet[] rs2)</samp>.</p>
|
|
<p>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.</p>
|
|
<p><strong>Example:</strong> Stored procedure that returns two result sets</p>
|
|
<div class="note"><span class="notetitle">Note:</span> By using the
|
|
code examples, you agree to the terms of the <a href="codedisclaimer.htm">Code license and disclaimer information</a>.</div>
|
|
<pre>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);
|
|
}
|
|
}</pre>
|
|
<p>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.</p>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="javaproc.htm" title="When using Java to write stored procedures, you can use two possible parameter passing styles.">Java stored procedures</a></div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html> |