Java Reference
In-Depth Information
Listing 18-8:
Using a CachedRowSet
package JavaDatabaseBible.ch18;
import java.io.*;
import sun.jdbc.rowset.*;
public class CachedRowSetDeserializer{
public static void main(String[] argv){
CachedRowSetDeserializer crd = new CachedRowSetDeserializer();
crd.deserializeRows(argv[0]);
}
public CachedRowSetDeserializer(){
}
public void deserializeRows(String fName){
try {
FileInputStream fIn = new FileInputStream(fName);
ObjectInputStream in = new ObjectInputStream(fIn);
CachedRowSet rowSet = (CachedRowSet)in.readObject();
while(rowSet.next()){
for(int j=1; j<=rowSet.getMetaData().getColumnCount(); j++){
System.out.print( rowSet.getObject(j)+"\t");
}
System.out.println();
}
rowSet.close();
}catch(Exception e) {
System.err.println(e.getMessage());
}
}
}
Despite its name, the distinguishing feature of the WebRowSet is that it is designed to serialize and
deserialize itself in XML. The details of how it works are discussed in the
next section
.
Generating XML from a RowSet
The designers of the
RowSet
object realized that
RowSets
had the potential to be very useful in XML
applications. One of the sample implementations in Sun's rowset jar is the
WebRowSet
. The
WebRowSet
is an extension of the
CachedRowSet
designed to serialize and deserialize a
RowSet
in
XML format. The class stores an
XmlReader
object that it uses to read a
RowSet
in XML format and
an
XmlWriter
object that it uses to write a
RowSet
in XML format.