
A RowSet object is a java bean component and extends the ResultSet interface Thus, it has a set of JavaBeans properties and follows the JavaBeans event model. A RowSet object's properties allow it to establish its own database connection and to execute its own query in order to fill itself with data
Types of Rowset
The RowSet is majorly classified into two types as per their properties:-
1. Connected Rowset.:- The connected rowset as the name suggests in connected to the database connection object like the resultset. The JDBCRowSet is the example of the connected RowSet.
2. Disconnected RowSet:- The disconnected RowSet only connects to the database whenever required and after finishing the interaction they close the database connection. So if the connection pool is minimally used in this case.
There are following ways to create the JDBCRowSet:-
1. Passing the ResultSet:- In this approach the data is populated in the object and then you can retrieve the data by using the getter methods as we did in case of the ResultSet. Exmple:-
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(“select * from employee”);
JdbcRowSet jdbcRs = new JdbcRowSetImpl(rs);
2. Creating the default object:- This approach is useful if you want to set the data sources dynamically. In this approach the Database URL, username and password is explicitly set in the RowSetObject.Exmple:-
JdbcRowSet jdbcRs = new JdbcRowSetImpl();
jdbcRs.setUsername("user");
jdbcRs.setPassword("password");
jdbcRs.setUrl("jdbc:mySubprotocol:mySubname");
jdbcRs.setCommand("select * from EMP ");
jdbcRs.execute();
The following example will illustrate the basic operation using the JDBCRowSet interface.
package com.visualbuilder;
import java.sql.SQLException;
import javax.sql.rowset.JdbcRowSet;
import com.sun.rowset.JdbcRowSetImpl;
public class JDBCRowSetExample {
public static void main(String[] args) throws SQLException{
JdbcRowSet jdbcRs = new JdbcRowSetImpl();
jdbcRs.setUsername("scott");
jdbcRs.setPassword("tiger");
jdbcRs.setUrl("jdbc:odbc:MyDsn");
jdbcRs.setCommand("select * from employee");
jdbcRs.execute();
while(jdbcRs.next()) {
System.out.println(jdbcRs.getString("ename"));
}
}
}
Ouput:-
Adam
Susan
Adran
Hardy
Tim
Michael
Java Discussion
- - Interviewing Next week -
- - Sudoku solver
- - Setting tab order in swin
- - Java opportunities
- - Struts


