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


Master the FilteredRowSet Interface for Disconnected Data Retrieval

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.

he latest version of Java, J2SE 5.0 (version 1.5 of the JDK) is the first to deliver the concept of disconnected RowSet objects to the Java language. The FilteredRowset interface extends from the WebRowSet interface, which in turn extends from the javax.sql.Rowset interface. As you will see, the FilteredRowSet lets one narrow down the number of rows in a disconnected object based on filtering logic you provide without requiring an ongoing connection to your database. In this article, I'll introduce you to FilteredRowSet objects.

Where Oh WHERE Clause?
If you were using simple JDBC, you could achieve the same thing using a WHERE clause in a query using a JdbcRowset object. But JdbcRowset objects require a connection to the database, whereas RowSet objects (a superset of FilteredRowSet objects) do not (i.e., they are disconnected). A WHERE clause requires a connection to the database to filter your database data. With the FilteredRowSet, you can retrieve your data from the database and then disconnect. As you will see, the FilteredRowSet uses Predicate objects to retrieve data from RowSet objects without a database connection using WHERE-like logic.

A Real World Example
I've decided to build a fun example to help facilitate understanding. This application will retrieve NBA (National Basketball Association) player statistics for the 2003-04 season. Don't worry, you don't need to know much about basketball for this study. You'll build a database table that stores player data including games played (G), field goals made (FG), free throws made (FT), total points scored (P), and average points per game (PG).

To do this, you'll need a database and, of course, a database table. You can choose any database you prefer but it must support FilteredRowSet objects, which are an offering of Java 1.5. I am going to use IBM's DB2 8.1, which you can download as a trial from http://www-306.ibm.com/software/data/db2/udb/v8/.

I'll start by creating a database using the DB2 command line processor:

db2 => create database balldb
Next, I'll connect to the database with my user name (db2admin) and password (db2admin).

db2=> connect to balldb user db2admin using db2admin
Next, I'll create a table (named STATS) to house all the statistical data mentioned above. The following statement creates the table:

db2=> create table stats (firstname varchar(40) not null, lastname varchar(40) 
   not null, team varchar(40) not null, G int not null, FG int not null, FT int not null, P int not null,
PG decimal(3,1) not null)
I need some data in my table. In the real world, this table would likely have data for all the players in the NBA. For the purposes of this example, I've included only 10 players in the table—namely the players with the highest average points per game during the 2003-2004 regular season:

Table 1. The NBA Table Data.

Tracy McGrady Orlando Magic 67 653 398 1,878 28.0
Predrag Stojakovic Sacremento Kings 81 665 394 1,964 24.2
Kevin Garnett Minnesota Timberwolves 82 804 368 1,987 24.2
Kobe Bryant Los Angeles Lakers 65 516 454 1,557 24.0
Paul Pierce Boston Celtics 80 602 517 1,836 23.0
Baron Davis New Orleans Hornets 67 554 237 1,532 22.9
Vince Carter Toronto Raptors 73 608 336 1,645 22.5
Tim Duncan San Antonio Spurs 69 592 352 1,538 22.3
Dirk Nowitzki Dallas Mavericks 77 605 371 1,680 21.8
Michael Redd Milwaukee Bucks 82 633 383 1,776 21.7

The following statement inserts the first row into the database.

insert into stats values ('Tracy','McGrady','Orlando Magic',67,653,398,1878,28.0)
You can add each of the remaining nine rows with appropriate data using the same process, until the table is complete.

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