ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzahh_5.4.0.1/jdbcst.htm

107 lines
6.8 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="Running SQL statements with Statement objects" />
<meta name="abstract" content="" />
<meta name="description" content="" />
<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="jdbcst" />
<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>Running SQL statements with Statement objects</title>
</head>
<body id="jdbcst"><a name="jdbcst"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Running SQL statements with Statement objects</h1>
<div><p></p>
<div class="section"><p>Use a <a href="javadoc/com/ibm/as400/access/AS400JDBCStatement.html#NAVBAR_TOP">Statement</a> object to run an SQL statement
and optionally obtain the ResultSet produced by it.</p>
<p>PreparedStatement
inherits from Statement, and CallableStatement inherits from PreparedStatement.
Use the following Statement objects to run different SQL statements:</p>
<ul><li> <a href="#jdbcst__jdbcststatement">Statement interface</a>: Runs a simple SQL statement
that has no parameters.</li>
<li><a href="jdbcpst.htm#jdbcpst">PreparedStatement interface</a> - Runs a precompiled SQL statement
that may or may not have IN parameters.</li>
<li><a href="jdbccst.htm#jdbccst">CallableStatement interface</a> - Runs a call to a database
stored procedure. A CallableStatement may or may not have IN, OUT, and INOUT
parameters.</li>
</ul>
<p>The Statement object allows you to submit multiple SQL commands as
a single group to a database through the use of batch support. You may improve
performance by using batch support because processing a group of operations
is typically faster than processing them one at a time. For more information
about using batch support, see <a href="rzahhjdbcenhancev5r2.htm#rzahhjdbcenhancev5r2__as400jdbcstatementmethods">Enhancements
to JDBC support</a>.</p>
<p>When using batch updates, typically you turn
off auto-commit. Turning off auto-commit allows your program to determine
whether to commit the transaction if an error occurs and not all of the commands
have executed. In JDBC 2.0 and later JDBC specifications, a Statement object
can keep track of a list of commands that can be successfully submitted and
executed together in a group. When this list of batch commands is executed
by the executeBatch() method, the commands are executed in the order in which
they were added to the list.</p>
<p>AS400JDBCStatement provides methods that
enable you to perform many actions, including the following:</p>
<ul><li><a href="javadoc/com/ibm/as400/access/AS400JDBCStatement.html#EXECUTE(JAVA.LANG.STRING)">Execute different kinds of statements</a></li>
<li>Retrieve the values for different parameters of the Statement object,
including: <ul><li><a href="javadoc/com/ibm/as400/access/AS400JDBCStatement.html#GETCONNECTION()">The connection</a></li>
<li>Any <a href="javadoc/com/ibm/as400/access/AS400JDBCStatement.html#GETGENERATEDKEYS()">auto-generated keys</a> created as a result
of executing the Statement</li>
<li>The <a href="javadoc/com/ibm/as400/access/AS400JDBCStatement.html#GETFETCHSIZE()">fetch size</a> and <a href="javadoc/com/ibm/as400/access/AS400JDBCStatement.html#GETFETCHDIRECTION()">fetch direction</a></li>
<li>The <a href="javadoc/com/ibm/as400/access/AS400JDBCStatement.html#GETMAXFIELDSIZE()">maximum field size</a> and <a href="javadoc/com/ibm/as400/access/AS400JDBCStatement.html#GETMAXROWS()">maximum row limit</a></li>
<li>The <a href="javadoc/com/ibm/as400/access/AS400JDBCStatement.html#GETRESULTSET()">current result set</a>, the <a href="javadoc/com/ibm/as400/access/AS400JDBCStatement.html#GETMORERESULTS(INT)">next result set</a>, the <a href="javadoc/com/ibm/as400/access/AS400JDBCStatement.html#GETRESULTSETTYPE()">type of result set</a>, the <a href="javadoc/com/ibm/as400/access/AS400JDBCStatement.html#GETRESULTSETCONCURRENCY()">result set concurrency</a>, and the <a href="javadoc/com/ibm/as400/access/AS400JDBCStatement.html#GETRESULTSETHOLDABILITY()">result set cursor holdability</a></li>
</ul>
</li>
<li><a href="javadoc/com/ibm/as400/access/AS400JDBCStatement.html#ADDBATCH(JAVA.LANG.STRING)">Add an SQL statement</a> to the current batch</li>
<li><a href="javadoc/com/ibm/as400/access/AS400JDBCStatement.html#EXECUTEBATCH()">Run the current batch</a> of SQL statements</li>
</ul>
</div>
<div class="section" id="jdbcst__jdbcststatement"><a name="jdbcst__jdbcststatement"><!-- --></a><h4 class="sectiontitle">Statement interface</h4><p>Use Connection.createStatement()
to create new Statement objects.</p>
<p>The following example shows how to
use a Statement object.</p>
<pre> // Connect to the server.
Connection c = DriverManager.getConnection("jdbc:as400://mySystem");
// Create a Statement object.
Statement s = c.createStatement();
// Run an SQL statement that creates
// a table in the database.
s.executeUpdate("CREATE TABLE MYLIBRARY.MYTABLE (NAME VARCHAR(20), ID INTEGER)");
// Run an SQL statement that inserts
// a record into the table.
s.executeUpdate("INSERT INTO MYLIBRARY.MYTABLE (NAME, ID) VALUES ('DAVE', 123)");
// Run an SQL statement that inserts
// a record into the table.
s.executeUpdate("INSERT INTO MYLIBRARY.MYTABLE (NAME, ID) VALUES ('CINDY', 456)");
// Run an SQL query on the table.
ResultSet rs = s.executeQuery("SELECT * FROM MYLIBRARY.MYTABLE");
// Close the Statement and the
// Connection.
s.close();
c.close();</pre>
</div>
</div>
</body>
</html>