Simply as DataSource objects present an alternative choice to DriverManager, RowSet objects present an alternative choice to ResultSet objects. The RowSet interface extends the ResultSet interface, giving you a similar performance for viewing and manipulating information plus further performance that makes it a versatile and highly effective object to make use of in your purposes.
A RowSet object allows you to use information retrieved from a knowledge supply in many alternative methods. For instance, utilizing rowsets, you possibly can carry out the next duties:
- Move populated RowSet objects over networks to skinny shoppers like PDAs
- View and replace information in disconnected environments
- Mimic scrollable and updateable consequence units for drivers that don’t assist these options
- Generate self-contained XML paperwork representing RowSet objects
RowSet Modes:
Rowsets function in two modes: linked and disconnected.
1) Related RowSet: A linked rowset, like a consequence set, requires a steady connection to the database. An error happens when you use both a ResultSet or RowSet object after explicitly closing the connection or it in any other case breaks unexpectedly. As well as, a linked rowset all the time requires the presence of a JDBC driver so it might probably talk with the database.
2) Disconnected RowSet: A disconnected rowset, against this, shops row and column information in reminiscence. Consequently, the item doesn’t depend on a knowledge supply connection to view and manipulate the info; it simply accesses it from reminiscence. Nevertheless, you do want a connection to initially populate a disconnected RowSet object. As soon as populated, the item disconnects from the info supply routinely.
RowSet Varieties:
1) JdbcRowSet: The JdbcRowSet class supplies a fundamental implementation of the javax.sql.RowSet interface, which turns a ResultSet object right into a JavaBean and abstracts the main points of working with ResultSet objects. You should use this object to simplify connection steps or to supply scrollable and updateable cursors for drivers that don’t assist these options. Nevertheless, JdbcRowSet objects can solely function in linked mode, thus requiring the presence of a JDBC driver.
2) Cached RowSet: The CachedRowSet class supplies the identical performance because the JdbcRowSet class, with one vital distinction: a CachedRowSet object can function in a disconnected surroundings. Consequently, it might probably perform with no JDBC driver current. When you populate a CachedRowSet object with information chances are you’ll ship it over the community to skinny shoppers, present it to gross sales professionals on their laptops, or serialize it for information archiving. The benefit of the CachedRowSet object is that the shopper to whom you ship the item doesn’t want the JDBC driver.
3) Net RowSet:The WebRowSet class extends the CachedRowSet class, which helps you to use the identical properties and strategies in addition to function in disconnected mode. Nevertheless, the WebRowSet object can generate XML paperwork representing itself. You should use these paperwork to create copies of the WebRowSet object, which makes it straightforward so that you can distribute the knowledge throughout the Net and thru firewalls utilizing HTTP. It can seemingly play a significant function because the Net-services structure continues to develop.
RowSet Comparision:
Following code display find out how to implement JdbcRowSet utilizing Oracle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
package deal org.techzoo.rowset;
import java.sql.SQLException;
import oracle.jdbc.rowset.OracleJDBCRowSet;
public class JdbcRowSetDemo {
public static void foremost(String[] args){
attempt {
//Instantiate a OracleJDBCRowSet object
// OracleJDBCRowSet is a Oracle implementation
OracleJDBCRowSet jrs = new OracleJDBCRowSet();
//Load driver and set connection parameters
Class.forName(“oracle.jdbc.driver.OracleDriver”);
jrs.setUrl(“jdbc:oracle:skinny:@localhost:1521:khan_db”);
jrs.setUsername(“scott”);
jrs.setPassword(“tiger”);
//Set and execute the command
String sql;
sql = “SELECT SSN, Title, Wage, Hiredate FROM Staff”;
jrs.setCommand(sql);
jrs.execute();
//Show values
whereas(jrs.subsequent()){
System.out.print(“SSN: “ + jrs.getInt(“ssn”));
System.out.print(“, Title: “ + jrs.getString(“identify”));
System.out.print(“, Wage: $” + jrs.getDouble(“wage”));
System.out.print(“, HireDate: “ + jrs.getDate(“hiredate”));
System.out.println();
}
//Shut the useful resource
jrs.shut();
}catch (SQLException se){
se.printStackTrace();
}catch (Exception ex) {
ex.printStackTrace();
}
//Say goodbye
System.out.println(“Goodbye!”);
}//finish foremost
}// finish JdbcRowSetDemo
|
CacheRowSet implementation utilizing Oracle…
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
import java.io.*;
import java.sql.SQLException;
import oracle.jdbc.rowset.OracleCachedRowSet;
package deal org.techzoo.rowset;
public class CachedRowSetDemo {
//Fixed to carry file identify used to retailer the CachedRowSet
personal closing static String CRS_FILE_LOC =“cachedrs.crs”;
public static void foremost(String[] args) throws Exception {
attempt {
//Create serialized CachedRowSet
writeCachedRowSet();
//Create CachedRowSet from serialized object
OracleCachedRowSet crs = readCachedRowSet();
//Show values
whereas(crs.subsequent()){
System.out.print(“SSN: “ + crs.getInt(“ssn”));
System.out.print(“, Title: “ + crs.getString(“identify”));
System.out.print(“, Wage: $” + crs.getDouble(“wage”));
System.out.print(“, HireDate: “ + crs.getDate(“hiredate”));
System.out.println();
}
//Shut useful resource
crs.shut();
} catch (SQLException se){
se.printStackTrace();
}catch (Exception ex) {
ex.printStackTrace();
}
}//finish foremost
public static void writeCachedRowSet() throws Exception {
//Instantiate a CachedRowSet object, set connection parameters
OracleCachedRowSet crs = new OracleCachedRowSet();
Class.forName(“oracle.jdbc.driver.OracleDriver”);
crs.setUrl(“jdbc:oracle:skinny:@localhost:1521:khan_db”);
crs.setUsername(“scott”);
crs.setPassword(“tiger”);
//Set and execute the command. Discover the parameter question.
String sql = “SELECT SSN, Title, Wage, Hiredate “;
sql = sql + “FROM Staff WHERE SSN=?”;
crs.setCommand(sql);
crs.setInt(1,123456);
crs.execute();
//Serialize CachedRowSet object.
FileOutputStream fos = new FileOutputStream(CRS_FILE_LOC);
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(crs);
out.shut();
crs.shut();
}//finish writeCachedRowSet()
public static OracleCachedRowSet readCachedRowSet() throws Exception{
//Learn serialized CachedRowSet object from storage
FileInputStream fis = new FileInputStream(CRS_FILE_LOC);
ObjectInputStream in = new ObjectInputStream(fis);
OracleCachedRowSet crs = (OracleCachedRowSet)in.readObject();
fis.shut();
in.shut();
return crs;
}//finish readCachedRowSet()
}//finish CachedRS
|
and Lastly WebRowSet…
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
package deal org.techzoo.rowset;
import java.io.*;
import java.sql.SQLException;
import oracle.jdbc.rowset.OracleWebRowSet;
public class WebRowSetDemo {
//Fixed representing the XML file
personal static String WRS_FILE_LOC =“wrs.xml”;
public closing static void foremost(String[] args) throws Exception {
attempt {
//Instantiate a WebRowSet object
OracleWebRowSet wrs = new OracleWebRowSet();
//Load driver and set connection parameters
Class.forName(“oracle.jdbc.driver.OracleDriver”);
wrs.setUrl(“jdbc:oracle:skinny:@localhost:1521:khan_db”);
wrs.setUsername(“scott”);
wrs.setPassword(“tiger”);
//Configure command and execute
System.out.println(“Connecting to information supply and “ +
“producing XML doc.”);
String sql = “SELECT ssn, identify FROM Staff”;
wrs.setCommand(sql);
wrs.execute();
//Write XML out to file
System.out.println(“Writing XML to file: “ + WRS_FILE_LOC);
FileWriter fw = new FileWriter(WRS_FILE_LOC);
wrs.writeXml(fw);
fw.shut();
wrs.shut();
System.out.println(“Completed writing XML file.”);
}catch (SQLException se){
se.printStackTrace();
}catch (Exception ex) {
ex.printStackTrace();
}
System.out.println(“Goodbye!”);
}//finish foremost()
}//finish WebRS class
|
Supply techzoo.org