112 lines
9.0 KiB
HTML
112 lines
9.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="Embed SQL statements in your Java application" />
|
|
<meta name="abstract" content="Static SQL statements in SQLJ are in SQLJ clauses. SQLJ clauses begin with #sql and end with a semicolon (;) character." />
|
|
<meta name="description" content="Static SQL statements in SQLJ are in SQLJ clauses. SQLJ clauses begin with #sql and end with a semicolon (;) character." />
|
|
<meta name="DC.Relation" scheme="URI" content="sqljintr.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="sqljprof.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="sqlj.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="sqljdbpc.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="sqljdbpp.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="sqljpdb.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="sqljpc.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="sqljcr.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="sqljhost.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="sqljex.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="sqljembd" />
|
|
<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>Embed SQL statements in your Java application</title>
|
|
</head>
|
|
<body id="sqljembd"><a name="sqljembd"><!-- --></a>
|
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
|
<h1 class="topictitle1">Embed SQL statements in your Java application</h1>
|
|
<div><p>Static SQL statements in SQLJ are in SQLJ clauses. SQLJ clauses
|
|
begin with <samp class="codeph">#sql</samp> and end with a semicolon (;) character.</p>
|
|
<p>Before you create any SQLJ clauses in your Java™ application, import the following packages:</p>
|
|
<ul><li>import java.sql.*;</li>
|
|
<li>import sqlj.runtime.*;</li>
|
|
<li>import sqlj.runtime.ref.*;</li>
|
|
</ul>
|
|
<p>The simplest SQLJ clauses are clauses that can be processed and consist
|
|
of the token <samp class="codeph">#sql</samp> followed by an SQL statement enclosed in
|
|
braces. For example, the following SQLJ clause may appear wherever a Java statement
|
|
may legally appear:</p>
|
|
<blockquote><pre>#sql { DELETE FROM TAB };</pre>
|
|
</blockquote>
|
|
<p>The previous example deletes all the rows in the table named TAB.</p>
|
|
<div class="note"><span class="notetitle">Note:</span> For information on compiling and running SQLJ applications, see <a href="sqljcr.htm">Compile and run SQLJ programs</a></div>
|
|
<p>In an SQLJ process clause, the tokens that appear inside the braces are
|
|
either SQL tokens or <a href="sqljhost.htm">host variables</a>. All
|
|
host variables are distinguished by the colon (:) character. SQL tokens never
|
|
occur outside the braces of an SQLJ process clause. For example, the following Java method
|
|
inserts its arguments into an SQL table:</p>
|
|
<blockquote><pre>public void insertIntoTAB1 (int x, String y, float z) throws SQLException
|
|
{
|
|
#sql { INSERT INTO TAB1 VALUES (:x, :y, :z) };
|
|
}</pre>
|
|
</blockquote>
|
|
<p>The method body consists of an SQLJ process clause containing the host
|
|
variables x, y, and z. For more information on host variables, see <a href="sqljhost.htm">Host
|
|
variables in SQLJ</a>.</p>
|
|
<p>In general, SQL tokens are case insensitive (except for identifiers delimited
|
|
by double quotation marks), and can be written in upper, lower, or mixed case. <em>Java</em> tokens,
|
|
however, are case sensitive. For clarity in examples, case insensitive SQL
|
|
tokens are uppercase, and Java tokens are lowercase or mixed case.
|
|
Throughout this topic, the lowercase <samp class="codeph">null</samp> is used to represent
|
|
the Java "null"
|
|
value, and the uppercase <samp class="codeph"> NULL</samp> is used to represent the SQL
|
|
"null" value.</p>
|
|
<p>The following types of SQL constructs may appear in SQLJ programs:</p>
|
|
<ul><li>Queries For example, SELECT statements and expressions.</li>
|
|
<li>SQL Data Change statements (DML) For example, INSERT, UPDATE, DELETE.</li>
|
|
<li>Data statements For example, FETCH, SELECT..INTO.</li>
|
|
<li>Transaction Control statements For example, COMMIT, ROLLBACK, etc.</li>
|
|
<li>Data Definition Language (DDL, also known as Schema Manipulation Language)
|
|
statements For example, CREATE, DROP, ALTER.</li>
|
|
<li>Calls to stored procedures For example, CALL MYPROC(:x, :y, :z)</li>
|
|
<li>Invocations of stored functions For example, VALUES( MYFUN(:x) )</li>
|
|
</ul>
|
|
</div>
|
|
<div>
|
|
<ul class="ullinks">
|
|
<li class="ulchildlink"><strong><a href="sqljhost.htm">Host variables in Structured Query Language for Java</a></strong><br />
|
|
Arguments to embedded SQL statements are passed through host variables. Host variables are variables of the host language, and they can appear in SQL statements.</li>
|
|
<li class="ulchildlink"><strong><a href="sqljex.htm">Example: Embed SQL Statements in your Java application</a></strong><br />
|
|
The following example SQLJ application, App.sqlj, uses static SQL
|
|
to retrieve and update data from the EMPLOYEE table of the DB2<sup>®</sup> sample database.</li>
|
|
</ul>
|
|
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="sqljintr.htm" title="DB2 Structured Query Language for Java (SQLJ) support is based on the SQLJ ANSI standard. The DB2 SQLJ support is contained in the IBM Developer Kit for Java. DB2 SQLJ support allows you to create, build, and run embedded SQL for Java applications.">Access databases using IBM Developer Kit for Java DB2 SQLJ support</a></div>
|
|
</div>
|
|
<div class="relconcepts"><strong>Related concepts</strong><br />
|
|
<div><a href="sqljprof.htm" title="Profiles are generated by the SQLJ Translator, sqlj, when you translate the SQLJ source file. Profiles are serialized binary files. That is why these files have a .ser extension. These files contain the SQL statements from the associated SQLJ source file.">Structured Query Language for Java profiles</a></div>
|
|
<div><a href="sqlj.htm" title="The SQLJ translator, sqlj, generates a serialized profile containing information about the SQL operations found in the SQLJ program. The SQLJ translator uses the /QIBM/ProdData/Java400/ext/translator.zip file.">The structured query language for Java (SQLJ) translator (sqlj)</a></div>
|
|
<div><a href="sqljdbpc.htm" title="You can use the DB2 SQLJ Profile Customizer, db2profc, to make your Java application work more efficiently with your database.">Precompile SQL statements in a profile using the DB2 SQLJ profile customizer, db2profc</a></div>
|
|
<div><a href="sqljdbpp.htm" title="The DB2 SQLJ Profile Printer, db2profp, prints the contents of a DB2 customized profile in plain text. The Profile Printer, profp, prints the contents of profiles generated by the SQLJ translator in plain text.">Print the contents of DB2 SQLJ profiles (db2profp and profp)</a></div>
|
|
<div><a href="sqljpdb.htm" title="The SQLJ profile auditor installer (profdb) installs and uninstalls debugging class-auditors. The debugging class-auditors are installed into an existing set of binary profiles. Once the debugging class-auditors are installed, all RTStatement and RTResultSet calls made during application run time are logged. They can be logged to a file or standard output. The logs can then be inspected to verify the behavior and trace errors of the application. Note that only the calls made to the underlying RTStatement and RTResultSetcall interface at run time are audited.">SQLJ profile auditor installer (profdb)</a></div>
|
|
</div>
|
|
<div class="reltasks"><strong>Related tasks</strong><br />
|
|
<div><a href="sqljpc.htm" title="The SQLJ profile conversion tool (profconv) converts a serialized profile instance to Java class format. The profconv tool is needed because some browsers do not support loading a serialized object from a resource file that is associated with an applet. Run the profconv utility to perform the conversion.">Convert a serialized profile instance to Java class format using the SQLJ profile conversion tool (profconv)</a></div>
|
|
<div><a href="sqljcr.htm" title="If you Java program has embedded SQLJ statements, you need to follow a special procedure to compile and run it.">Compile and run SQLJ programs</a></div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html> |