Home Tutorial Java Jdbc JDBC RowSet Example

 
 

JDBC RowSet Example
Posted on: October 18, 2010 at 12:00 AM
Advertisement
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 (
RollNo int(9)  PRIMARY KEY NOT NULL,
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') ;

JDBCRowSetExample.java

package roseindia.net;

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);
		jdbcRowSet.setType(ResultSet.TYPE_SCROLL_INSENSITIVE);
		String queryString = "SELECT * FROM student";
		jdbcRowSet.setCommand(queryString);
		jdbcRowSet.execute();
		jdbcRowSet.addRowSetListener(new ExampleListener());

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

	// 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";

		Class.forName(driver);
		Connection connection = DriverManager.getConnection(url, username,
				password);
		return connection;
	}

}

class ExampleListener implements RowSetListener {

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

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

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

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


Connection Done
Cursor Moved Listener
javax.sql.RowSetEvent[source=com.sun.rowset.JdbcRowSetImpl@85af80]
Roll No- 1
name- vnay
Cursor Moved Listener
javax.sql.RowSetEvent[source=com.sun.rowset.JdbcRowSetImpl@85af80]
Roll No- 2
name- John
Cursor Moved Listener
javax.sql.RowSetEvent[source=com.sun.rowset.JdbcRowSetImpl@85af80]

Download this example code

Advertisement

Related Tags for JDBC RowSet Example:


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.

Posted on: October 18, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com