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

146 lines
9.7 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="task" />
<meta name="DC.Title" content="Use Java SQL routines" />
<meta name="abstract" content="You can access Java programs from SQL statements and programs. This can be done using Java stored procedures and Java user-defined functions (UDFs)." />
<meta name="description" content="You can access Java programs from SQL statements and programs. This can be done using Java stored procedures and Java user-defined functions (UDFs)." />
<meta name="DC.Relation" scheme="URI" content="jsqlrout.htm" />
<meta name="DC.Relation" scheme="URI" content="javaproc.htm" />
<meta name="DC.Relation" scheme="URI" content="writeudf.htm" />
<meta name="DC.Relation" scheme="URI" content="sqljjar.htm" />
<meta name="DC.Relation" scheme="URI" content="udfparam.htm" />
<meta name="DC.Relation" scheme="URI" content="sqljsetup.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="jsqlover" />
<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>Use Java SQL routines</title>
</head>
<body id="jsqlover"><a name="jsqlover"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Use Java SQL routines</h1>
<div><p>You can access Java™ programs from SQL statements and programs.
This can be done using Java stored procedures and Java user-defined
functions (UDFs).</p>
<div class="section"><p>To use Java SQL routines, complete the following
tasks:</p>
</div>
<ol><li class="stepexpand"><span>Enable SQLJ</span> <p>Because any Java SQL routine may use SQLJ, make SQLJ
runtime support always available when running Java 2 Software Development Kit (J2SDK).
To enable runtime support for SQLJ in J2SDK, add a link to the SQLJ runtime.zip
file from your extensions directory. For more information, see <a href="sqljsetup.htm#sqljsetup">Setting up your server to use SQLJ</a>.</p>
</li>
<li class="stepexpand"><span>Write the Java methods for the routines</span> <p>A Java SQL
routine processes a Java method from SQL. This method must be
written using either the DB2<sup>®</sup> or SQLJ parameter passing conventions. See <a href="javaproc.htm">Java stored procedures</a>, <a href="writeudf.htm">Java user-defined
functions</a>, and <a href="udftable.htm">Java user-defined table functions</a> for
more information about coding a method used by a Java SQL routine.</p>
</li>
<li class="stepexpand"><span>Compile the Java classes</span> <p>Java SQL
routines written using the Java parameter style may be compiled without
any addition setup. However, Java SQL routines using the DB2GENERAL parameter
style must extend either the com.ibm.db2.app.UDF class or com.ibm.db2.app.StoredProc
class. These classes are contained in the JAR file, /QIBM/ProdData/Java400/ext/db2routines_classes.jar.
When using javac to compile these routines, this JAR file must exist in the
CLASSPATH. For example, the following command compiles a Java source
file containing a routine which uses the DB2GENERAL parameter style:</p>
<pre> javac -DCLASSPATH=/QIBM/ProdData/Java400/ext/db2routines_classes.jar
source.java</pre>
</li>
<li class="stepexpand"><span>Make the compiled classes accessible to the Java virtual
machine (JVM) used by the database</span> <p>The user-defined classes
used by the database JVM can either reside in the /QIBM/UserData/OS400/SQLLib/Function
directory or in a JAR file registered to the database.</p>
<p>The /QIBM/UserData/OS400/SQLLib/Function
is the iSeries™ equivalent
of /sqllib/function, the directory where DB2 UDB stores Java stored procedures and Java UDFs
on other platforms. If the class is part of a Java package, it must reside in the
appropriate subdirectory. For example, if the runit class is created as part
of the foo.bar package, the file runnit.class should be in the integrated
file system directory, /QIBM/ProdData/OS400/SQLLib/Function/foo/bar.</p>
<p>The class file may also be placed in a JAR file that is registered to
the database. The JAR file is registered using the SQLJ.INSTALL_JAR stored
procedure. This stored procedure is used to assign a JAR ID to a JAR file.
This JAR ID is used to identify the JAR file in which the class file
resides. See <a href="sqljjar.htm">SQLJ procedures that manipulate JAR
files</a> for more information on SQLJ.INSTALL_JAR as well as other stored
procedures to manipulate JAR files.</p>
</li>
<li class="stepexpand"><span>Register the routine with the database.</span> <div class="p">Java SQL
routines is registered with the database using the CREATE PROCEDURE and CREATE
FUNCTION SQL statements. These statements contain the following elements:<dl><dt class="dlterm"><strong>CREATE keywords</strong></dt>
<dd>The SQL statements to create a Java SQL routine begin with either CREATE
PROCEDURE or CREATE STATEMENT.</dd>
<dt class="dlterm"><strong>Name of routine</strong></dt>
<dd>The SQL statement then identifies the name of the routine that is known
to the database. This is the name that is used to access the Java routine
from SQL.</dd>
<dt class="dlterm"><strong>Parameters and return value</strong></dt>
<dd>The SQL statement then identifies the parameters and return values, if
applicable, for the Java routine.</dd>
<dt class="dlterm"><strong>LANGUAGE JAVA</strong></dt>
<dd>The SQL statement uses the keywords LANGUAGE JAVA to indicate that the
routine was written in Java.</dd>
<dt class="dlterm"><strong>PARAMETER STYLE KEYWORDS</strong></dt>
<dd>The SQL statement then identifies the parameter style using the keywords
PARAMETER STYLE JAVA or PARAMETER STYLE DB2GENERAL.</dd>
<dt class="dlterm"><strong>External name</strong></dt>
<dd>The SQL statement then identifies the Java method to be processed as Java SQL
routines. The external name has one of two formats: <ul><li>If the method is in a class file that is located under the /QIBM/UserData/OS400/SQLLib/Function
directory, then the method is identified using the format <em>classname.methodname</em>,
where <em>classname</em> is the fully qualified name of the class and <em>methodname</em> is
the name of the method.</li>
<li>If the method is in a JAR file registered to the database, then the method
is identified using the format <em>jarid</em>:<em>classname</em>.<em>methodname</em>,
where <em>jarid</em> is the JAR ID of the registered JAR file, <em>classname</em>
is the name of the class, and <em>methodname</em> is the name of the method.</li>
</ul>
</dd>
</dl>
</div>
<p>The iSeries Navigator may be used to create
a stored procedure or user-defined function that uses the Java parameter
style.</p>
</li>
<li class="stepexpand"><span>Use the Java procedure</span> <p>A Java stored
procedure is called using the SQL CALL statement. A Java UDF
is a function that is called as part of another SQL statement.</p>
</li>
</ol>
</div>
<div>
<ul class="ullinks">
<li class="ulchildlink"><strong><a href="sqljsetup.htm">Setting up your server to use SQLJ</a></strong><br />
Before running a Java program that contains embedded SQLJ
statements, ensure that you set up your server to support SQLJ. SQLJ support
requires that you modify the CLASSPATH environment variable for your server.</li>
</ul>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="jsqlrout.htm" title="Your iSeries server provides the ability to access Java programs from SQL statements and programs. This can be done using Java stored procedures and Java user-defined functions (UDFs). The iSeries server supports both the DB2 and SQLJ conventions for calling Java stored procedures and Java UDFs. Both Java stored procedures and Java UDFs can use Java classes that are stored in JAR files. The iSeries server uses stored procedures defined by the SQLJ Part 1 standard to register JAR files with the database.">Java SQL routines</a></div>
</div>
<div class="relconcepts"><strong>Related concepts</strong><br />
<div><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><a href="writeudf.htm" title="A Java scalar function returns one value from a Java program to the database. For example, a scalar function could be created that returns the sum of two numbers.">Java user-defined scalar functions</a></div>
<div><a href="sqljjar.htm" title="Both Java stored procedures and Java UDFs can use Java classes that are stored in Java JAR files.">SQLJ procedures that manipulate JAR files</a></div>
<div><a href="udfparam.htm" title="The following table lists how SQL data types are represented in Java stored procedures and UDFs.">Parameter passing conventions for Java stored procedures and UDFs</a></div>
</div>
</div>
</body>
</html>