RowSet is an interface available in javax.sql package.
RowSet interface is extending ResultSet interface.
RowSet interface implementation classes are provided by Java vendor.
RowSet funcationality is similar to ResultSet.
ResultSet | RowSet |
---|---|
ResultSet object is used to store records returned by SELECT SQL Statement. | ResultSet object is used to store records returned by SELECT SQL Statement. |
ResultSet object can be created as follows : con=D.M.getConnection(url,un,pw); st=con.createStatement(); rs=st.executeQuery(sql) |
ResultSet object can be created as follows : RowSet jrs = new JdbcRowSetImpl(); jrs.setUrl(url); jrs.setUsername(un); jrs.setPassword(pw); jrs.setCommand(Sql); jrs.execute(); |
By Default, ReultSets are forward-only and read-only. | By Default, ReultSets are scrolled and updatable. |
ResultSet objects are connection oriented i.e you can access the ResultSet data as long as connection is available. Once Connection is closed, Result also will be closed automatically. | ResultSet are connection less object i.e you can access the RowSet data without Connection. |
ResultSet objects are not eligible for Serialization. | ResultSet objects are eligible for Serialization. |
Following are subtypes of RowSet interface:
JdbcRowSet interface.
CachedRowSet interface.
WebRowSet interface.
JoinRowSet interface.
Types of Rowsets:
Connected RowSets
Disconnected RowSets
Connected RowSets:
These rowsets establish a connection with the database and retain it.
Connected RowSets are like ResultSets i.e Connected RowSets needs the connection as long as you are accessing the RowSet data.
You can not serialize connected rowsets.
JdbcRowSet is connected rowSet.
Disconnected RowSets:
These rowsets establishes a connection, executes a query and closes the connection.
Disconnected RowSets are not like ResultSets i.e Disconnected RowSets do not need the connection always while you access the RowSet data.
You can serialize disconnected rowsets.
CachedRowSet is disconnected rowset.
package com.jtcindia.jdbc;
import java.sql.SQLException;
import javax.sql.RowSet;
import com.sun.rowset.CachedRowSetImpl;
import com.sun.rowset.JdbcRowSetImpl;
public class Lab23 {
public static void main(String[] args) {
RowSet myrowset = null;
try {
String SQL = "select * from mycustomers where city='Noida'";
//myrowset = new JdbcRowSetImpl();
myrowset = new CachedRowSetImpl();
myrowset.setUrl("jdbc:mysql://localhost:3306/myjdbcdb");
myrowset.setUsername("root");
myrowset.setPassword("india");
myrowset.setCommand(SQL);
myrowset.execute(); // ***
while (myrowset.next()) {
int cid = myrowset.getInt(1);
String cn = myrowset.getString(2);
String em = myrowset.getString(3);
int ph = myrowset.getInt(4);
String ci = myrowset.getString(5);
System.out.println(cid + "\t" + cn + "\t" + em + "\t" + ph + "\t" + ci);
}
System.out.println("----Done-----");
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
myrowset.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}