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.
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.
Example: DB2JdbcRowSet events
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 < 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"); } }