Advertisment

Caching Query Results in XML

author-image
PCQ Bureau
New Update

Take a form that has more than a few combo boxes and list boxes to be filled with data from a database. An example of such a form can be a Find/Search form. Each time a user launches the search form, he may have to wait for a significant time before all the combos and lists are filled-for example, a combo box with a list of thousand cities and a list box of several hundred users. If the database is sitting across networks, response can become a nightmare to the user. An obvious solution is caching-storing the results of the database query in a local file. 

Advertisment

What J2SE (Java 2 Standard Edition) 5 makes easier is the storing and retrieving in an XML file. With WebRowSets in J2SE 5, we just need to say readXML and writeXML to populate it with data and to store its data in an XML file, respectively. A WebRowSet (and other RowSets like JdbcRowSet, CachedRowSet, JoinRowSet and FilteredRowSet) are disconnected ResultSets-ie, unlike in the case of a ResultSet, RowSets do not require a connection to the database while iterating through them. With all the good reasons, to use WebRowSet, in place, we now look at some code to see how it works. 

The algorithm



Here is how a typical Java code using WebRowSet will look like.

if(cacheFileExists( )) {



// read data from cache using WebRowSet


readFromCache(wrs) //here wrs is the WebRowSet object


}


else {


// connect to database, fire the query


// write the results in the WebRowSet to the cache file 


writeToCache(wrs);


}






Advertisment

Create a WebRowSet object



A WebRowSet object is created as follows.

WebRowSetImpl wrs = new WebRowSetImpl( );

WebRowSetImpl is a class that provides a reference implementation for the interface WebRowSet. Different database vendors can have different implementations for this interface. What we are using here is Sun's reference implementation WebRowSetImpl. The WebRowSetImpl class belongs to the package com.sun.rowset. So do not forget to import this package in your code. 

Advertisment

Read from cache



If the cache file (say cache.xml residing in your home directory) exists, then the data is read from the cache file into the wrs object. To accomplish this, the wrs object requires a Reader object to the file, as follows.

BufferedReader br = new BufferedReader(new FileReader(cacheFile));

Subsequently the readXML( ) method of the wrs object is used to read the data from the file into the wrs object. This method requires the Reader object as a parameter. Following is the implementation code for the readFromCache( ); method in our algorithm.

Advertisment

private void readFromCache(WebRowSetImpl wrs) {



try {


File cacheFile = new File(System.getProperty("user.home")+"\\cache.xml");


BufferedReader br = new BufferedReader(new FileReader(cacheFile));


wrs.readXml(br);


}


catch(Exception e) {


System.err.println(e); 





}







Before reading the cache file, check for its existence to avoid exceptions. We do this in

the cacheExists( ) method which is as:

private boolean cacheExists( ) {



File cacheFile = new File(System.getProperty("user.home")+"\\cache.xml");


if(cacheFile.exists())


return true;


else


return false; 


}




Advertisment

Write to cache



If the cache file does not exist, when the application is launched for the first time, a usual query is fired against the database and the results are stored in a ResultSet object. The results from a ResultSet object can be transferred to the wrs object simply as:

wrs.populate(rs);

Here rs is a ResultSet object. Now the wrs object is filled with the data from the executed database query. Next, to write the data in the wrs object to the cache file, first a Writer object to the file is required which can be created as follows.

Advertisment

BufferedWriter bw = new BufferedWriter(new FileWriter(cacheFile));

Subsequently the writeXML( ) method of the wrs object is used to write the data to the cache file. This method requires the Writer object as a parameter. Following is the implementation code for the writeToCache( ); method in our algorithm.

private void writeToCache(WebRowSetImpl wrs){



try{


File cacheFile = new File(System.getProperty("user.home")+"\\cache.xml");


BufferedWriter bw = new BufferedWriter(new FileWriter(cacheFile));


wrs.writeXml(bw);


}


catch(Exception e){


System.err.println(e); 





}







Advertisment

Walking through the WebRowSet



Iterating through the wrs object is similar to that for a ResultSet object. Following is the code for

the same.

while(wrs.next( ))



System.out.println (wrs.getString("
"));

A WebRowSet application



At forums.pcquest.com, we have posted a simple application that demonstrates the use of WebRowSets. It is small desktop application with a form (JForm) and a combo box (JComboBox). The combo box is filled with the name of the cities from a database. To run this example application you will need to use MS SQL Server. Create a database (say db) with a table named cities with a column named 'name'. Populate it with a list of cities. We will use the free jTDS JDBC drivers to connect to the SQL Server database. Download the drivers from http://jtds.sourceforge. net. Download the latest (0.9 as of this writing) JAR file of the drivers. 

To compile and run the code, download WebRowSetApplication.java file from the Developer section at forums.pcquest.com. Place it with the downloaded jar file (jtds-0.9.jar) of jTDS drivers. Open it in a text editor and substitute the database credentials in the getConnection( ) method with the ones for your database. Install J2SE 5.0 from the November PCQ Xtreme CD. Change to the directory containing the WebRowSetApplication. java and jtds-0.9.jar file and issue the following:

\bin\javac WebRowSetApplication.java



\bin\java -cp jtds-0.9.jar;. WebRowSetApplication

Shekhar Govindarajan



IT4Enterprise

Advertisment