What is CachedRowSet ?

It holds the data even if connection is closed. It can be serialized.
import java.io.*;
import javax.sql.*;
import java.sql.*;
import javax.sql.rowset.*;
import com.sun.rowset.*;
public class demo{
public static void main(String[] args)throws Exception {
CachedRowSet crs=new CachedRowSetImpl();
Connection con=DriverManager.getConnection("jdbc:odbc:dsn","system","oracle");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("Select *from emp");
crs.populate(rs);//populates the data into RowSet from ResultSet
con.close();//connection is closed
while(crs.next()) {
//this below line writes the RowSet Object into temp.ser
new ObjectOutputStream(new FileOutputStream("temp.ser")).writeObject(crs);
Here the CachedRowSet shows the data after connection is closed.
The CachedRowSet object(crs) is serialized into a file temp.ser

Updating CachedRowSet:
Most of the steps for updating a CachedRowSet is same as ResultSet.
updateXXX() method , insertRow() all are same as ResultSet.
When the updates are performed in the CachedRowSet it is disconnected from the data source and result updates are not finally written to the database.
So after invoking all the update methods we have to call acceptChanges() method to make changed data permanent in the database.

Summerize capabilities of java.sql.rowset.CachedRowSet:
1- Obtains a connection to the data source and execute a query.
2- Reads the data from ResultSet and populate itself with the data.
3- Manipulates data and make changes to data while it is disconnected.
4- Reconnects to data source to write the changes back to it.
5- Check conflicts with the data source and resolve those.
6- You can write the CachedRowSet object to a file if you need.

No comments:

Post a Comment