RowSet: - It is an object that combines a set of rows from either JDBC result sets or tabular data sources. It supports component-based development models such as JavaBeans, with a standard set of properties and an event notification mechanism.
Types of RowSets:-
Implementation classes are:-
- com.sun.rowset.CachedRowSetImpl
- com.sun.rowset.JdbcRowSetImpl
- com.sun.rowset.WebRowSetImpl
- com.sun.rowset.FilteredRowSetImpl
- com.sun.rowset.JoinRowSetImpl
RowSets are divided into two categories:-
- Connected Rowset :- The connected rowset is connected to the db connection object like resultset. It is making the permanent connection and doesn’t terminate until the application is terminated.Example :- JdbcRowSet.
- Disconnected RowSet: - A disconnected RowSet retrieves the data from DB whenever requires, it establishes the connection and closes it after finishing the required task. The data that is modified during disconnected state is updated after the connection is re-established. Example:- CachedRowSet, WebRowSet, FilteredRowSet, JoinRowSet.
RowSet Sample Code:-
package com.gaurav.jdbcexamples;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.RowSetEvent;
import javax.sql.RowSetListener;
import javax.sql.rowset.JdbcRowSet;
import com.sun.rowset.JdbcRowSetImpl;
public class JDBCRowSetDemo {
privatestatic JdbcRowSet jdbcRowSet = null;
privatestatic Connection con = null;
publicstatic Connection getMySqlConnection() throws Exception {
String JDBC_DRIVER = "com.mysql.jdbc.Driver"; // MySQL Driver filename
String USERNAME = "root";
String PASSWORD = "root";
String DATABASE_URL = "jdbc:mysql://localhost:3306/test";
Class.forName(JDBC_DRIVER);
Connection connection = DriverManager.getConnection(DATABASE_URL,
USERNAME, PASSWORD);
return connection;
}
publicstatic void main(String... args) {
try {
con = getMySqlConnection();
System.out.println(" SUCCESSFULLY GOT THE CONNECTION ");
jdbcRowSet = new JdbcRowSetImpl(con);
jdbcRowSet.setType(ResultSet.TYPE_SCROLL_INSENSITIVE);
String query = "SELECT * FROM student";
jdbcRowSet.setCommand(query);
jdbcRowSet.execute();
jdbcRowSet.addRowSetListener(new TestListener());
System.out
.println("------------------- ROWSET IS MOVING FORWARD ------------------------------");
while (jdbcRowSet.next()) {
// cursor Movement event occurs here
System.out.println("STUDENT_ID - " + jdbcRowSet.getInt(1));
System.out.println("FIRSTNAME - " + jdbcRowSet.getString(2));
}
System.out
.println("------------------- ROWSET IS MOVING BACK ------------------------------");
while (jdbcRowSet.previous()) {
// cursor Movement event occurs here
System.out.println("STUDENT_ID - " + jdbcRowSet.getInt(1));
System.out.println("FIRSTNAME - " + jdbcRowSet.getString(2));
}
System.out
.println("------------------ ROWSET IS MOVING RANDOMLY -----------------------------");
boolean isAbsoulteRow = jdbcRowSet.absolute(2);
System.out.println("isAbsoulteRow->" + isAbsoulteRow);
} catch (SQLException sqe) {
sqe.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (jdbcRowSet != null)
jdbcRowSet.close();
if (con != null)
con.close();
} catch (Exception e) {
e.getMessage();
}
}
}
}
class TestListener implements RowSetListener {
@Override
publicvoid cursorMoved(RowSetEvent event) {
System.out.println("EVENT OCCURRED FOR CURSOR MOVEMENT");
}
@Override
publicvoid rowChanged(RowSetEvent event) {
System.out.println("EVENT OCCURRED FOR CURSOR CHANGED");
}
@Override
publicvoid rowSetChanged(RowSetEvent event) {
System.out.println("EVENT OCCURRED FOR ROWSET CHANGED");
}
}
Result:-