Exploring JDBC and XML (Page 1 of 4 )
For those who want to delve deeply into Java, this article continues our exploration of Java Database Connectivity (JDBC), examines JDBC drivers, and introduces you to using XML. The second of three parts, it is excerpted from chapter 20 of the book
Sams Teach Yourself Java 2 in 21 Days, 4th Edition, written by Rogers Cadenhead and Laura Lemay (Sams; ISBN: 0672326280).
Moving Through Resultsets
The default behavior of resultsets permits one trip through the set using its next() method to retrieve each record.
By changing how statements and prepared statements are created, you can produce resultsets that support these additional methods:
afterLast()—Moves to a place immediately after the last record in the set
beforeFirst()—Moves to a place immediately before the first record in the set
first()—Moves to the first record in the set
last()—Moves to the last record in the set
previous()—Moves to the previous record in the set
These actions are possible when the resultset's policies have been specified as arguments to a database connection's createStatement() and prepareStatement() methods.
Normally, createStatement() takes no arguments, as in this example:
Connection payday = DriverManager.getConnection(
"jdbc:odbc:Payroll", "Doc", "1rover1");
Statement lookSee = payday.CreateStatement();
For a more flexible resultset, call createStatement() with three integer arguments that set up how it can be used. Here's a rewrite of the preceding statement:
Statement lookSee = payday.CreateStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY,
ResultSet.CLOSE_CURSORS_AT_COMMIT);
The same three arguments can be used in the prepareStatement(String, int, int, int) method after the text of the statement.
The ResultSet class includes other class variables that offer more options in how sets can be read and modified.
Next: JDBC Drivers >>
More Java Articles
More By Sams Publishing
|
This article is excerpted from chapter 20 of the book Sams Teach Yourself Java 2 in 21 Days, 4th Edition, written by Rogers Cadenhead and Laura Lemay (Sams; ISBN: 0672326280). Check it out today at your favorite bookstore. Buy this book now.
|
|