It holds the data even if connection is closed. It can be serialized.
Example:
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 {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
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
rs.close();
st.close();
con.close();//connection is closed
while(crs.next()) {
System.out.print(crs.getInt(1)+"\t");
System.out.println(crs.getString(2)+"\t");
System.out.println(crs.getString(3)+"\t");
}
//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