Sunday, 4 January 2015

What is RowSet Interface in JDBC

JDBC RowSet:

The instance of RowSet is the java bean component because it has properties and java bean notification mechanism. It is introduced since JDK 5. It is the wrapper of ResultSet. It holds tabular data like ResultSet but it is easy and flexible to use.

The implementation classes of RowSet interface are given below:

JdbcRowSet
CachedRowSet
WebRowSet
JoinRowSet
FilteredRowSet

 See how to create and execute RowSet 

 JdbcRowSet rowSet = RowSetProvider.newFactory().createJdbcRowSet();
 rowSet.setUrl("jdbc:oracle:thin:@localhost:1521:xe");
 rowSet.setUsername("system");
 rowSet.setPassword("oracle");
 rowSet.setCommand("select * from student");
 rowSet.execute();

 Note: It is the new way to get the instance of JdbcRowSet since JDK 7.

 Advantage of RowSet:

>1. It is easy and flexible to use.
>2. It is Scrollable and Updatable by default.

Example of JdbcRowSet

import java.sql.*;
import javax.sql.rowset.JdbcRowSet;
import javax.sql.rowset.RowSetProvider;
 public class RowSetExample {
 public static void main(String[]args) throws Exception {
 Class.forName("oracle.jdbc.driver.OracleDriver");
 //Creating and Executing RowSet
 JdbcRowSet rowSet = RowSetProvider.newFactory().createJdbcRowSet();
 rowSet.setUrl("jdbc:oracle:thin:@localhost:1521:xe");
rowSet.setUsername("system");
 rowSet.setPassword("oracle");
 rowSet.setCommand("select * from student");
 rowSet.execute();
 while (rowSet.next()) {

 System.out.println("Id: " + rowSet.getString(1));
 System.out.println("Name: " + rowSet.getString(2));
 System.out.println("Salary: " + rowSet.getString(3));
  System.out.println("Address: " + rowSet.getString(3));
   }
    }
 }

No comments:

Post a Comment