Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Jdbc JdbcRowSet interface

Related Tutorials


 
 

Share on Google+Share on Google+

JdbcRowSet interface

Advertisement
In this section we will read about the JdbcRowSet interface in Java.

JdbcRowSet interface

In this section we will read about the JdbcRowSet interface in Java.

javax.sql.rowset.JdbcRowSet is an interface which is a wrapper around a ResultSet object that facilitate to work with the ResultSet object as a JavaBeans component. JdbcRowSet object, an enhanced ResultSet object keeps the connection to its data source like a ResultSet object keeps. JdbcRowSet has a set of properties and listener notification mechanism. JdbcRowSet object makes the ResultSet objects scrollable and updatable.

Object of interface JdbcRowSet can be created by following ways :

1. By a constructor which is implemented by reference and took the object of ResultSet.

2. By a constructor which is implemented by reference and took the object of Connection.

3. By a default constructor which is implemented by reference.

4. Using an instance of RowSetFactory

You can create JdbcRowSet object using com.sun.rowset.JdbcRowSetImpl default constructor as follows :

jrs = new JdbcRowSetImpl();
jrs.setCommand("select * from COFFEES");
jrs.setUrl("jdbc:odbc:swing");
//jrs.setUsername(username);
//jrs.setPassword(password);
jrs.execute();

JdbcRowSet object can be created using RowSetFactory as follows :

RowSetFactory rsf = RowSetProvider.newFactory();
JdbcRowSet jrs = rsf.createJdbcRowSet();
jrs.setUrl("jdbc:odbc:swing");
jrs.setUsername(username);
jrs.setPassword(password);
jrs.setCommand("select * from emp");
jrs.execute();

An easy way to create a JdbcRowSet object is to pass a ResultSet object into the com.sun.rowset.JdbcRowSetImpl constructor. This will create a JdbcRowSet object with the facility to populate it with the ResultSet object's data.

for example :

String sql = "select * from emp";
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();			
JdbcRowSet jrs = new JdbcRowSetImpl(rs);

To create object of JdbcRowSet by passing the Connection object you can pass the Connection object into the com.sun.rowset.JdbcRowSetImpl constructor as follows :

jrs = new JdbcRowSetImpl(conn);
jrs.setCommand("select * from emp");
jrs.execute();

Methods Of JdbcRowSet

  • commit() : This method is used to grant auto commit or non auto commit transactional control support.

    Syntax : void commit() throws SQLException
     
  • getAutoCommit() : This method is used to provide the application to specify the transactional behavior of JdbcRowSet.

    Syntax : boolean getAutoCommit() throws SQLException
     
  • getRowSetWarnings() : This object is used to get the warning generated by the JdbcRowSet object.

    Syntax : RowSetWarning getRowSetWarnings() throws SQLException
     
  • getShowDeleted() : This method specifies whether the row is marked for deleting or not. In case of value is returned to true then the deleted rows will be visible with the current rows otherwise not visible. Default value is false.

    Syntax : boolean getShowDeleted() throws SQLException
     
  • rollback() : This method is used to cancel the current transaction's changes and make free from the database locks (if) held by the Connection object.

    Syntax : void rollback() throws SQLException
     
  • rollback(Savepoint s) : This method is used to cancel the current transaction's changes to the last set savepoint and make free from the database locks (if) held by the Connection object.

    Syntax : void rollback(Savepoint s) throws SQLException
     
  • setAutoCommit(boolean autoCommit) : This method grants an application for setting the JdbcRowSet transaction behavior.

    Syntax : void setAutoCommit(boolean autoCommit) throws SQLException
     
  • setShowDeleted(boolean b) : This method is used to set the showDeleted value with the specified boolean value.

    Syntax : void setShowDeleted(boolean b) throws SQLException

Example

Here an example is being given which demonstrates about how to create object of JdbcRowSet and how its object manipulates with the ResultSet object. Here in this example I have created object by passing the ResultSet object to JdbcRowSetImpl(). In this example I have created a Java class where loaded a driver and make a connection with the database and write a SQL statement to display all the records in the emp table. The main thing is I have passed the two constants ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE which makes the ResultSet object scrollable and updatable. This is required if the createStatement() method is used without passing the parameter the ResultSet object will not be the scrollable and updatable and neither the JdbcRowSet object.

Source Code

JavaJDBCRowSetExample.java

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

Output

When you will execute the above example you will get the output as follows :

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 4, 2013

Related Tutorials

Discuss: JdbcRowSet interface  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com