Oracle CachedRowSet Tutorial with JDBC
- Thursday, October 20, 2011, 2:55
- JDBC Oracle, JDBC Tutorial
- Add a comment
The CachedRowSet class provides the same functionality as the JdbcRowSet class, with one important difference: a CachedRowSet object can operate in a disconnected environment. As a result, it can function without a JDBC driver present. Once you populate a CachedRowSet object with data you may send it over the network to thin clients (PDA’s etc.), provide it to sales professionals on their laptops, or serialize it for data archiving. The advantage of the CachedRowSet object is that the client to whom you send the object does not need the JDBC driver.
JdbcRowSet Vs. CachedRowSet
The JdbcRowSet is a connected rowset that requires a continuous connection with the data source and you cannot serialize a JdbcRowSet object, which limits your ability to distribute or save the object.The CachedRowSet class overcomes those limitations. It provides a disconnected and serializable implementation of the RowSet interface.
To operate in a disconnected state a CachedRowSet object creates a virtual database by caching the tabular information it receives from the data source. Storing the data internally
makes the object self-contained and thereby allows it to operate while disconnected.
Following is the tutorial of CachedRowSet which is using serialization to read and write the Object.
import java.io.*; import java.sql.SQLException; import oracle.jdbc.rowset.OracleCachedRowSet; public class CachedRowSetDemo { //Constant to hold file name used to store the CachedRowSet private final static String CRS_FILE_LOC ="cachedrs.crs"; public static void main(String[] args) throws Exception { try { //Create serialized CachedRowSet writeCachedRowSet(); //Create CachedRowSet from serialized object OracleCachedRowSet crs = readCachedRowSet(); //Display values while(crs.next()){ System.out.print("SSN: " + crs.getInt("ssn")); System.out.print(", Name: " + crs.getString("name")); System.out.print(", Salary: $" + crs.getDouble("salary")); System.out.print(", HireDate: " + crs.getDate("hiredate")); System.out.println(); } //Close resource crs.close(); }catch (SQLException se){ se.printStackTrace(); }catch (Exception ex) { ex.printStackTrace(); } }//end main public static void writeCachedRowSet() throws Exception{ //Instantiate a CachedRowSet object, set connection parameters OracleCachedRowSet crs = new OracleCachedRowSet(); Class.forName("oracle.jdbc.driver.OracleDriver"); crs.setUrl("jdbc:oracle:thin:@localhost:1521:khan_db"); crs.setUsername("scott"); crs.setPassword("tiger"); //Set and execute the command. Notice the parameter query. String sql = "SELECT SSN, Name, Salary, Hiredate "; sql = sql + "FROM Employees WHERE SSN=?"; crs.setCommand(sql); crs.setInt(1,123456); crs.execute(); //Serialize CachedRowSet object. FileOutputStream fos = new FileOutputStream(CRS_FILE_LOC); ObjectOutputStream out = new ObjectOutputStream(fos); out.writeObject(crs); out.close(); crs.close(); }//end writeCachedRowSet() public static OracleCachedRowSet readCachedRowSet() throws Exception{ //Read serialized CachedRowSet object from storage FileInputStream fis = new FileInputStream(CRS_FILE_LOC); ObjectInputStream in = new ObjectInputStream(fis); OracleCachedRowSet crs = (OracleCachedRowSet)in.readObject(); fis.close(); in.close(); return crs; }//end readCachedRowSet() }//end CachedRS

