What does filtered row set? in JDBC

The FilteredRowSet (javax.sql.FilteredRowSet) is a disconnected rowset, which enables you to retrieve a custom view of database data using a filter (filtering logic) that takes a snapshot of, but doesn’t alter, your table. The FilteredRowset interface extends the WebRowSet interface, which in turn extends the javax.sql.Rowset interface. The FilteredRowSet enables you to narrow down the number of rows in a disconnected object based on filtering logic you provide without requiring an ongoing connection to your database. How do you filter rowsets? You have at least two options:

  • Use the WHERE clause in a SQL’s SELECT statement (this option can be applied once and cannot be repeated for a disconnected rowset object). This might be a good option for connected rowsets such as JdbcRowSet—it will not work for disconnected rowsets.
  • Use the Predicate (javax.sql.rowset.Predicate) interface (this option can be applied many times to a disconnected object). You may implement the Predicate interface for filtering your desired rowsets dynamically.

The following is taken from the J2SE 5.0 documentation:

There are occasions when a RowSet object has a need to provide a degree of filtering to its contents. One possible solution is to provide a query language for all standard RowSet implementations; however, this is an impractical approach for lightweight components such as disconnected RowSet objects. The FilteredRowSet interface seeks to address this need without supplying a heavyweight query language along with the processing that such a query language would require. A JDBC FilteredRowSet standard implementation implements the RowSet interfaces and extends the CachedRowSet class. The CachedRowSet class provides a set of protected cursor manipulation methods, which a FilteredRowSet implementation can override to supply filtering support.

What is the Predicate interface? According to J2SE 5.0 documentation:

The Predicate interface is a standard interface that applications can implement to define the filter they wish to apply to a FilteredRowSet object. A FilteredRowSet object consumes implementations of this interface and enforces the constraints defined in the implementation of the method evaluate. A FilteredRowSet object enforces the filter constraints in a bi-directional manner: It outputs only rows that are within the constraints of the filter; and conversely, it inserts, modifies, or updates only rows that are within the constraints of the filter.

The Predicate interface has the following methods:

  • boolean evaluate(Object value, int column): This method is called by a FilteredRowSet object to check whether the value lies within the filtering criterion (or criteria if multiple constraints exist) set using the setFilter() method.
  • boolean evaluate(Object value, String columnName): This method is called by the FilteredRowSet object to check whether the value lies within the filtering criteria set using the setFilter method.
  • boolean evaluate(RowSet rs): This method is typically called a FilteredRowSet object’s internal methods (not public) that control the RowSet object’s cursor moving from one row to the next.

How does a FilteredRowSet object use a Predicate object? The FilteredRowSet interface has two specific methods for dealing with Predicate objects:

  • Predicate getFilter(): Retrieves the active filter for this FilteredRowSet object
  • void setFilter(Predicate p): Applies the given Predicate object to this FilteredRowSet object

With some basic examples, I will show you how to use a FilteredRowSet object and set a simple filter for filtering rowsets.

MySQL Database Setup

Implementing Predicate

Using Predicate with a FilteredRowSet

Using a FilteredRowSet with a MySQL Database

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JDBC Topics