ResultSet Interface

A table of data representing a database result set, which is usually generated by executing a statement that queries the database.A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set.
When you create a ResultSet there are three attributes you can set. These are: 
  • Type
  • Concurrency
  • Holdability
SNIPPET:
 Statement stmt = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE,
                                      ResultSet.CONCUR_UPDATABLE
                                      ResultSet.HOLD_CURSORS_OVER_COMMIT);
 ResultSet rs = stmt.executeQuery("SELECT a, b FROM TABLE2");
       // rs will be scrollable, will not show changes made by others,
       // and will be updatable and ResultSet is not closed when the commit()
                             method of the underlying connection is called

ResultSet Types
A ResultSet can be of a certain type. The type of a ResultSet object determines the level of its functionality in two areas: the ways in which the cursor can be manipulated, and how concurrent changes made to the underlying data source are reflected by the ResultSet object.
There are three ResultSet types:
  • ResultSet.TYPE_FORWARD_ONLY: The ResultSet can only be navigated forward. That is, you can only move from row 1, to row 2, to row 3 etc. You cannot move backwards in the ResultSet..
  • ResultSet.TYPE_SCROLL_INSENSITIVE :The ResultSet can be navigated (scrolled) both forward and backwards. You can also jump to a position relative to the current position, or jump to an absolute position. The ResultSet is sensitive to changes in the underlying data source while theResultSet is open. That is, if a record in the ResultSet is changed in the database by another thread or process, it will be reflected in already opened ResulsSet's of this type.
  • ResultSet.TYPE_SCROLL_SENSITIVE:The ResultSet can be navigated (scrolled) both forward and backwards. You can also jump to a position relative to the current position, or jump to an absolute position. The ResultSet is sensitive to changes in the underlying data source while theResultSet is open. That is, if a record in the ResultSet is changed in the database by another thread or process, it will be reflected in already opened ResulsSet's of this type.

The default type is TYPE_FORWARD_ONLY
Note: Not all databases and JDBC drivers support all ResultSet types. The method DatabaseMetaData.supportsResultSetType returns true if the specified ResultSet type is supported andfalse otherwise


ResultSet Concurrency
The ResultSet concurrency determines whether the ResultSet can be updated, or only read.
A ResultSet can have one of two concurrency levels:
  • ResultSet.CONCUR_READ_ONLY:Creates a read-only result set. This is the default
  • ResultSet.CONCUR_UPDATABLE:Creates an updateable result set.
Note: Not all JDBC drivers and databases support concurrency. The method DatabaseMetaData.supportsResultSetConcurrency returns true if the specified concurrency level is supported by the driver and false otherwise.


ResultSet Holdability
The ResultSet holdability determines if a ResultSet is closed when the commit() method of the underlying connection is called.
There are two types of holdability:
  • HOLD_CURSORS_OVER_COMMIT: It means that all ResultSet instances are closed when connection.commit() method is called on the connection that created the ResultSet.
  • CLOSE_CURSORS_AT_COMMIT: It means that the ResultSet is kept open when theconnection.commit() method is called on the connection that created the ResultSet. 
The HOLD_CURSORS_OVER_COMMIT holdability might be useful if you use the ResultSet to update values in the database. Thus, you can open a ResultSet, update rows in it, callconnection.commit() and still keep the same ResultSet open for future transactions on the same rows.

Note:Not all holdability modes are supported by all databases and JDBC drivers. TheDatabaseMetaData.supportsResultSetHoldability(int holdability) returns true or false depending on whether the given holdability mode is supported or not


Navigating a Result Set:

There are several methods in the ResultSet interface that involve moving the cursor, including:
  • public void beforeFirst() throws SQLException:Moves the cursor to just before the first row
  • public void afterLast() throws SQLException:Moves the cursor to just after the last row.
  • public boolean first() throws SQLException:Moves the cursor to the first row
  • public void last() throws SQLException:Moves the cursor to the last row.
  • public boolean absolute(int row) throws SQLException:Moves the cursor to the specified row
  • public boolean relative(int row) throws SQLException:Moves the cursor the given number of rows forward or backwards from where it currently is pointing.
  • public boolean previous() throws SQLException:Moves the cursor to the previous row. This method returns false if the previous row is off the result set
  • public boolean next() throws SQLException:Moves the cursor to the next row. This method returns false if there are no more rows in the result set
  • public int getRow() throws SQLException:Returns the row number that the cursor is pointing to.
  • public void moveToInsertRow() throws SQLException:Moves the cursor to a special row in the result set that can be used to insert a new row into the database. The current cursor location is remembered.
  • public void moveToCurrentRow() throws SQLException:Moves the cursor back to the current row if the cursor is currently at the insert row; otherwise, this method does nothing

Viewing a Result Set:

The ResultSet interface contains dozens of methods for getting the data of the current row.
There is a get method for each of the possible data types, and each get method has two versions:
1.     One that takes in a column name.
2.     One that takes in a column index.
For example, if the column you are interested in viewing contains an int, you need to use one of the getInt() methods of ResultSet:
  • public int getInt(String columnName) throws SQLException:Returns the int in the current row in the column named columnName
  • public int getInt(int columnIndex) throws SQLException:Returns the int in the current row in the specified column index. The column index starts at 1, meaning the first column of a row is 1, the second column of a row is 2, and so on.

Similarly there are get methods in the ResultSet interface for each of the eight Java primitive types, as well as common types such as java.lang.String, java.lang.Object, and java.net.URL
There are also methods for getting SQL data types java.sql.Date, java.sql.Time, java.sql.TimeStamp, java.sql.Clob, and java.sql.Blob. Check the documentation for more information about using these SQL data types.


Updating a Result Set:

The ResultSet interface contains a collection of update methods for updating the data of a result set.
As with the get methods, there are two update methods for each data type:
1.     One that takes in a column name.
2.     One that takes in a column index.
For example, to update a String column of the current row of a result set, you would use one of the following updateString() methods:
  • public void updateString(int columnIndex, String s) throws SQLException:Changes the String in the specified column to the value of s.
  •  public void updateString(String columnName, String s) throws SQLException:Similar to the previous method, except that the column is specified by its name instead of its index.

There are update methods for the eight primitive data types, as well as String, Object, URL, and the SQL data types in the java.sql package.
Updating a row in the result set changes the columns of the current row in the ResultSet object, but not in the underlying database. To update your changes to the row in the database, you need to invoke one of the following methods.
  •  public void updateRow():Updates the current row by updating the corresponding row in the database.
  • public void deleteRow():Deletes the current row from the database
  • public void refreshRow():Refreshes the data in the result set to reflect any recent changes in the database.
  •  public void cancelRowUpdates():Cancels any updates made on the current row.
  • public void insertRow():Inserts a row into the database. This method can only be invoked when the cursor is pointing to the insert row.


Comments

Popular posts from this blog

Servlet Advantages and Disadvantages

The Deployment Descriptor: web.xml

Session Management