How Do You Get Metadata from a FilteredRowSet Object? 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, which I’ll describe next.

First, you create an empty FilteredRowset object:

After creating an empty FilteredRowSet object, you can populate it:

Alternatively, you can populate the FilteredRowSet object from a ResultSet object:

After populating a FilteredRowSet object, you can apply an additional filter to restrict the selected rows or records by using the setFilter() method:

Customized Predicates

Some vendors provide a customized filter to be used by FilteredRowSet objects. For example , BEA WebLogic Server provides a SQLPredicate class
(weblogic.jdbc.rowset.SQLPredicate) , which directly implements the
javax.sql.rowset.Predicate interface and provides the following constructor:

public SQLPredicate(String selector)

Using this class, you can create customized filters and predicates:

The SQLPredicate class borrows its grammar from the JMS selector grammar, which is very similar to the grammar for a SQL SELECT WHERE clause.

Extracting Metadata from a FilteredRowSet Object

After populating a WebRowSet object, you can get its metadata by using the following:

Representing a FilteredRowSet Object as an XML Document

Writing a FilteredRowSet object to an XML document can be accomplished by using the FilteredRowSet.writeXml() method, which can take in either an OutputStream object (if we want to write in bytes) or a Writer object (if we want to write in characters). The code snippet that follows writes to a file called emps.xml using a FileWriter object:

The JoinRowSet is a disconnected rowset object, which extends the WebRowSet interface (which is also a disconnected rowset). JoinRowSet mimics the concept of a SQL JOIN. A JOIN combines records or rows from two or more tables in a relational database. According to Wikipedia (http://en.wikipedia.org), “A join combines records from two or more tables in a relational database. In the Structured Query Language (SQL), there are two types of joins: ‘inner’ and ‘outer’. Outer joins are subdivided further into left outer joins, right outer joins, and full outer joins.” To understand the JoinRowSet interface better, I set up some database tables to be used by JoinRowSet objects.

MySQL Database Setup

The following query verifies that a join is basically a Cartesian product that is followed by a predicate to filter the results:

You can filter the result of the JOIN operation. In the following SQL query, note that the INNER JOIN statement was issued using the on employees.dept = departments.dept clause. This JOIN operation effectively combined the data contained in two tables (employees and departments) based on a common attribute (the "dept" value). Using a JoinRowSet object, the SQL JOIN operation will be performed using a match column of "dept". To be a match column, the column of interest needs to be residing in the employees and departments tables, which are being joined.

We will use this database object to show how to create a JoinRowSet object. JoinRowSet is capable of doing a SQL JOIN operation. Let’s see how.

First, create CachedRowSet objects for both tables:

Next, we perform a JOIN operation:

There is an alternative method to doing a JOIN operation: you can use the Joinable interface to accomplish the same thing:

After performing our offline JOIN, you can traverse our JoinRowSet object using getter methods like we do with ResultSet objects. Here’s this iteration in the code:

Extracting Metadata from a JoinRowSet Object

After creating a JoinRowSet object, you can get its metadata by using the following:



Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

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

JDBC Topics