In this section we will read about the JdbcRowSet interface in Java.
javax.sql.rowset.JdbcRowSet is an interface which is a wrapper around a ResultSet object that facilitate to work with the ResultSet object as a JavaBeans component. JdbcRowSet object, an enhanced ResultSet object keeps the connection to its data source like a ResultSet object keeps. JdbcRowSet has a set of properties and listener notification mechanism. JdbcRowSet object makes the ResultSet objects scrollable and updatable.
Object of interface JdbcRowSet can be created by following ways :
1. By a constructor which is implemented by reference and took the object of ResultSet.
2. By a constructor which is implemented by reference and took the object of Connection.
3. By a default constructor which is implemented by reference.
4. Using an instance of RowSetFactory
You can create JdbcRowSet object using com.sun.rowset.JdbcRowSetImpl default constructor as follows :
jrs = new JdbcRowSetImpl();
jrs.setCommand("select * from COFFEES");
jrs.setUrl("jdbc:odbc:swing");
//jrs.setUsername(username);
//jrs.setPassword(password);
jrs.execute();
JdbcRowSet object can be created using RowSetFactory as follows :
RowSetFactory rsf = RowSetProvider.newFactory();
JdbcRowSet jrs = rsf.createJdbcRowSet();
jrs.setUrl("jdbc:odbc:swing");
jrs.setUsername(username);
jrs.setPassword(password);
jrs.setCommand("select * from emp");
jrs.execute();
An easy way to create a JdbcRowSet object is to pass a ResultSet object into the com.sun.rowset.JdbcRowSetImpl constructor. This will create a JdbcRowSet object with the facility to populate it with the ResultSet object's data.
for example :
String sql = "select * from emp"; PreparedStatement ps = con.prepareStatement(sql); ResultSet rs = ps.executeQuery(); JdbcRowSet jrs = new JdbcRowSetImpl(rs);
To create object of JdbcRowSet by passing the Connection object you can pass the Connection object into the com.sun.rowset.JdbcRowSetImpl constructor as follows :
jrs = new JdbcRowSetImpl(conn);
jrs.setCommand("select * from emp");
jrs.execute();
Methods Of JdbcRowSet
Example
Here an example is being given which demonstrates about how to create object of JdbcRowSet and how its object manipulates with the ResultSet object. Here in this example I have created object by passing the ResultSet object to JdbcRowSetImpl(). In this example I have created a Java class where loaded a driver and make a connection with the database and write a SQL statement to display all the records in the emp table. The main thing is I have passed the two constants ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE which makes the ResultSet object scrollable and updatable. This is required if the createStatement() method is used without passing the parameter the ResultSet object will not be the scrollable and updatable and neither the JdbcRowSet object.
Source Code
JavaJDBCRowSetExample.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.rowset.JdbcRowSet;
import com.sun.rowset.JdbcRowSetImpl;
public class JavaJDBCRowSetExample {
public static void main(String[] args) {
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.
getConnection("jdbc:odbc:swing");
String sql = "select * from emp";
Statement ps = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = ps.executeQuery(sql);
JdbcRowSet jrs = new JdbcRowSetImpl(rs);
jrs.afterLast();
System.out.println("Records before deleting of row");
System.out.println("id \t fName \t lName \t address");
while (jrs.previous())
{
int id = jrs.getInt("id");
String fName = jrs.getString("fName");
String lName = jrs.getString("lName");
String address = jrs.getString("address");
System.out.println(id+" \t "+fName+" \t "+lName
+" \t "+address);
}
boolean bol = jrs.absolute(3);
int i = jrs.getRow();
System.out.println("Deleting row no. "+i+".....");
jrs.deleteRow();
jrs.refreshRow();
System.out.println("row no. "+i+" is deleted");
jrs.afterLast();
System.out.println("Records after deleting of row");
System.out.println("id \t fName \t lName " +
"\t address");
while (jrs.previous())
{
int id = jrs.getInt("id");
String fName = jrs.getString("fName");
String lName = jrs.getString("lName");
String address = jrs.getString("address");
System.out.println(id+" \t "+fName+" \t "+lName
+" \t "+address);
}
rs.close();
ps.close();
}
catch(SQLException sex)
{
System.out.println(sex);
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
Output
When you will execute the above example you will get the output as follows :

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: JdbcRowSet interface
Post your Comment