You can use a ResultSet object to access a table of data that was generated by running a query. The table rows are retrieved in sequence. Within a row, column values can be accessed in any order.
The data stored in ResultSet is retrieved by using the various get methods, depending on the type of data being retrieved. The next() method is used to move to the next row.
ResultSet allows you to get and update columns by name, although using the column index results improves performance.
A cursor, which is an internal pointer, is used by a result set to point the row in the result set that is being accessed by the Java™ program.
The performance of the getRow() method has been improved. Before V5R2, using ResultSet.last(), ResultSet.afterLast(), and ResultSet.absolute() with a negative value made the current row number not available. The previous restrictions are lifted, which makes the getRow() method fully functional.
JDBC 2.0 and later JDBC specifications provide additional methods for accessing specific positions within a database:
Scrollable cursor positions | |
---|---|
absolute |
isFirst |
If a result set is created by executing a statement, you can move (scroll) backward (last-to-first) or forward (first-to-last) through the rows in a table.
A result set that supports this movement is called a scrollable result set. Scrollable result sets also support absolute positioning. Absolute positioning allows you to move directly to a row by specifying its position in the result set.
With JDBC 2.0 and later JDBC specifications, you have two additional scrolling capabilities available to use when working with the ResultSet class: scroll-insensitive and scroll-sensitive result sets.
A scroll-insensitive result set is not usually sensitive to changes that are made while it is open, while the scroll-sensitive result set is sensitive to changes.
In your application, you can use result sets that use either read-only concurrency (no updates can be made to the data) or updateable concurrency (allows updates to the data and uses database write locks to control access to the same data item by different transactions). In an updateable result set, rows can be updated, inserted, and deleted. Numerous update methods are available for you to use in your program, for example:
See Method Summary for a complete listing of the update methods available through the ResultSet interface.
The following example shows how to use a result set that allows updates to the data (update concurrency) and allows changes to be made to the result set while it is open (scroll sensitive).
// Connect to the server. Connection c = DriverManager.getConnection("jdbc:as400://mySystem"); // Create a Statement object. Set the result set // concurrency to updatable. Statement s = c.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); // Run a query. The result is placed // in a ResultSet object. ResultSet rs = s.executeQuery ("SELECT NAME,ID FROM MYLIBRARY.MYTABLE FOR UPDATE"); // Iterate through the rows of the ResultSet. // As we read the row, we will update it with // a new ID. int newId = 0; while (rs.next ()) { // Get the values from the ResultSet. // The first value is a string, and // the second value is an integer. String name = rs.getString("NAME"); int id = rs.getInt("ID"); System.out.println("Name = " + name); System.out.println("Old id = " + id); // Update the id with a new integer. rs.updateInt("ID", ++newId); // Send the updates to the server. rs.updateRow (); System.out.println("New id = " + newId); } // Close the Statement and the // Connection. s.close(); c.close();
The ResultSetMetaData interface determines the types and properties of the columns in a ResultSet.
When connecting to a server running i5/OS™ V5R2 or later, using the extended metadata property enables you to increase the accuracy of the following ResultSetMetaData methods:
Additionally, setting this property to true enables support for the ResultSetMetaData.getSchemaName(int) method. Be aware that using the extended metadata property may degrade performance because it requires retrieving more information from the server.