The CachedRowSet interface defines the basic capabilities available to all disconnected RowSet objects. The cachedRowSet actually caches the data in its own location rather than getting data for all the operations and after updations it will write back the updated data in the database. It also resolve the conflicts occurs during any update to data in the database In the cachedRowSets the Key columns are set to identify the rows uniquely in the cache.


So it is advisable it should be same as primary key column defined in the database.


 


Note:- A disconnected RowSet object must call the method acceptChanges() in order to save its changes to the data source.


package com.visualbuilder;


import javax.sql.rowset.CachedRowSet;


import com.sun.rowset.CachedRowSetImpl;


public class CachedRowSetExample {
    public static void main(String[] args) throws Exception {


        Class.forName("org.postgresql.Driver");
               CachedRowSet cachedRs = new CachedRowSetImpl();
        cachedRs.setUsername("user");
        cachedRs.setPassword("pass");
        cachedRs.setUrl("jdbc:postgresql://localhost:5432/postgres");
        cachedRs.setCommand("select * from dept");
        cachedRs.setPageSize(4);
        cachedRs.execute();
                System.out.println("The Deptartment Retrieved from the Database");
        while (cachedRs.nextPage()) {
          while (cachedRs.next()) {
                      System.out.println(cachedRs.getString("dname"));
                  }
           }
    }


}


Output is:-


The Deptartment Retrieved from the Database


Sales


Account


Marketing

                    

Copyright © 2010 VisualBuilder. All rights reserved