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

79 lines
4.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="reference" />
<meta name="DC.Title" content="PreparedStatement interface" />
<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="jdbcpst" />
<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>PreparedStatement interface</title>
</head>
<body id="jdbcpst"><a name="jdbcpst"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">PreparedStatement interface</h1>
<div><p></p>
<div class="section"><p>You can use a <a href="javadoc/com/ibm/as400/access/AS400JDBCPreparedStatement.html#NAVBAR_TOP"> PreparedStatement</a> object when an SQL statement
is going to be run many times. An SQL statement can be precompiled. A "prepared."
statement is an SQL statement that has been precompiled. This approach is
more efficient than running the same statement multiple times using a Statement
object, which compiles the statement each time it is run. In addition, the
SQL statement contained in a PreparedStatement object may have one or more
IN parameters. Use Connection.prepareStatement() to create PreparedStatement
objects.</p>
<p>The PreparedStatement 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>
</div>
<div class="section"><h4 class="sectiontitle">Example: Using PreparedStatement</h4><p>The following example
shows how to use the PreparedStatement interface.</p>
<pre> // Connect to the server.
Connection c = DriverManager.getConnection("jdbc:as400://mySystem");
// Create the PreparedStatement
// object. It precompiles the
// specified SQL statement. The
// question marks indicate where
// parameters must be set before the
// statement is run.
PreparedStatement ps = c.prepareStatement("INSERT INTO MYLIBRARY.MYTABLE (NAME, ID) VALUES (?, ?)");
// Set parameters and run the
// statement.
ps.setString(1, "JOSH");
ps.setInt(2, 789);
ps.executeUpdate();
// Set parameters and run the
// statement again.
ps.setString(1, "DAVE");
ps.setInt(2, 456);
ps.executeUpdate();
// Close PreparedStatement and the
// Connection.
ps.close();
c.close();</pre>
</div>
</div>
</body>
</html>