What Is a WebRowSet? JDBC

A WebRowSet (javax.sql.WebRowSet) object is a disconnected rowset that caches its data in memory in the same manner as a CachedRowSet object. A WebRowSet object is special because in addition to offering all the capabilities of a CachedRowSet object, it can write itself as an XML document and can also read that XML document to convert itself back to a WebRowSet object. Due to its XML support, WebRowSet is an ideal object format for web services. WebRowSet is also ideal for just painting a data for a single web page—a large-sized WebRowSet might introduce performance problems. Therefore, for better performance, you should keep its size as small as possible.

XML is the language through which disparate enterprises can communicate with each other, and has become the standard for web services communication. As a consequence, a WebRowSet object fills a real need by making it easy for web services to send and receive data from a database in the form of an XML document.

According to J2SE 5.0 documentation, WebRowSet describes the standard XML document format required when describing a RowSet object in XML and must be used by all standard implementations of the WebRowSet interface to ensure interoperability. In addition, the WebRowSet schema uses specific SQL/XML schema annotations, thus ensuring greater crossplatform interoperability. This is an effort currently under way at the ISO organization.

The schema definition describes the internal data of a RowSet object in three distinct areas:

  • Properties: These properties describe the standard synchronization provider properties in addition to the more general RowSet properties.
  • Metadata: This area describes the metadata associated with the tabular structure governed by a WebRowSet object. The metadata described is closely aligned with the metadata accessible in the underlying java.sql.ResultSet interface.
  • Data: This area describes the original data (the state of data since the last population or last synchronization of the WebRowSet object) and the current data. By keeping track of the delta between the original data and the current data, a WebRowSet maintains the ability to synchronize changes in its data back to the originating data source.

The Creation and Population of WebRowSet

You can create a WebRowSet object and its associated metadata by using the following code fragments. Before creating a WebRowSet object, I set up a database table:

mysql> use octopus; Database changed mysql> desc employees;Creation and Population of WebRowSet

First, you need to create an empty WebRowSet object:

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

Updating a WebRowSet Object

To update a WebRowSet object, you may update your desired rows(by using WebRowSet.
updateXXX()methods)and then invoke the WebRowSet.acceptChanges()method. The following
code fragment updates the first two rows in the WebRowSet object wrs:

Representing a WebRowSet Object as an XML Object

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

The created emps.xml file will have three sections:

  • Properties
  • Metadata
  • Data

A generated XML document will contain a properties section, which will contain information about our connection, including the command associated with our WebRowSet object—SELECT id, name, age FROM employees—as well as other important information. The properties section of a WebRowSet object is depicted here (extracted from emps.xml):

The metadata section contains metadata about the structure of the columns that our WebRowSet object consists of. For each rowset’s column we have a <column-definition> element that describes the structure of the underlying column. The metadata section of a WebRowSet object is depicted here (extracted from emps.xml):

The data section of a WebRowSet object is depicted here (extracted from emps.xml):

The Solution

Here, I put all of the pieces together as a single program, which shows how to create a WebRowSet object and populate it from a database:

Running the Solution for a MySQL Database

Oracle Database Setup

Running the Solution for an Oracle Database

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

JDBC Topics