Recent Posts

Sunday, 18 November 2018

RowSets

☀ It is alternative to ResultSet.
☀ We can use RowSet to handle a group of records in more effective way than ResultSet.
☀ RowSet interface present in javax.sql package
☀ RowSet is child interface of ResultSet.
☀ RowSet implementations will be provided by Java vendor and database vendor.
☀ By default RowSet is scrollable and updatable.
☀ By default RowSet is serializable and hence we can send RowSet object across the network. But ResultSet object is not serializable.
☀ ResultSet is connected i.e to use ResultSet compulsary database Connection must be required.
☀ RowSet is disconnected. i.e., to use RowSet database connection is not required.
Types of RowSets
     There are 2 types of RowSets
1.Connected RowSets
2. Disconnected RowSets

1. Connected RowSets
☀ Connected RowSets are just like ResultSets.
☀ To access RowSet data compulsory connection should be available to database.
☀ We cannot serialize Connected RowSets
E.g: JdbcRowSet

2. Disconnected RowSets
☀ Without having Connection to the database we can access RowSet data.
☀ We can serialize Disconnected RowSets.
E.g: CachedRowSet, WebRowSet, FilteredRowSet, JoinRowSet

Create Different RowSet Objects
import javax.sql.rowset.*; 
/**
 * 
 * @author ashok.mariyala
 *
 */
public class Test { 
   public static void main(String[] args) throws Exception { 
      RowSetFactory rsf = RowSetProvider.newFactory(); 
      JdbcRowSet jrs = rsf.createJdbcRowSet(); 
      CachedRowSet crs = rsf.createCachedRowSet(); 
      WebRowSet wrs = rsf.createWebRowSet(); 
      JoinRowSet jnrs = rsf.createJoinRowSet(); 
      FilteredRowSet frs = rsf.createFilteredRowSet();
      System.out.println(jrs.getClass().getName()); 
      System.out.println(crs.getClass().getName()); 
      System.out.println(wrs.getClass().getName()); 
      System.out.println(jnrs.getClass().getName()); 
      System.out.println(frs.getClass().getName());
   }
}
JdbcRowSet
☀ It is exactly same as ResultSet except that it is scrollable and updatable.
☀ JdbcRowSet is connected and hence to access JdbcRowSet compulsary Connection must be required.
☀ JdbcRowSet is non serializable and hence we cannot send RowSet object across the network.
Example :
Retrieve records from JdbcRowSet
import javax.sql.rowset.*;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class JdbcRowSetRetrieveTest { 
   public static void main(String[] args)throws Exception { 
      RowSetFactory rsf = RowSetProvider.newFactory(); 
      JdbcRowSet rs = rsf.createJdbcRowSet(); 
      rs.setUrl("jdbc:mysql://localhost:3306/ashokdb"); 
      rs.setUsername("root"); 
      rs.setPassword("root"); 
      rs.setCommand("select * from emp"); 
      rs.execute(); 
      System.out.println("Employee Details In Forward Direction"); 
      System.out.println("ENo\tEName\tESalary\tEAddress"); 
      System.out.println("----------------------------------"); 
      while(rs.next()) { 
         System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getFloat(3)+"\t"+rs.getString(4)); 
      } 
      System.out.println("Employee Details In Backward Direction"); 
      System.out.println("ENo\tEName\tESalary\tEAddress"); 
      System.out.println("----------------------------------"); 
      while(rs.previous()) { 
         System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getFloat(3)+"\t"+rs.getString(4)); 
      } 
      System.out.println("Accessing Randomly..."); 
      rs.absolute(3); 
      System.out.println(rs.getRow()+"--->"+rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getFloat(3)+"\t"+rs.getString(4));
      rs.first();
      System.out.println(rs.getRow()+"--->"+rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getFloat(3)+"\t"+rs.getString(4));
      rs.last(); 
      System.out.println(rs.getRow()+"--->"+rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getFloat(3)+"\t"+rs.getString(4)); 
      rs.close();
   } 
}
CachedRowSet
☀ It is the child interface of RowSet.
☀ It is bydefault scrollable and updatable.
☀ It is disconnected RowSet. ie we can use RowSet without having database connection.
☀ It is Serializable.
☀ The main advantage of CachedRowSet is we can send this RowSet object for multiple people across the network and all those people can access RowSet data without having DB Connection.
☀ If we perform any update operations(like insert,delete and update) to the CachedRowSet,to reflect those changes compulsary Connection should be established. Once Connection established then only those changes will be reflected in Database.

WebRowSet
☀ It is the child interface of CachedRowSet.
☀ It is bydefault scrollable and updatable.
☀ It is disconnected and serializable
☀ WebRowSet can publish data to xml files,which are very helpful for enterprise applications.
FileWriter fw = new FileWriter("employee.xml");
rs.writeXml(fw);
☀ We can read XML data into RowSet as follows
FileReader fr = new FileReader("employee.xml");
rs.readXml(fr);

JoinRowSet
☀ It is the child interface of WebRowSet.
☀ It is by default scrollable and updatable
☀ It is disconnected and serializable
☀ If we want to join rows from different rowsets into a single rowset based on matched column(common column) then we should go for JoinRowSet.
☀ We can add RowSets to the JoinRowSet by using addRowSet() method.
addRowSet(RowSet rs,int commonColumnIndex);

FilteredRowSet
☀ It is the child interface of WebRowSet.
☀ If we want to filter rows based on some condition then we should go for FilteredRowSet.
☀ We can define the filter by implementing Predicate interface.

Differences Between ResultSet and RowSet



No comments:

Post a Comment