Share on Google+Share on Google+

JDBC RowSet Example

In this tutorial you will learn about javax.sql.JdbcRowSet interface and also learn about Jdbc RowSet Listener

JDBC RowSet Example

JDBC RowSet is an interface of javax.sql.rowset interface. This interface is wrapper around a ResultSet object that makes possible to use resultSet as java beans object. It can be one bean that makes available for composing of an application. Because a it continually maintain a connection JDBC connection to the database.

Another advantage of JDBC RowSet is that it is used to makes ResultSet object scrollable and updateable. By default all the RowSet object are scrollable and updateable.

An Example of Row Set Event listener is given below, To run this example at first create a database name student and create a table also named student

CREATE TABLE student (
Name tinytext NOT NULL,
Course varchar(25) NOT NULL,
Address text

Then insert the value into it as

NSERT INTO student VALUES(1, 'Ram', 'B.Tech', 'Delhi') ;

NSERT INTO student VALUES(2, 'Syam', 'M.Tech', 'Mumbai') ;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.sql.RowSetEvent;
import javax.sql.RowSetListener;
import javax.sql.rowset.JdbcRowSet;

import com.sun.rowset.JdbcRowSetImpl;

public class JDBCRowSetExample {
	public static void main(String[] args) throws Exception {
		Connection connection = getMySqlConnection();
		System.out.println("Connection Done");
		Statement statement = connection.createStatement();
		JdbcRowSet jdbcRowSet;
		jdbcRowSet = new JdbcRowSetImpl(connection);
		String queryString = "SELECT * FROM student";
		jdbcRowSet.addRowSetListener(new ExampleListener());

		while ( {
			// Generating cursor Moved event
			System.out.println("Roll No- " + jdbcRowSet.getString(1));
			System.out.println("name- " + jdbcRowSet.getString(2));

	// My Sql connection method
	public static Connection getMySqlConnection() throws Exception {
		String driver = "com.mysql.jdbc.Driver";
		String url = "jdbc:mysql://localhost:3306/student";
		String username = "root";
		String password = "root";

		Connection connection = DriverManager.getConnection(url, username,
		return connection;


class ExampleListener implements RowSetListener {

	public void cursorMoved(RowSetEvent event) {
		// TODO Auto-generated method stub
		System.out.println("Cursor Moved Listener");

	public void rowChanged(RowSetEvent event) {
		// TODO Auto-generated method stub
		System.out.println("Cursor Changed Listener");

	public void rowSetChanged(RowSetEvent event) {
		// TODO Auto-generated method stub
		System.out.println("RowSet changed Listener");

When you run this application it will display message as shown below:

Connection Done
Cursor Moved Listener
javax.sql.RowSetEvent[[email protected]]
Roll No- 1
name- vnay
Cursor Moved Listener
javax.sql.RowSetEvent[[email protected]]
Roll No- 2
name- John
Cursor Moved Listener
javax.sql.RowSetEvent[[email protected]]

Download this example code


Posted on: October 18, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null