JdbcRowSet with program in java


What is RowSet in JDBC java?
javax.sql.RowSet is an interface which can be used as a  >
  • JavaBeans component in a visual Bean development environment,
  • It can be created and configured at design time and can be executed at run time in JDBC java.

RowSet interface extends ResultSet interface in JDBC java.

Following classes implements RowSet interface in java>
Connected implementations of RowSet (connected rowset means it continues to maintain its connection with database after retrieval of data as well)
  • javax.sql.rowset.JdbcRowSet

Disconnected implementations of RowSet in JDBC java
  • javax.sql.rowset.CachedRowSet
  • javax.sql.rowset.WebRowSet
  • javax.sql.rowset.FilteredRowSet
  • javax.sql.rowset.JoinRowSet

More about Connected implementations of Rowset in JDBC java >
JdbcRowSet extends RowSet

More about Disconnected implementations of Rowset in JDBC java >
CachedRowSet extends RowSet
WebRowSet extends CachedRowSet
FilteredRowSet and JoinRowSet extends WebRowSet


What is Connected implementations of RowSet in JDBC java ?
connected rowset means it continues to maintain its connection with database after retrieval of data as well


What is Disconnected implementations of RowSet in JDBC java ?
Disconnected rowset means it doesn’t continues to maintain its connection with database after retrieval of data as well. It disconnects after retrieval of data.



What is JdbcRowSet in JDBC java ?
javax.sql.rowset.JdbcRowSet interface extends RowSet interface.
javax.sql.rowset.JdbcRowSet is a wrapper around a ResultSet.
Being wrapper on ResultSet makes it possible to use the result set as a JavaBeans component.

JdbcRowSet is a connected rowset >
JdbcRowSet is a connected rowset means it continues to maintain its connection with database after retrieval of data as well.

Being scrollable and updatable?
JdbcRowSet can be easily used to make ResultSet scrollable and updatable.
All the javax.sql.RowSet objects are scrollable and updatable by default.


Let’s create Program to use JdbcRowSet in JDBC java >
--Before executing java program execute these database scripts  >
create table EMPLOYEE(id number(4),name varchar2(22));
insert into EMPLOYEE values(1,'ankit');
insert into EMPLOYEE values(2,'rohit');
insert into EMPLOYEE values(3,'amy');
commit;

--If table already exists then execute the DROP command >
drop table EMPLOYEE;

Program/Example : How to use JdbcRowSet in JDBC java >
import java.sql.SQLException;
import javax.sql.rowset.JdbcRowSet;
import javax.sql.rowset.RowSetProvider;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class JdbcRowSetExample {
   public static void main(String... arg) {
          JdbcRowSet jdbcRowSet = null;
          try {
                 // registering Oracle driver class
                 Class.forName("oracle.jdbc.driver.OracleDriver");
                 //create jdbcRowSet object
                 jdbcRowSet = RowSetProvider.newFactory().createJdbcRowSet();
                 jdbcRowSet.setUrl("jdbc:oracle:thin:@localhost:1521:orcl");
                 jdbcRowSet.setUsername("ankit");
                 jdbcRowSet.setPassword("Oracle123");
                 //Fetch data from database using jdbcRowSet
                 jdbcRowSet.setCommand("select * from EMPLOYEE where ID = ?");
                 jdbcRowSet.setString(1, "1");
                 jdbcRowSet.execute();
                
                 System.out.println("jdbcRowSet has created connection, PreparedStatement and RowSet object");
                 //Display fetched data using jdbcRowSet
                 while (jdbcRowSet.next()) {
                       System.out.print(jdbcRowSet.getInt("ID") + " ");
                       System.out.println(jdbcRowSet.getString("NAME"));
                 }
                 jdbcRowSet.close(); // close jdbcRowSet
                
          } catch (ClassNotFoundException e) {
                 e.printStackTrace();
          } catch (SQLException e) {
                 e.printStackTrace();
          }
   }
}
/*OUTPUT
jdbcRowSet has created connection, PreparedStatement and RowSet object
1 ankit
*/

When execute() method of jdbcRowSet is executed connection, PreparedStatement and ResultSet objects are formed in java.

connection - connection between RowSet and database in java.
PreparedStatement - Query which produces ResultSet objects in java.
ResultSet - Data fetched from database in java.



Notifications of events on RowSet object  in JDBC java>
For getting notifications of events on RowSet object we must implement javax.sql.RowSetListener interface.
addRowSetListener method of jdbcRowSet must be used to register the listener.
Program/Example : How to use JdbcRowSet with event listener in JDBC java>
import java.sql.SQLException;
import javax.sql.RowSetEvent;
import javax.sql.RowSetListener;
import javax.sql.rowset.JdbcRowSet;
import javax.sql.rowset.RowSetProvider;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class JdbcRowSetListenerExample {
   public static void main(String... arg) {
          JdbcRowSet jdbcRowSet = null;
          try {
                 // registering Oracle driver class
                 Class.forName("oracle.jdbc.driver.OracleDriver");
                 //create jdbcRowSet instance
                 jdbcRowSet = RowSetProvider.newFactory().createJdbcRowSet();
                 jdbcRowSet.setUrl("jdbc:oracle:thin:@localhost:1521:orcl");
                 jdbcRowSet.setUsername("ankit");
                 jdbcRowSet.setPassword("Oracle123");
                 //Fetch data from database using jdbcRowSet
                 jdbcRowSet.setCommand("select * from EMPLOYEE");
                 jdbcRowSet.execute();
                 //Adding rowListener
                 jdbcRowSet.addRowSetListener(new Listener());
         
                 //Display fetched data using jdbcRowSet
                 while (jdbcRowSet.next()) {
                       System.out.print(jdbcRowSet.getInt("ID") + " ");
                       System.out.println(jdbcRowSet.getString("NAME"));
                 }

                  jdbcRowSet.close(); // close jdbcRowSet

          } catch (ClassNotFoundException e) {
                 e.printStackTrace();
          } catch (SQLException e) {
                 e.printStackTrace();
          }
   }
}
class Listener implements RowSetListener {
   @Override
   public void rowSetChanged(RowSetEvent event) {
          System.out.println("Event - jdbcRowSet has changed its contents.");
   }
   @Override
   public void rowChanged(RowSetEvent event) {
          System.out.println("Event - jdbcRowSet has change in one of its row.");
   }
   @Override
   public void cursorMoved(RowSetEvent event) {
          System.out.println("Event - jdbcRowSet cursor has moved.");
   }
}
/*OUTPUT
Event - jdbcRowSet cursor has moved.
1 ankit
Event - jdbcRowSet cursor has moved.
2 rohit
Event - jdbcRowSet cursor has moved.
3 amy
Event - jdbcRowSet cursor has moved.
*/


Labels: Core Java JDBC
eEdit
Must read for you :