Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Master the FilteredRowSet Interface for Disconnected Data Retrieval : Page 2

The FilteredRowSet interface, added to version 1.5 of Java, lets you retrieve a custom view of database data using a filter that takes a snapshot, but doesn't alter, your table. Best of all, it does this without a persistent database connection.




Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

The Predicate Interface
In order to filter data in a disconnected RowSet, one must provide a filtering criterion. This criterion is dependent on the javax.sql.rowset.Predicate interface. At the time this article was published, Java did not ship with a default implementation of the Predicate interface. Accordingly, the onus of implementation is placed on developers.

In order to implement the Predicate interface, you need to implement the following methods (pulled directly from the javax.sql.rowset.Predicate API).

Table 2. Methods for Implementing the Predicate Interface

boolean evaluate(Object value, int column) This method is called by a FilteredRowSet object to check whether the value lies between 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 between the filtering criteria set using the setFilter method.
boolean evaluate(RowSet rs) This method is typically called a FilteredRowSet object internal method (not public) that control the RowSet object's cursor moving from one row to the next.

For demonstration purposes, I'll create a simple range filter. This filter checks if a given column value falls between two other values. I've initialized the Predicate implementation object with the values for the filter. My filtering class is RangeFilter.java, which you can find in the downloadable zip file associated with this article. It is a good idea to study its structure alongside my scrutiny of the class.

You can build any filter you like. I chose a range evaluation, but you can create a Predicate implementation that grabs all names beginning with a certain letter or anything else you can dream up.

The filtering logic for my Predicate implementation lies in the evaluate methods. It is in these methods that I have to check if a given value for a specified column falls between the high and low values I established when I call the RangeFilter constructor. You can see this logic in the evaluate method below, which takes in the column name as an argument.

public boolean evaluate(Object value, String columnName) { int colVal; boolean bool = true; if(columnName.equals(this.columnName) ) { colVal = ( (Integer)value ).intValue(); if ((colVal>=low) && (colVal<=high)) bool = true; else bool = false; } return bool; }

In order to decide whether a row meets the filter requirements, I first have to check to make sure I'm looking at the proper column:


The Predicate object forces me to pass in an Object as an argument. Because I am scrutinizing integers, I have to cast to an Integer wrapper object and get the housed integer to evaluate. I'll then do a check to see if the value falls between the specified range:

if ((colVal>=low) && (colVal<=high))

If it does, I'll set my boolean variable to true so that the evaluate method will return a true, signifying that the given row is allowed to be part of the FilteredRowSet. If I am on the correct column and the value does not fall between the range, my boolean variable is set to false. False causes an SQLException to be thrown when a row that does not pass the filter is inserted.

You will notice similar code to the evaluate method above in the evaluate(RowSet) method. This method is used internally when you apply your filter. When a row does not pass the filter scrutiny, an SQLException is thrown and the row is discarded from the RowSet.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date