Row Set

This chapter describes functions related to row sets provided by tbJDBC.

A row set is an object that includes a set of row data. It can be accessed using the javax.sql.RowSet object.

There are three kinds of row sets.

  • Cached Row Set(tbJDBC currently provides this row set only.)

  • JDBC Row Set

  • Web Row Set

Row Set Listener

Multiple listeners can be registered to a row set. To register a listener, the addRowSetListener() method is used, and to delete a listener, the removeRowSetListener() method is used. The listener must be implemented using the javax.sql.RowSetListener interface.

The RowSetListener interface provides the following events.

Event
Description

cursorMoved

Occurs whenever a row is moved using the next() or previous() method.

rowChanged

Occurs when a new row is added, or an existing row is modified or deleted.

rowSetChanged

Occurs when the entire row set is created or modified.

The events of the RowSetListener interface are used as follows:

  1. Create a listener class.

public class MyListener implements RowSetListener
{
    public void cursorMoved(RowSetEvent event) {
        // do work
    }
    public void rowChanged(RowSetEvent event) {
        // do work
    }
    public void rowSetChanged(RowSetEvent event) {
        // do work
    }
}

  1. Register the listener to a list of RowSet objects.

MyListener mListener = new MyListener();
rowset.addRowSetListener(mListener);


Cached Row Set

A Cached Row Set is a row set that stores all rows in a cache, and is not connected to the database. In tbJDBC, a Cached Row Set is provided as the TbCachedRowSet class.

Creating a RowSet Object

A RowSet object is initialized using the execute() method, and can then be used in the same way as a java.sql.ResultSet object.

This section describes two ways to create a RowSet object.

Creating a RowSet Object Using a Query

The following example illustrates how to create a RowSet object using a query.

  1. Create a TbCachedRowSet.

TbCachedRowSet rowset = new TbCachedRowSet();

  1. Specify the URL, user name, password, and query, and then create a RowSet object by calling the execute() method.

rowset.setUrl("jdbc:tibero:thin:@localhost:8629:dbsvr"); 
rowset.setUsername("tibero"); 
rowset.setPassword("tmax");
rowset.setCommand("SELECT * FROM emp"); 
rowset.execute();

Creating a RowSet Object using a Result Set

The following example shows how to create a RowSet object using an existing result set.

  1. Create a TbCachedRowSet object.

TbCachedRowSet rowset = new TbCachedRowSet();

  1. Create a RowSet object by calling the populate() method with a result set object (rset).

ResultSet rset = pstmt.executeQuery();
rowset.populate(rset);

Searching Rows

It is possible to search rows by moving forward or backward with a RowSet object.

The following is an example.

rowset.beforeFirst(); 
while (rowset.next()) {
    System.out.println(rowset.getString(1));
}

rowset.afterLast();
while (rowset.previous()) {
    System.out.println(rowset.getString(1));
}

It is also possible to insert, delete, or modify rows. The acceptChanges() method must be called to commit any changes.

The following is an example.

rowset.absolute(5);
rowset.moveToInsertRow(); 
rowset.updateString(1, "tibero"); 
rowset.insertRow(); 
rowset.acceptChanges();

Restrictions

The restrictions on row sets are the same as those for updatable result sets.

Last updated