126 lines
8.1 KiB
HTML
126 lines
8.1 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="Statements" />
|
||
|
<meta name="abstract" content="A Statement object is used for processing a static SQL statement and obtaining the results produced by it. Only one ResultSet for each Statement object can be open at a time. All statement methods that process an SQL statement implicitly close a statement's current ResultSet if an open one exists." />
|
||
|
<meta name="description" content="A Statement object is used for processing a static SQL statement and obtaining the results produced by it. Only one ResultSet for each Statement object can be open at a time. All statement methods that process an SQL statement implicitly close a statement's current ResultSet if an open one exists." />
|
||
|
<meta name="DC.Relation" scheme="URI" content="statetyp.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="prepstat.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="callable.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="stateex.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="statemnt" />
|
||
|
<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>Statements</title>
|
||
|
</head>
|
||
|
<body id="statemnt"><a name="statemnt"><!-- --></a>
|
||
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
||
|
<h1 class="topictitle1">Statements</h1>
|
||
|
<div><p>A Statement object is used for processing a static SQL statement
|
||
|
and obtaining the results produced by it. Only one ResultSet for each Statement
|
||
|
object can be open at a time. All statement methods that process an SQL statement
|
||
|
implicitly close a statement's current ResultSet if an open one exists.</p>
|
||
|
<div class="section"><h4 class="sectiontitle">Create statements</h4><p>Statement objects are created
|
||
|
from Connection objects with the createStatement method. For example, assuming
|
||
|
a Connection object named conn already exists, the following line of code
|
||
|
creates a Statement object for passing SQL statements to the database:</p>
|
||
|
<blockquote><pre>Statement stmt = conn.createStatement();</pre>
|
||
|
</blockquote>
|
||
|
</div>
|
||
|
<div class="section"><h4 class="sectiontitle">Specify ResultSet characteristics</h4><p>The characteristics
|
||
|
of ResultSets are associated with the statement that eventually creates them.
|
||
|
The Connection.createStatement method allows you to specify these ResultSet
|
||
|
characteristics. The following are some examples of valid calls to the createStatement
|
||
|
method:</p>
|
||
|
<p><strong>Example:</strong> The createStatement method</p>
|
||
|
<p><strong>Note:</strong> Read
|
||
|
the <a href="codedisclaimer.htm">Code example disclaimer</a> for important
|
||
|
legal information.</p>
|
||
|
<blockquote><pre>// The following is new in JDBC 2.0
|
||
|
|
||
|
Statement stmt2 = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
|
||
|
ResultSet.CONCUR_UPDATEABLE);
|
||
|
|
||
|
// The following is new in JDBC 3.0
|
||
|
|
||
|
Statement stmt3 = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
|
||
|
ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSOR_OVER_COMMIT);</pre>
|
||
|
</blockquote>
|
||
|
<p>For
|
||
|
more information about these characteristics, see <a href="rsltsets.htm">ResultSets</a>.</p>
|
||
|
</div>
|
||
|
<div class="section"><h4 class="sectiontitle">Process statements</h4><p>Processing SQL statements with
|
||
|
a Statement object is accomplished with the executeQuery(), executeUpdate(),
|
||
|
and execute() methods.</p>
|
||
|
</div>
|
||
|
<div class="section"><h4 class="sectiontitle">Return results from SQL queries</h4><p>If an SQL query
|
||
|
statement returning a ResultSet object is to be processed, the executeQuery()
|
||
|
method should be used. You can refer to the <a href="stateex.htm">example</a> program
|
||
|
that uses a Statement object's executeQuery method to obtain a ResultSet.</p>
|
||
|
<p><strong>Note:</strong> If
|
||
|
an SQL statement processed with executeQuery does not return a ResultSet,
|
||
|
an SQLException is thrown.</p>
|
||
|
</div>
|
||
|
<div class="section"><h4 class="sectiontitle">Return update counts for SQL Statements</h4><p>If the SQL
|
||
|
is known to be a Data Definition Language (DDL) statement or a Data Manipulation
|
||
|
Language (DML) statement returning an update count, the executeUpdate() method
|
||
|
should be used. The <a href="stateex.htm">StatementExample</a> program
|
||
|
uses a Statement object's executeUpdate method.</p>
|
||
|
</div>
|
||
|
<div class="section"><h4 class="sectiontitle">Process SQL statements where the expected return is unknown</h4><p>If
|
||
|
the SQL statement type is not known, the execute method should be used. Once
|
||
|
this method has been processed, the JDBC driver can tell the application what
|
||
|
types of results the SQL statement has generated through API calls. The execute
|
||
|
method returns true if the result is at least one ResultSet and false if the
|
||
|
return value is an update count. Given this information, applications can
|
||
|
use the statement method's getUpdateCount or getResultSet to retrieve the
|
||
|
return value from processing the SQL statement. The StatementExecute program
|
||
|
uses the execute method on a Statement object. This program expects a parameter
|
||
|
to be passed that is an SQL statement. Without looking at the text of the
|
||
|
SQL that you provide, the program processes the statement and determines information
|
||
|
about what was processed.</p>
|
||
|
<p><strong>Note:</strong> Calling the getUpdateCount method
|
||
|
when the result is a ResultSet returns -1. Calling the getResultSet method
|
||
|
when the result is an update count returns null.</p>
|
||
|
</div>
|
||
|
<div class="section"><h4 class="sectiontitle">The cancel method</h4><p>The methods of the native JDBC
|
||
|
driver are synchronized to prevent two threads running against the same object
|
||
|
from corrupting the object. An exception is the cancel method. The cancel
|
||
|
method can be used by one thread to stop a long running SQL statement on another
|
||
|
thread for the same object. The native JDBC driver cannot force the thread
|
||
|
to stop doing work; it can only request that it stop whatever task it was
|
||
|
doing. For this reason, it still takes time for a cancelled statement to stop.
|
||
|
The cancel method can be used to halt runaway SQL queries on the system. </p>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div>
|
||
|
<ul class="ullinks">
|
||
|
<li class="ulchildlink"><strong><a href="stateex.htm">Example: Use the Statement object's executeUpdate method</a></strong><br />
|
||
|
This is an example of how to use the Statement object's executeUpdate method.</li>
|
||
|
</ul>
|
||
|
|
||
|
<div class="familylinks">
|
||
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="statetyp.htm" title="The Statement interface and its PreparedStatement and CallableStatement subclasses are used to process structured query language (SQL) commands against the database. SQL statements cause the generation of ResultSet objects.">Statement types</a></div>
|
||
|
</div>
|
||
|
<div class="relconcepts"><strong>Related concepts</strong><br />
|
||
|
<div><a href="prepstat.htm" title="PreparedStatements extend the Statement interface and provide support for adding parameters to SQL statements.">PreparedStatements</a></div>
|
||
|
<div><a href="callable.htm" title="The CallableStatement interface extends PreparedStatement and provides support for output and input/output parameters. The CallableStatement interface also has support for input parameters that is provided by the PreparedStatement interface.">CallableStatements</a></div>
|
||
|
</div>
|
||
|
</div>
|
||
|
</body>
|
||
|
</html>
|