Enjoying this Java & JDBC tutorial?
Subscribe to Ram N Java on YouTubeIntroduction
One of the most powerful features of the RowSet technology in Java JDBC is its built-in event handling model. By adhering to the JavaBeans event architecture, a JdbcRowSet can notify registered listeners whenever the cursor moves, a row is modified, or the entire RowSet contents change.
In this guide, you will learn how to implement the RowSetListener interface and attach it to a JdbcRowSet to track and manage database events in real-time.
1. Understand the RowSetListener Interface
The RowSetListener interface provides three callback methods to handle different events:
cursorMoved(RowSetEvent event): Triggered whenever the cursor moves to a new position (e.g., callingnext(),previous(), orabsolute()).rowChanged(RowSetEvent event): Triggered when an individual row is inserted, updated, or deleted.rowSetChanged(RowSetEvent event): Triggered when a change affects the entire RowSet, such as executing a new query viaexecute().
2. Implement the Custom Listener
Create a class implementing RowSetListener to define custom actions when events fire:
import javax.sql.RowSetEvent;
import javax.sql.RowSetListener;
public class MyRowSetListener implements RowSetListener {
@Override
public void cursorMoved(RowSetEvent event) {
System.out.println("[EVENT] Cursor moved to another position.");
}
@Override
public void rowChanged(RowSetEvent event) {
System.out.println("[EVENT] A row has been modified, inserted, or deleted.");
}
@Override
public void rowSetChanged(RowSetEvent event) {
System.out.println("[EVENT] The entire RowSet content has changed.");
}
}
3. Register Listener and Trigger Events
Attach your listener to the JdbcRowSet using addRowSetListener() and perform database actions to trigger the event callbacks:
import javax.sql.rowset.JdbcRowSet;
import javax.sql.rowset.RowSetProvider;
public class JdbcRowSetEventDemo {
public static void main(String[] args) throws Exception {
JdbcRowSet rowSet = RowSetProvider.newFactory().createJdbcRowSet();
rowSet.setUrl("jdbc:mysql://localhost:3306/testdb");
rowSet.setUsername("root");
rowSet.setPassword("password");
rowSet.setCommand("SELECT id, name, salary FROM employee");
// Register the event listener
rowSet.addRowSetListener(new MyRowSetListener());
// Triggers rowSetChanged event
rowSet.execute();
// Triggers cursorMoved events
while (rowSet.next()) {
System.out.println(rowSet.getInt("id") + " : " + rowSet.getString("name"));
}
// Clean up
rowSet.close();
}
}
Watch More Java Tutorials
Click here to watch in Youtube : https://www.youtube.com/watch?v=p8VzKEpG_KE
![]() |
| JDBCRowSet Event Handling Demo |
![]() |
| JDBCRowSet Event Handling Demo |

