Java Reference
In-Depth Information
The Java 7 RowSetFactory has the static methods shown in
Table 18-4
to create RowSet in-
stances. Prior to Java 7, you had to instantiate the corresponding “Impl” classes from the un-
supported
com.sun.rowset
package.
is created and populated with
setCommand()
and
execute()
. Then (hypothetically some
time later) the user changes some data. After that is completed, we call
updateRow()
, which
tells the
CachedRowSet
to put the changes back into the JDBC database.
Example 18-8. CachedRowSetDemo
public
public class
class
CachedRowSetDemo
CachedRowSetDemo
{
public
public static
static
void
void
main
(
String
[]
args
)
throws
throws
Exception
{
RowSet rs
;
RowSetFactory rsFactory
=
RowSetProvider
.
newFactory
();
rs
=
rsFactory
.
createCachedRowSet
();
rs
.
setUrl
(
"jdbc:postgresql:tmclub"
);
rs
.
setUsername
(
"ian"
);
rs
.
setPassword
(
"secret"
);
rs
.
setCommand
(
"select * from members where name like ?"
);
rs
.
setString
(
1
,
"I%"
);
// This will cause the RowSet to connect, fetch its data, and
// disconnect
rs
.
execute
();
// Some time later, the client tries to do something.
// Suppose we want to update data:
while
while
(
rs
.
next
()) {
iif
(
rs
.
getInt
(
"id"
) ==
42
) {
rs
.
setString
(
1
,
"Marvin"
);
rs
.
updateRow
();
// Normal JDBC
// This additional call tells the CachedRowSet to connect
// to its database and send the updated data back.
rs
.
updateRow
();
}
}
// If we're all done...
rs
.
close
();