ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Making the Most of JDBC with WebRowSet
Pages: 1, 2, 3, 4, 5

Exporting ResultSet

Let me now take this example bit further by assuming that this program is part of an application that requires data conversion from database to XML for some further processing. I will use implementation class com.sun.rowset.WebRowSetImpl for this purpose. To see this in action, let's add some code to construct an instance of javax.sql.rowset.WebRowSet and export the result set to XML file. Here's how you do this:



... ... ... 
ResultSet rs = 
  stmt.executeQuery("select * from student");
WebRowSet wrs = new WebRowSetImpl();
wrs.populate(rs);
try {
  wrs.writeXml(
  new FileOutputStream("student.xml"));
} catch (FileNotFoundException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
}
... ... ...

The output from wrs.writeXML() results in an XML document that conforms to the WebRowSet schema definition. It contains three parts: properties, metadata and data. The general layout is below; see the resources section for the complete output file.

<?xml version="1.0"?> 
<webRowSet xmlns=
 "http://java.sun.com/xml/ns/jdbc" 
xmlns:xsi=
 "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
 "http://java.sun.com/xml/ns/jdbc 
http://java.sun.com/xml/ns/jdbc/webrowset.xsd"> 
 < properties> 
... ... ... 
 </properties> 
 <metadata> 
       ... ... ...
 </metadata> 
 <data> 
... ... ...
</data> 
</webRowSet>

The <properties> tag provides details of the synchronization provider, such as isolation level, RowSet type, etc. The <metadata> tag provides information about the underlying database table, such as numbers, name, and type of columns, etc. The <data> tag holds actual data that has following information for the table above.

 <data>
   <currentRow>
     <columnValue>200 < /columnValue>
     <columnValue>Jack</columnValue>
     <columnValue>Dakota</columnValue>
     <columnValue>21</columnValue>
   </currentRow>
   <currentRow>
     <columnValue>100</columnValue>
     <columnValue>John</columnValue>
     <columnValue>Doe</columnValue>
     <columnValue>26</columnValue>
   </currentRow>
 </data>

The <currentRow> tag in the above example shows the values of each column for the WebRowSet object currently mapped to an underlying data source without any modification. Any data manipulation operations such as insert, update, or delete are marked appropriately in the output XML that I will explain in the next section.

Pages: 1, 2, 3, 4, 5

Next Pagearrow