ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzaha_5.4.0.1/db2cafea.htm

370 lines
15 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="Other DB2CachedRowSet features" />
<meta name="abstract" content="In addition to working like a ResultSet as several examples have shown, the DB2CachedRowSet class has some additional functionality that makes it more flexible to use. Methods are provided for turning either the entire Java Database Connectivity (JDBC) RowSet or just a portion of it into a Java collection. Moreover, because of their disconnected nature, DB2CachedRowSets do not have a strict one-to-one relationship with ResultSets." />
<meta name="description" content="In addition to working like a ResultSet as several examples have shown, the DB2CachedRowSet class has some additional functionality that makes it more flexible to use. Methods are provided for turning either the entire Java Database Connectivity (JDBC) RowSet or just a portion of it into a Java collection. Moreover, because of their disconnected nature, DB2CachedRowSets do not have a strict one-to-one relationship with ResultSets." />
<meta name="DC.Relation" scheme="URI" content="db2cache.htm" />
<meta name="DC.Relation" scheme="URI" content="db2cause.htm" />
<meta name="DC.Relation" scheme="URI" content="crdb2cac.htm" />
<meta name="DC.Relation" scheme="URI" content="accdb2ca.htm" />
<meta name="DC.Relation" scheme="URI" content="chgdb2ca.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="db2cafea" />
<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>Other DB2CachedRowSet features</title>
</head>
<body id="db2cafea"><a name="db2cafea"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Other DB2CachedRowSet features</h1>
<div><p>In addition to working like a ResultSet as several examples have
shown, the DB2CachedRowSet class has some additional functionality that makes
it more flexible to use. Methods are provided for turning either the entire Java™ Database
Connectivity (JDBC) RowSet or just a portion of it into a Java collection.
Moreover, because of their disconnected nature, DB2CachedRowSets do not have
a strict one-to-one relationship with ResultSets.</p>
<p>In addition to working like a ResultSet as several examples have shown,
the DB2CachedRowSet class has some additional functionality that makes it
more flexible to use. Methods are provided for turning either the entire Java Database
Connectivity (JDBC) RowSet or just a portion of it into a Java collection.
Moreover, because of their disconnected nature, DB2CachedRowSets do not have
a strict one-to-one relationship with ResultSets.</p>
<p>With the methods provided by DB2CachedRowSet, you can perform the following
tasks:</p>
<div class="section"><h4 class="sectiontitle">Obtain collections from DB2CachedRowSets</h4><p>There are
three methods that return some form of a collection from a DB2CachedRowSet
object. They are the following:</p>
<ul><li><strong>toCollection</strong> returns an ArrayList (that is, one entry for each
row) of vectors (that is, one entry for each column).</li>
<li><strong>toCollection(int columnIndex)</strong> returns a vector containing the value
for each row from the given column.</li>
<li><strong>getColumn(int columnIndex)</strong> returns an array containing the value
for each column for a given column.</li>
</ul>
<p>The major difference between toCollection(int columnIndex) and getColumn(int
columnIndex) is that the getColumn method can return an array of primitive
types. Therefore, if columnIndex represents a column that has integer data,
an integer array is returned and not an array containing java.lang.Integer
objects.</p>
<p>The following example shows how you can use these methods.</p>
<p><strong>Example:</strong> Obtain
collections from DB2CachedRowSets</p>
<div class="note"><span class="notetitle">Note:</span> Read the <a href="codedisclaimer.htm">Code
example disclaimer</a> for important legal information.</div>
<pre>import java.sql.*;
import javax.sql.*;
import com.ibm.db2.jdbc.app.DB2CachedRowSet;
import java.util.*;
public class RowSetSample4
{
public static void main(String args[])
{
// Register the driver.
try {
Class.forName("com.ibm.db2.jdbc.app.DB2Driver");
}
catch (ClassNotFoundException ex) {
System.out.println("ClassNotFoundException: " +
ex.getMessage());
// No need to go any further.
System.exit(1);
}
try {
Connection conn = DriverManager.getConnection("jdbc:db2:*local");
Statement stmt = conn.createStatement();
// Clean up previous runs
try {
stmt.execute("drop table cujosql.test_table");
}
catch (SQLException ex) {
System.out.println("Caught drop table: " + ex.getMessage());
}
// Create test table
stmt.execute("Create table cujosql.test_table (col1 smallint, col2 smallint)");
System.out.println("Table created.");
// Insert some test rows
for (int i = 0; i &lt; 10; i++) {
stmt.execute("insert into cujosql.test_table values (" + i + ", " + (i + 100) + ")");
}
System.out.println("Rows inserted");
ResultSet rs = stmt.executeQuery("select * from cujosql.test_table");
System.out.println("Query executed");
// Create a new rowset and populate it...
DB2CachedRowSet crs = new DB2CachedRowSet();
crs.populate(rs);
System.out.println("RowSet populated.");
conn.close();
System.out.println("RowSet is detached...");
System.out.println("Test the toCollection() method");
Collection collection = crs.toCollection();
ArrayList map = (ArrayList) collection;
System.out.println("size is " + map.size());
Iterator iter = map.iterator();
int row = 1;
while (iter.hasNext()) {
System.out.print("row [" + (row++) + "]: \t");
Vector vector = (Vector)iter.next();
Iterator innerIter = vector.iterator();
int i = 1;
while (innerIter.hasNext()) {
System.out.print(" [" + (i++) + "]=" + innerIter.next() + "; \t");
}
System.out.println();
}
System.out.println("Test the toCollection(int) method");
collection = crs.toCollection(2);
Vector vector = (Vector) collection;
iter = vector.iterator();
while (iter.hasNext()) {
System.out.println("Iter: Value is " + iter.next());
}
System.out.println("Test the getColumn(int) method");
Object values = crs.getColumn(2);
short[] shorts = (short [])values;
for (int i =0; i &lt; shorts.length; i++) {
System.out.println("Array: Value is " + shorts[i]);
}
}
catch (SQLException ex) {
System.out.println("SQLException: " + ex.getMessage());
}
}
}</pre>
</div>
<div class="section"><h4 class="sectiontitle">Create copies of RowSets</h4><p>The createCopy method creates
a copy of the DB2CachedRowSet. All the data associated with the RowSet is
replicated along with all control structures, properties, and status flags.</p>
<p>The
following example shows how you can use this method.</p>
<p><strong>Example:</strong> Create
copies of RowSets</p>
<div class="note"><span class="notetitle">Note:</span> Read the <a href="codedisclaimer.htm">Code
example disclaimer</a> for important legal information.</div>
<pre>import java.sql.*;
import javax.sql.*;
import com.ibm.db2.jdbc.app.*;
import java.io.*;
public class RowSetSample5
{
public static void main(String args[])
{
// Register the driver.
try {
Class.forName("com.ibm.db2.jdbc.app.DB2Driver");
}
catch (ClassNotFoundException ex) {
System.out.println("ClassNotFoundException: " +
ex.getMessage());
// No need to go any further.
System.exit(1);
}
try {
Connection conn = DriverManager.getConnection("jdbc:db2:*local");
Statement stmt = conn.createStatement();
// Clean up previous runs
try {
stmt.execute("drop table cujosql.test_table");
}
catch (SQLException ex) {
System.out.println("Caught drop table: " + ex.getMessage());
}
// Create test table
stmt.execute("Create table cujosql.test_table (col1 smallint)");
System.out.println("Table created.");
// Insert some test rows
for (int i = 0; i &lt; 10; i++) {
stmt.execute("insert into cujosql.test_table values (" + i + ")");
}
System.out.println("Rows inserted");
ResultSet rs = stmt.executeQuery("select col1 from cujosql.test_table");
System.out.println("Query executed");
// Create a new rowset and populate it...
DB2CachedRowSet crs = new DB2CachedRowSet();
crs.populate(rs);
System.out.println("RowSet populated.");
conn.close();
System.out.println("RowSet is detached...");
System.out.println("Now some new RowSets from one.");
DB2CachedRowSet crs2 = crs.createCopy();
DB2CachedRowSet crs3 = crs.createCopy();
System.out.println("Change the second one to be negated values");
crs2.beforeFirst();
while (crs2.next()) {
short value = crs2.getShort(1);
value = (short)-value;
crs2.updateShort(1, value);
crs2.updateRow();
}
crs.beforeFirst();
crs2.beforeFirst();
crs3.beforeFirst();
System.out.println("Now look at all three of them again");
while (crs.next()) {
crs2.next();
crs3.next();
System.out.println("Values: crs: " + crs.getShort(1) + ", crs2: " + crs2.getShort(1) +
", crs3: " + crs3.getShort(1));
}
}
catch (Exception ex) {
System.out.println("SQLException: " + ex.getMessage());
ex.printStackTrace();
}
}
}</pre>
</div>
<div class="section"><h4 class="sectiontitle">Create shares for RowSets</h4><p>The createShared method
creates a new RowSet object with high-level status information and allows
two RowSet objects to share the same underlying physical data.</p>
<p>The following
example shows how you can use this method.</p>
<p><strong>Example:</strong> Create shares
of RowSets</p>
<div class="note"><span class="notetitle">Note:</span> Read the <a href="codedisclaimer.htm">Code example
disclaimer</a> for important legal information.</div>
<pre>import java.sql.*;
import javax.sql.*;
import com.ibm.db2.jdbc.app.*;
import java.io.*;
public class RowSetSample5
{
public static void main(String args[])
{
// Register the driver.
try {
Class.forName("com.ibm.db2.jdbc.app.DB2Driver");
}
catch (ClassNotFoundException ex) {
System.out.println("ClassNotFoundException: " +
ex.getMessage());
// No need to go any further.
System.exit(1);
}
try {
Connection conn = DriverManager.getConnection("jdbc:db2:*local");
Statement stmt = conn.createStatement();
// Clean up previous runs
try {
stmt.execute("drop table cujosql.test_table");
}
catch (SQLException ex) {
System.out.println("Caught drop table: " + ex.getMessage());
}
// Create test table
stmt.execute("Create table cujosql.test_table (col1 smallint)");
System.out.println("Table created.");
// Insert some test rows
for (int i = 0; i &lt; 10; i++) {
stmt.execute("insert into cujosql.test_table values (" + i + ")");
}
System.out.println("Rows inserted");
ResultSet rs = stmt.executeQuery("select col1 from cujosql.test_table");
System.out.println("Query executed");
// Create a new rowset and populate it...
DB2CachedRowSet crs = new DB2CachedRowSet();
crs.populate(rs);
System.out.println("RowSet populated.");
conn.close();
System.out.println("RowSet is detached...");
System.out.println("Test the createShared functionality (create 2 shares)");
DB2CachedRowSet crs2 = crs.createShared();
DB2CachedRowSet crs3 = crs.createShared();
System.out.println("Use the original to update value 5 of the table");
crs.absolute(5);
crs.updateShort(1, (short)-5);
crs.updateRow();
crs.beforeFirst();
crs2.afterLast();
System.out.println("Now move the cursors in opposite directions of the same data.");
while (crs.next()) {
crs2.previous();
crs3.next();
System.out.println("Values: crs: " + crs.getShort(1) + ", crs2: " + crs2.getShort(1) +
", crs3: " + crs3.getShort(1));
}
crs.close();
crs2.close();
crs3.close();
}
catch (Exception ex) {
System.out.println("SQLException: " + ex.getMessage());
ex.printStackTrace();
}
}
}</pre>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="db2cache.htm" title="The DB2CachedRowSet object is a disconnected RowSet, meaning that it can be used without being connected to the database. Its implementation adheres closely to the description of a CachedRowSet. The DB2CachedRowSet is a container for rows of data from a ResultSet. The DB2CachedRowSet holds all its own data so it does not need to maintain a connection to the database other than explicitly while reading or writing data to the database.">DB2CachedRowSet</a></div>
</div>
<div class="relconcepts"><strong>Related concepts</strong><br />
<div><a href="db2cause.htm" title="Because the DB2CachedRowSet object can be disconnected and serialized, it is useful in environments where it is not always practical to run a full JDBC driver (for example, on Personal Digital Assistants (PDAs) and Java-enabled cell phones).">Use DB2CachedRowSet</a></div>
</div>
<div class="relref"><strong>Related reference</strong><br />
<div><a href="crdb2cac.htm" title="There are several ways to place data into a DB2CachedRowSet.">Create and populate a DB2CachedRowSet</a></div>
<div><a href="accdb2ca.htm" title="This topic provides information about accessing DB2CachedRowSet data and various cursor manipulation functions.">Access DB2CachedRowSet data and cursor manipulation</a></div>
<div><a href="chgdb2ca.htm" title="This topic provides information about making changes to rows in a DB2CachedRowSet and then updating the underlying database.">Change DB2CachedRowSet data and reflect changes back to the data source</a></div>
</div>
</div>
</body>
</html>