Java Reference
In-Depth Information
changes, regardless of the type of result set, by closing the result set and reopening
it.
The second argument must be one of the two ResultSet constants for specifying
whether a result set is read-only or updateable: CONCUR_READ_ONLY or
CONCUR_UPDATABLE. If you specify a result-set type, you must also specify
whether the result set is read-only or updateable.
The ResultSet.getType() method checks whether the ResultSet object is scrollable:
if(rs.getType()==ResultSet.TYPE_FORWARD_ONLY)
System.out.println("FORWARD_ONLY");
else
System.out.println("SCROLLABLE");
Cursor Control
Once you have a scrollable ResultSet object, you can use it to move the cursor
around in the result set. As with a ResultSet that is not scrollable, the cursor is initially
positioned before the first row.
In addition to the ResultSet.next() method, which is used to move the cursor forward,
one row at a time, scrollable ResultSets support the method ResultSet.previous(),
which moves the cursor back one row.
Both methods return false when the cursor goes beyond the result set (to the position
after the last row or before the first row), which makes it possible to use them in a
while loop. Listing 4-7 replaces the default ResultSet of Listing 4-6 with a scrollable
ResultSet navigated using both next() and previous().
Listing 4-7: Scrollable ResultSet
public void printResultSet(String query){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection ("jdbc:odbc:Inventory");
Statement stmt = con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData md = rs.getMetaData();
int nColumns = md.getColumnCount();
for(int i=1;i<=nColumns;i++){
System.out.print(md.getColumnLabel(i)+((i==nColumns)?"\n":"\t"));
Search WWH ::




Custom Search