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
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 tablenamely the players with the highest average points per game during the 2003-2004 regular season:
Table 1. The NBA Table Data.
||Los Angeles Lakers
||New Orleans Hornets
||San Antonio Spurs
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.