Cached RS : CachedRowSet « Database SQL JDBC « Java






Cached RS

 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.sql.rowset.CachedRowSet;

public class CachedRS {
  private final static String CRS_FILE_LOC = "cachedrs.crs";

  public static void main(String[] args) throws Exception {
    FileInputStream fis = new FileInputStream(CRS_FILE_LOC);
    ObjectInputStream in = new ObjectInputStream(fis);
    CachedRowSet crs = (CachedRowSet) in.readObject();
    fis.close();
    in.close();

    
    Class.forName("oracle.jdbc.driver.OracleDriver");
    crs.setUrl("jdbc:oracle:thin:@localhost:1521:ORCL");
    crs.setUsername("yourName");
    crs.setPassword("mypwd");
    String sql = "SELECT SSN, Name, Salary, Hiredate FROM Employees WHERE SSN=?";
    crs.setCommand(sql);
    crs.setInt(1, 111111111);
    crs.execute();

    FileOutputStream fos = new FileOutputStream(CRS_FILE_LOC);
    ObjectOutputStream out = new ObjectOutputStream(fos);
    out.writeObject(crs);
    out.close();
    crs.close();
    
    fis = new FileInputStream(CRS_FILE_LOC);
    in = new ObjectInputStream(fis);
    crs = (CachedRowSet) in.readObject();
    fis.close();
    in.close();

    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"));
    }
    crs.close();
  }
}

 








Related examples in the same category

1.Save CachedRowSet Back To Database