JDBC_RowSet1.htm
 
  • A ResultSet requires  a stable connection to a database, and unlike Rowset it can’t be serialized and you can't pass the Resultset object from one class to other class across the network.
  • RowSet is a  serializable  version of a JDBC ResultSet, essentially a Java Bean, and it also the RowSet extends the ResultSet interface suppoerting all the methods of ResultSet.
  • The RowSet can be connected or disconnected,  serialized, and also it can be sent from one class to another across the network.
    • Query results are scrollable and updatable without relying on the underlying JDBC driver
    • Read and Write data results to or from XML documents
    • Has following five interfaces : JdbcRowSet, CachedRowSet, WebRowSet, JoinRowSet, and FilteredRowSet
  • Connected VS Disconnected RowSet
    • Connected : JdbcRowset, maintains it's connection to DB through out its life cycle.
    • Disconnected : WebRowSet and ChachedRowSet, doest need active connection to DB, must reestablish connection to commit any changes.
  • CahedRowSet : The CachedRowSet object represents disconnected RowSet holds data row data and metadata in a buffer so that when the connection to the database is restored, the altered data in the RowSet are submitted back to the database.
    • A CachedRowSet object includes all the functionalities of a JdbcRowSet object plus it can also do the following:
      • Obtain a connection to a data source and execute a query.
      • Read the data from the resulting ResultSet object and populate itself with that data
      • It allows you to manipulate data and make changes while it is disconnected from the db
      • Must Reconnect to the data source to write or alter the data to table
      • Check for conflicts with the data source and resolve those conflicts
  • WebRowset :with a WebRowSetXmlWriter writes rowset to an XML document and using a WebRowSetXmlReader, it reads an XML document into the rowset.
    • A WebRowSet object has all the functionalites of a CachedRowSet object, in additions  it can it allows the following operations:
      • Write itself as an XML document
      • Read an XML document through a WebRowSet object
  • FilteredRowSet object simulates a SQL WHERE clause
    • A FilteredRowSet object includes all the options of WebRowSet object (and therefore also a CachedRowSet object) plus it can also do the following:
      • Apply filtering criteria so that only selected data is visible. This is equivalent to executing a query on a RowSet object without having to use a query language or connect to a data source.
  • JoinRowSet represents SQL join among the disconnected RowSets. 
    • A JoinRowSet object has all the capabilities of a WebRowSet object (and therefore
      also a CachedRowSet object) plus it can also do the following:
      • Form the equivalent of an SQL JOIN without having to connect to a data
      source.
  Example of : JdbcRowSet // Connected Rowset
  • default constructor:
    JdbcRowSet rowset = new JdbcRowSetImpl();
  • with RowSetProvider Interface:
    • rowSet = RowSetProvider.newFactory().createJdbcRowSet();
  • SQL query steps:
    rowSet.setCommand(sql1);
    rowSet.execute();
 
  Code : JDBC_RowSet1.txt

package javatemplate1;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.RowSetEvent;
import javax.sql.RowSetListener;
import javax.sql.rowset.JdbcRowSet;
import javax.sql.rowset.RowSetProvider;
/**
*
* @author Manas14
*/
public class JavaTemplate1 {

public static void main(String[] args) {
// TODO code application logic here
System.out.println("main block executing ");
Connection c = null;
String dbURL = "jdbc:postgresql://localhost:5432/pgsdemo1";
String user = "postgres"; String pwd = "postgre_manas9";
PreparedStatement ps= null;
String sql1 = "Select * from emp";
// connecting to db
try {
c = DriverManager.getConnection(dbURL,user,pwd);
System.out.println("Connected to database successfully");
JdbcRowSet rowSet = RowSetProvider.newFactory().createJdbcRowSet();
rowSet.setUrl(dbURL);
rowSet.setUsername(user);
rowSet.setPassword(pwd);
rowSet.setCommand(sql1);
rowSet.execute();

//
while(rowSet.next())
{
int eno= rowSet.getInt("EMPNO");
String name = rowSet.getString("ename");
String job = rowSet.getString("JOB");
int manager = rowSet.getInt("MGR");
Date date = rowSet.getDate("HIREDATE");
float salary = rowSet.getFloat("SAL");
// double commision = rs.getInt("COMM");
int deptno = rowSet.getInt("DEPTNO");
System.out.println("deptno " + deptno + " eno " + eno+ " name "+
name + " JOB " + job +" manager " + manager +
" hired" + date + " sal " + salary );

}

//
} catch (SQLException ex) { ex.getErrorCode();
String message = ex.getMessage();
System.out.println(message);
} finally {
System.out.println("going through final block");
try {
if (ps== null || ps.isClosed()) {
} else {
ps.close();
}
if (c != null && !c.isClosed()) {
c.close(); }
} catch (SQLException ex) { ex.getErrorCode();
ex.getMessage();
}
}
System.out.println("dis-Connected to database successfully");
// st.close(); con.commit();con.close();
}

}

 
  Disclaimer: and References :

I relied on the content of this document

" JDBC TM  RowSet Implementations Tutorial
Maydene Fisher with contributions from Jonathan Bruce, Amit Handa & Shreyas Kaushik "

Sun Microsystems Inc.
4150 Network Circle
Santa Clara, CA 95054
USA
Revision 1.0