Thursday 5 December 2013

RowSets

       We can send only those java objects over the network that are Serializable.
       In order to make object as Serializable object, the class of the object must implement java.io.Serializable interface.
       In order to store data of the object in a file, the object must be serializable object.
       ResultSet object is not Serializable object, so we can not send this object over the network directly.
       To overcome this problem, there are two solutions
       1. Use RowSets in the place of ResultSets.
       2. copy data of ResultSet object to RowSet
       Rowsets are Serializable objects and given as enhancement of ResultSet
       All collection framework dataStructures are Serializable objects by default.
       RowSet object means it is the object of a class that implements javax.sql.RowSet interface
       RowSets are introduced to JDBC from JDBC 2.0
TYPES OF ROWSETS
       There are 3 types of RowSets
       1. Cached RowSet
       Disconnected Rowset, like insensitive ResultSet.
       2.JDBC Rowset: like sensitive ResultSet
       3.Web Rowset: connected RowSet->like Sensitive ResultSet, userful to store xml data collected from the table
       Cached RowSet, JDBC RowSet can not deal with xml data as column values. But web RowSet can do this work.
       The oracle thin driver, OCI driver is giving support for two types of RowSets. (catched RowSet, JDBC RowSet).
       The oracle corporation supplies two classes having the functionality of cached, JDBC RowSet.
       They are :
       1. oracle.jdbc.RowSet.OracleCachedRowSet.
       2.
       These two classes are there oracle-home\ora92\jdbc\lib\ocrs12.jar file
       So we need to add it in classpath.
       If one jar file classes are using the classes of other jar files then other jar files are called dependent jar files to main jar file.
       When java application is using classes of main jar file and these classes are having dependent classes in other jar files, then we need to execute both main and dependent jar files in the classpath.
       Ojdbc14.jar file is not having dependent jar files. But this ojdbc14.jar file is the dependent jar file for ocrs12.jar file.
       In order to work with Oracle corporation supplied RowSets, we need to add ocrs12.jar file, ojdbc14.jar files in claspath environment variable.
Advantages of RowSets
       1. RowSet  object are Serializable objects. So we can send them directly over the network.
       2. we can perform select operations on the table by simply working with setXXX(), getXXX() methods and there is no need of working with the regular JDBC objects like Connection, ResultSet etc.
Limitations of RowSets
       1. RowSets are given only for select operations. So non-select operations are not possible.
RowSet example program
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.rowset.JdbcRowSet;

import com.sun.rowset.JdbcRowSetImpl;

public class JavaJDBCRowSetExample {

    public static void main(String[] args) throws SQLException {

        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection("jdbc:odbc:swing");
            String sql = "select * from emp";
            Statement ps = con.createStatement(1004, 1008);
            ResultSet rs = ps.executeQuery(sql);           
            JdbcRowSet jdbcRs = new JdbcRowSetImpl(rs);
            jdbcRs.afterLast();
            System.out.println("Records before deleting of row");
            System.out.println("id \t fName \t lName \t address");
            while (jdbcRs.previous())
            {
                int id = jdbcRs.getInt("id");
                String fName = jdbcRs.getString("fName");
                String lName = jdbcRs.getString("lName");
                String address = jdbcRs.getString("address");              
                System.out.println(id+" \t "+fName+" \t "+lName+" \t "+address);           
            }
            boolean bol = jdbcRs.absolute(3);              
            int i = jdbcRs.getRow();
            System.out.println("Deleting row no. "+i+".....");
            jdbcRs.deleteRow();
            jdbcRs.refreshRow();
            System.out.println("row no. "+i+" is deleted");
            jdbcRs.afterLast();
            System.out.println("Records after deleting of row");
            System.out.println("id \t fName \t lName \t address");
            while (jdbcRs.previous())
            {
                int id = jdbcRs.getInt("id");
                String fName = jdbcRs.getString("fName");
                String lName = jdbcRs.getString("lName");
                String address = jdbcRs.getString("address");              
                System.out.println(id+" \t "+fName+" \t "+lName+" \t "+address);           
            }
            rs.close();
            ps.close();
        }
        catch(Exception ex)
        {
            System.out.println(ex);
        }
    }
}

No comments:

Post a Comment