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

178 lines
10 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="reference" />
<meta name="DC.Title" content="ResultSet class" />
<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="jdbcrs" />
<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>ResultSet class</title>
</head>
<body id="jdbcrs"><a name="jdbcrs"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">ResultSet class</h1>
<div><p></p>
<div class="section"><p>You can use a <a href="javadoc/com/ibm/as400/access/AS400JDBCResultSet.html#NAVBAR_TOP">ResultSet</a> object to access a table of data that was generated
by running a query. The table rows are retrieved in sequence. Within a row,
column values can be accessed in any order.</p>
<p>The data stored in ResultSet
is retrieved by using the various <a href="javadoc/com/ibm/as400/access/AS400JDBCResultSet.html#GETASCIISTREAM(INT)">get</a> methods, depending on the type of data being retrieved.
The <a href="javadoc/com/ibm/as400/access/AS400JDBCResultSet.html#NEXT()">next()</a> method is used to move to the next row.</p>
<p>ResultSet
allows you to <a href="rzahhjdbcenhancev5r2.htm#rzahhjdbcenhancev5r2__callablestatementmethods">get
and update columns by name</a>, although using the column index results
improves performance.</p>
</div>
<div class="section" id="jdbcrs__curmov"><a name="jdbcrs__curmov"><!-- --></a><h4 class="sectiontitle">Cursor movement</h4><p>A cursor, which is an
internal pointer, is used by a result set to point the row in the result
set that is being accessed by the Java™ program.</p>
<p>The performance of
the getRow() method has been improved. Before V5R2, using ResultSet.last(),
ResultSet.afterLast(), and ResultSet.absolute() with a negative value made
the current row number not available. The previous restrictions are lifted,
which makes the getRow() method fully functional.</p>
<p>JDBC 2.0 and later
JDBC specifications provide additional methods for accessing specific positions
within a database:</p>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" frame="border" border="1" rules="all"><thead align="left"><tr class="tablemainheaderbar"><th colspan="2" valign="top" id="d0e48">Scrollable cursor positions</th>
</tr>
</thead>
<tbody><tr><td valign="top" width="41.91919191919192%" headers="d0e48 "><p><a href="javadoc/com/ibm/as400/access/AS400JDBCResultSet.html#ABSOLUTE(INT)">absolute</a> <br />
<a href="javadoc/com/ibm/as400/access/AS400JDBCResultSet.html#AFTERLAST()">afterLast</a> <br />
<a href="javadoc/com/ibm/as400/access/AS400JDBCResultSet.html#BEFOREFIRST()">beforeFirst</a> <br />
<a href="javadoc/com/ibm/as400/access/AS400JDBCResultSet.html#FIRST()">first</a> <br />
<a href="javadoc/com/ibm/as400/access/AS400JDBCResultSet.html#GETROW()">getRow</a> <br />
<a href="javadoc/com/ibm/as400/access/AS400JDBCResultSet.html#ISAFTERLAST()">isAfterLast</a> <br />
<a href="javadoc/com/ibm/as400/access/AS400JDBCResultSet.html#ISBEFOREFIRST()">isBeforeFirst</a></p>
</td>
<td valign="top" width="58.080808080808076%" headers="d0e48 "><p><a href="javadoc/com/ibm/as400/access/AS400JDBCResultSet.html#ISFIRST()">isFirst</a> <br />
<a href="javadoc/com/ibm/as400/access/AS400JDBCResultSet.html#ISLAST()">isLast</a> <br />
<a href="javadoc/com/ibm/as400/access/AS400JDBCResultSet.html#LAST()">last</a> <br />
<a href="javadoc/com/ibm/as400/access/AS400JDBCResultSet.html#MOVETOCURRENTROW()">moveToCurrentRow</a> <br />
<a href="javadoc/com/ibm/as400/access/AS400JDBCResultSet.html#MOVETOINSERTROW()">moveToInsertRow</a> <br />
<a href="javadoc/com/ibm/as400/access/AS400JDBCResultSet.html#PREVIOUS()">previous</a> <br />
<a href="javadoc/com/ibm/as400/access/AS400JDBCResultSet.html#RELATIVE(INT)">relative</a></p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section" id="jdbcrs__scroll"><a name="jdbcrs__scroll"><!-- --></a><h4 class="sectiontitle">Scrolling capabilities</h4><p>If a result set
is created by executing a statement, you can move (scroll) backward (last-to-first)
or forward (first-to-last) through the rows in a table.</p>
<p>A result set
that supports this movement is called a scrollable result set. Scrollable
result sets also support absolute positioning. Absolute positioning allows
you to move directly to a row by specifying its position in the result set.</p>
<p>With
JDBC 2.0 and later JDBC specifications, you have two additional scrolling
capabilities available to use when working with the ResultSet class: scroll-insensitive
and scroll-sensitive result sets.</p>
<p>A scroll-insensitive result set is
not usually sensitive to changes that are made while it is open, while the
scroll-sensitive result set is sensitive to changes. </p>
<div class="note"><span class="notetitle">Note:</span> IBM<sup>®</sup> iSeries™ Server
only allows read-only access for scrollable insensitive cursors. IBM Toolbox for Java supports
a scroll-insensitive cursor if the result set concurrency is read-only. If
the result set type is specified as insensitive and the concurrency is specified
as updateable, the result set type changes to sensitive and issues a warning
to you.</div>
</div>
<div class="section" id="jdbcrs__concur"><a name="jdbcrs__concur"><!-- --></a><h4 class="sectiontitle">Updateable result sets</h4><p>In your application,
you can use result sets that use either read-only concurrency (no updates
can be made to the data) or updateable concurrency (allows updates to the
data and uses database write locks to control access to the same data item
by different transactions). In an updateable result set, rows can be updated,
inserted, and deleted. Numerous update methods are available for you to use
in your program, for example:</p>
<ul><li><a href="javadoc/com/ibm/as400/access/AS400JDBCResultSet.html#UPDATEASCIISTREAM(INT, JAVA.IO.INPUTSTREAM, INT)">Update ASCII stream</a></li>
<li><a href="javadoc/com/ibm/as400/access/AS400JDBCResultSet.html#UPDATEBIGDECIMAL(INT, JAVA.MATH.BIGDECIMAL)">Update Big Decimal</a></li>
<li><a href="javadoc/com/ibm/as400/access/AS400JDBCResultSet.html#UPDATEBINARYSTREAM(INT, JAVA.IO.INPUTSTREAM, INT)">Update binary stream</a></li>
</ul>
<p>See <a href="javadoc/com/ibm/as400/access/AS400JDBCResultSet.html#METHOD_SUMMARY"> Method Summary</a> for a complete listing of the update
methods available through the ResultSet interface.</p>
</div>
<div class="section" id="jdbcrs__updex"><a name="jdbcrs__updex"><!-- --></a><h4 class="sectiontitle">Example: Updatable result sets</h4><p>The following
example shows how to use a result set that allows updates to the data (update
concurrency) and allows changes to be made to the result set while it is open
(scroll sensitive).</p>
<pre> // Connect to the server.
Connection c = DriverManager.getConnection("jdbc:as400://mySystem");
// Create a Statement object. Set the result set
// concurrency to updatable.
Statement s = c.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
// Run a query. The result is placed
// in a ResultSet object.
ResultSet rs = s.executeQuery ("SELECT NAME,ID FROM MYLIBRARY.MYTABLE FOR UPDATE");
// Iterate through the rows of the ResultSet.
// As we read the row, we will update it with
// a new ID.
int newId = 0;
while (rs.next ())
{
// Get the values from the ResultSet.
// The first value is a string, and
// the second value is an integer.
String name = rs.getString("NAME");
int id = rs.getInt("ID");
System.out.println("Name = " + name);
System.out.println("Old id = " + id);
// Update the id with a new integer.
rs.updateInt("ID", ++newId);
// Send the updates to the server.
rs.updateRow ();
System.out.println("New id = " + newId);
}
// Close the Statement and the
// Connection.
s.close();
c.close();</pre>
</div>
<div class="section" id="jdbcrs__resultsetmetadata"><a name="jdbcrs__resultsetmetadata"><!-- --></a><h4 class="sectiontitle">ResultSetMetaData</h4><p>The <a href="javadoc/com/ibm/as400/access/AS400JDBCResultSetMetaData.html#NAVBAR_TOP"> ResultSetMetaData</a> interface determines the types and
properties of the columns in a ResultSet.</p>
<p> When connecting to a server
running i5/OS™ V5R2
or later, using the <a href="jdbcproperties.htm#jdbcproperties__other">extended
metadata property</a> enables you to increase the accuracy of the following
ResultSetMetaData methods: </p>
<ul><li>getColumnLabel(int)</li>
<li>isReadOnly(int)</li>
<li>isSearchable(int)</li>
<li>isWriteable(int)</li>
</ul>
<p>Additionally, setting this property to true enables support for the
ResultSetMetaData.getSchemaName(int) method. Be aware that using the extended
metadata property may degrade performance because it requires retrieving more
information from the server. </p>
</div>
</div>
</body>
</html>