You want to create a new CachedRowSet object from a non-JDBC source (such as a file or a spreadsheet). To do so, follow these steps:
Before creating a CachedRowSet object, a RowSetMetaData object must be created, set up with a description of the data, and attached to the CachedRowset object before loading the actual data from a non-JDBC source. Next, I discuss these steps in detail.
Creating an Object That Implements javax.sql.RowSetReader
This step involves creating a class that implements the javax.sql.RowSetReader interface, which loads the data from a non-JDBC source. According to the J2SE documentation, RowSetReader is the facility that a disconnected RowSet object calls on to populate itself with rows of data. A reader (an object implementing the RowSetReader interface) may be registered with a RowSet object that supports the reader/writer paradigm. When the RowSet object’s execute() method is called, it in turn calls the reader’s readData() method. The RowSetReader interface has only one method:void readData(RowSetInternal caller)
The following class (CustomRowSetReader) implements the RowSetReader interface, which loads the data into a CachedRowSet object from a non-JDBC source. According to JDK 1.5, RowSetInternal is an interface that a RowSet object implements in order to present itself to a RowSetReader or RowSetWriter object. The RowSetInternal interface contains methods that let the reader or writer access and modify the internal state of the rowset.import java.sql.*;
Creating an Object That Implements javax.sql.RowSetWriter This step involves creating a class that implements the javax.sql.RowSetWriter interface, which writes the data from a CachedRowSet object to a data source. According to the J2SE documentation, an object that implements the RowSetWriter interface, called a writer, may be registered with a RowSet object that supports the reader/writer paradigm. If a disconnected RowSet object modifies some of its data, and it has a writer associated with it, it may be implemented so that it calls on the writer’s writeData method internally to write the updates back to the data source. In order to do this, the writer must first establish a connection with the rowset’s data source. If the data to be updated has already been changed in the data source, there is a conflict, in which case the writer will not write the changes to the data source. The algorithm that the writer uses for preventing or limiting conflicts depends entirely on its implementation.
The RowSetWriter interface has only one method:boolean writeData(RowSetInternal caller)
The following class (CustomRowSetWriter) implements the RowSetWriter interface, which reads the data from a CachedRowSet object and writes it to a data source:import java.sql.*;
Registering Classes Created in Steps 1 and 2
To register your custom RowSetReader and RowSetWriter, you have to implement the javax.sql.rowset.spi.SyncProvider. SyncProvider is the synchronization mechanism that provides reader and writer capabilities for disconnected RowSet objects. A SyncProvider
implementation is a class that extends the SyncProvider abstract class. The MySyncProvider class registers our custom RowSetReader and RowSetWriter:
Instantiating a CachedRowset Object with Registered Classes
To instantiate a CachedRowSet object with registered classes, we need to pass the MySyncProvider object to a constructor of the CachedRowSetImpl class (a Sun reference implementation of the cachedRowSet interface).CachedRowSet crs;
Invoking CachedRowset.execute() to Load the Data
We next invoke the method CachedRowset.execute():crs.execute(); // load data from custom RowSetReader
Invoking CachedRowset.acceptChanges() to Write the Data
Finally, we invoke CachedRowset.acceptChanges() to commit changes back to the database. I’ve provided a complete solution here. This solution puts together the steps I defined earlier to create a custom RowSet object. You have complete freedom in creating your RowSet object (you even tailor your custom RowSet’s metadata).import java.util.Hashtable;
JDBC Related Interview Questions
|Core Java Interview Questions||JSP Interview Questions|
|Java Servlets Interview Questions||EJB(Enterprise JavaBeans) Interview Questions|
|JSTL(JSP Standard Tag Library) Interview Questions||JNDI (Java Naming and Directory Interface) Interview Questions|
|Java Bean Utils Interview Questions||AWT (Abstract Window Toolkit) Interview Questions|
What Is Jdbc Programming?
Database Metadata, Part 1
Database Metadata, Part 2
Exploring Driver Property Information
Rowset Metadata A
Web Access To Metadata,part 1
Web Access To Metadata, Part 2
Rdf And Jdbc Metadata
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.