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

180 lines
8.6 KiB
HTML
Raw Permalink 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="DB2JdbcRowSet events" />
<meta name="abstract" content="All RowSet implementations support event handling for situations that are of interest to other components. This support allows application components to &#34;talk&#34; to each other when events happen to them. For example, updating a database row through a RowSet can cause a Graphical User Interface (GUI) table shown to you to update itself." />
<meta name="description" content="All RowSet implementations support event handling for situations that are of interest to other components. This support allows application components to &#34;talk&#34; to each other when events happen to them. For example, updating a database row through a RowSet can cause a Graphical User Interface (GUI) table shown to you to update itself." />
<meta name="DC.Relation" scheme="URI" content="db2jdbcr.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="db2rowev" />
<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>DB2JdbcRowSet events</title>
</head>
<body id="db2rowev"><a name="db2rowev"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">DB2JdbcRowSet events</h1>
<div><p>All RowSet implementations support event handling for situations
that are of interest to other components. This support allows application
components to "talk" to each other when events happen to them. For example,
updating a database row through a RowSet can cause a Graphical User Interface
(GUI) table shown to you to update itself.</p>
<div class="section"><p>In the following example, the main method does the update to the
RowSet and is your core application. The listener is part of your wireless
server used by your disconnected clients in the field. It is possible to tie
these two aspects of a business together without getting the code for the
two processes intermingled. While the event support of RowSets was designed
primarily for updating GUIs with database data, it works perfectly for this
type of application problem.</p>
<p><strong>Example:</strong> DB2JdbcRowSet events</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.DB2JdbcRowSet;
public class RowSetEvents {
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 {
// Obtain the JDBC Connection and Statement needed to set
// up this example.
Connection conn = DriverManager.getConnection("jdbc:db2:*local");
Statement stmt = conn.createStatement();
// Clean up any previous runs.
try {
stmt.execute("drop table cujosql.test_table");
} catch (SQLException ex) {
System.out.println("Caught drop table: " + ex.getMessage());
}
// Create the test table
stmt.execute("Create table cujosql.test_table (col1 smallint)");
System.out.println("Table created.");
// Populate the table with data.
for (int i = 0; i &lt; 10; i++) {
stmt.execute("insert into cujosql.test_table values (" + i + ")");
}
System.out.println("Rows inserted");
// Remove the setup objects.
stmt.close();
conn.close();
// Create a new rowset and set the properties need to
// process it.
DB2JdbcRowSet jrs = new DB2JdbcRowSet();
jrs.setUrl("jdbc:db2:*local");
jrs.setCommand("select col1 from cujosql.test_table");
jrs.setConcurrency(ResultSet.CONCUR_UPDATEABLE);
// Give the RowSet object a listener. This object handles
// special processing when certain actions are done on
// the RowSet.
jrs.addRowSetListener(new MyListener());
// Process the RowSet to provide access to the database data.
jrs.execute();
// Cause a few cursor change events. These events cause the cursorMoved
// method in the listener object to get control.
jrs.next();
jrs.next();
jrs.next();
// Cause a row change event to occur. This event causes the rowChanged method
// in the listener object to get control.
jrs.updateShort(1, (short)6);
jrs.updateRow();
// Finally, cause a RowSet change event to occur. This causes the
// rowSetChanged method in the listener object to get control.
jrs.execute();
// When completed, close the RowSet.
jrs.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
/**
* This is an example of a listener. This example prints messages that show
* how control flow moves through the application and offers some
* suggestions about what might be done if the application were fully implemented.
*/
class MyListener
implements RowSetListener {
public void cursorMoved(RowSetEvent rse) {
System.out.println("Event to do: Cursor position changed.");
System.out.println(" For the remote system, do nothing ");
System.out.println(" when this event happened. The remote view of the data");
System.out.println(" could be controlled separately from the local view.");
try {
DB2JdbcRowSet rs = (DB2JdbcRowSet) rse.getSource();
System.out.println("row is " + rs.getRow() + ". \n\n");
} catch (SQLException e) {
System.out.println("To do: Properly handle possible problems.");
}
}
public void rowChanged(RowSetEvent rse) {
System.out.println("Event to do: Row changed.");
System.out.println(" Tell the remote system that a row has changed. Then,");
System.out.println(" pass all the values only for that row to the ");
System.out.println(" remote system.");
try {
DB2JdbcRowSet rs = (DB2JdbcRowSet) rse.getSource();
System.out.println("new values are " + rs.getShort(1) + ". \n\n");
} catch (SQLException e) {
System.out.println("To do: Properly handle possible problems.");
}
}
public void rowSetChanged(RowSetEvent rse) {
System.out.println("Event to do: RowSet changed.");
System.out.println(" If there is a remote RowSet already established, ");
System.out.println(" tell the remote system that the values it ");
System.out.println(" has should be thrown out. Then, pass all ");
System.out.println(" the current values to it.\n\n");
}
}</pre>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="db2jdbcr.htm" title="The DB2JdbcRowSet is a connected RowSet, meaning that it can only be used with the support of an underlying Connection object, PreparedStatement object, or ResultSet object. Its implementation adheres closely to the description of a JdbcRowSet.">DB2JdbcRowSet</a></div>
</div>
</div>
</body>
</html>