Tutorial
Menus

JDBC - JDBC Tutorials

Using CachedRowSet

Rating: 5.0/5 (3 votes cast)

Level   : Beginners
Author : Arunkumar S
Download Source : CachedRowSetTester.java
Download Source : UpdateCachedRowSet.java
Download Source : InsertCachedRowSet.java
Download Source : All-Src.zip

Using CachedRowSet tutorial

The reference implementation of the CachedRowSet interface provided by Sun Microsystems is a standard implementation. Developers may use this implementation just as it is, they may extend it, or they may choose to write their own implementations of this interface.

A CachedRowSet object is a container for rows of data that caches its rows in memory, which makes it possible to operate without always being connected to its data source. Further, it is a JavaBeansTM component and is scrollable, updatable, and serializable. A CachedRowSet object typically contains rows from a result set, but it can also contain rows from any file with a tabular format, such as a spread sheet. The reference implementation supports getting data only from a ResultSet object, but developers can extend the SyncProvider implementations to provide access to other tabular data sources.

An application can modify the data in a CachedRowSet object, and those modifications can then be propagated back to the source of the data.

A CachedRowSet object is a disconnected rowset, which means that it makes use of a connection to its data source only briefly. It connects to its data source while it is reading data to populate itself with rows and again while it is propagating changes back to its underlying data source. The rest of the time, a CachedRowSet object is disconnected, including while its data is being modified. Being disconnected makes a RowSet object much leaner and therefore much easier to pass to another component. For example, a disconnected RowSet object can be serialized and passed over the wire to a thin client such as a personal digital assistant (PDA)

Getting Data Using CachedRowSet

package com.javaorigin.jdbc.sample.cachedrowset;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.sql.rowset.CachedRowSet;

import com.sun.rowset.CachedRowSetImpl;


public class 
CachedRowSetTester {

    public static 
void main(String[] argsthrows Exception {
        Class.
forName("oracle.jdbc.driver.OracleDriver");
        
Connection conDriverManager.getConnection("jdbc:oracle:thin:@javaorigin.com:1521:orcl","mydb","password123");
        
Statement st=con.createStatement();      
        
st.setMaxRows(10);
        
ResultSet rsst.executeQuery("select * from users");
        
CachedRowSet cachedRowSet=new CachedRowSetImpl();
        
cachedRowSet.populate(rs);
        
rs.close();
        
st.close();
        
con.close();
        
        while (
cachedRowSet.next()) {
            
System.out.println(cachedRowSet.getString(1));
        }
        
    }

}



Update Using CachedRowSet


package com.javaorigin.jdbc.sample.cachedrowset;

import javax.sql.rowset.CachedRowSet;

import com.sun.rowset.CachedRowSetImpl;

public class 
UpdateCachedRowSet {    

      public static 
void main(String[] argsthrows Exception {
        
CachedRowSet rs=new CachedRowSetImpl();
        Class.
forName("oracle.jdbc.driver.OracleDriver");
        
rs.setUrl("jdbc:oracle:thin:@javaorigin.com:1521:orcl");
        
rs.setUsername("mydb");
        
rs.setPassword("password123");
        
rs.setCommand("select * from users where userid=?");
        
rs.setString(1"%");
        
rs.execute();
     
        while (
rs.next()) {
          if (
rs.getString("usrid").equals("Arun")) {
              
rs.setString("usrid""Arunkumar");
              
rs.updateRow();            
              
rs.acceptChanges();
          }
        }       
        
rs.close();
      }
    
}



Insert using CachedRowSet


package com.javaorigin.jdbc.sample.cachedrowset;

import java.sql.*;

import javax.sql.rowset.CachedRowSet;

import com.sun.rowset.CachedRowSetImpl;

public class 
InsertCachedRowSet {
    public static 
void main(String[] argsthrows Exception {
        
CachedRowSet crs = new CachedRowSetImpl();
        
//Class.forName("org.sqlite.JDBC");
        //Connection conn = DriverManager.getConnection("jdbc:sqlite:test.db");
        
Class.forName("oracle.jdbc.driver.OracleDriver");
        
Connection connDriverManager.getConnection("jdbc:oracle:thin:@javaorigin.com:1521:orcl","mydb","password123");        
        
crs.setCommand("drop table if exists people;");
        
crs.execute(conn);
        
crs.setCommand("create table people (name)");
        
crs.execute(conn);

        
// First way to insert
        
crs.setCommand("insert into people(name) values(?)");
        
crs.setString(1"Arunkumar");
        
crs.execute(conn);
        
// end of first way

        
crs.setCommand("select * from people ");
        
crs.execute(conn);

        
// Second way to insert
        
crs.moveToInsertRow();
        
crs.updateString(1"Arunkumar Subramaniam");
        
crs.insertRow();
        
crs.moveToCurrentRow();
        
// end of second way

        
while (crs.next()) {
            
System.out.println("Name : " crs.getString("name"));
        }

        
conn.close();
    }
}


1 | 

Discussion about this tutorial

  Start a new Discussion | Read All Discussion
Subject RepliesLast Post
Javaorigin.com contact@javaorigin.com