Friday 18 January 2013

ResultSet

Retrieve & Modify ResultSet


A ResultSet object is a table of data representing a database result set, which is usually generated by executing a statement that queries the database.


ResultSet object can be created through any object that implements the Statement interface, including PreparedStatement, CallableStatement and RowSet.

You access the data in a ResultSet object through a cursor.
Note that this cursor is not a database cursor. This cursor is a pointer that points to one row of data in the ResultSet.

ResultSet Types

  • TYPE_FORWARD_ONLY : The cursor moves forward only. Starts from before the first row to after the last row. It is default implementation.
  • TYPE_SCROLL_INSENSITIVE : Cursor can move both forward and backward and it can move to an absolute position. The result set is insensitive to changes made to data in DB. 
  • TYPE_SCROLL_SENSITIVE : It has same properties as that of TYPE_SCROLL_INSENSITIVE. The result set reflects changes made to the underlying data source while the result set remains open.

ResultSet Concurrency

The concurrency of a ResultSet object determines what level of update functionality is supported.
There are two concurrency levels:
  • CONCUR_READ_ONLY: The ResultSet object cannot be updated using the ResultSet interface.
  • CONCUR_UPDATABLE: The ResultSet object can be updated using the ResultSet interface.
The default ResultSet concurrency is CONCUR_READ_ONLY.

Cursor Objects

  • next: Moves the cursor forward one row. Returns true if the cursor is now positioned on a row and false if the cursor is positioned after the last row.
  • previous: Moves the cursor backward one row. Returns true if the cursor is now positioned on a row and false if the cursor is positioned before the first row.
  • first: Moves the cursor to the first row in the ResultSet object. Returns true if the cursor is now positioned on the first row and false if the ResultSet object does not contain any rows.
  • last:: Moves the cursor to the last row in the ResultSet object. Returns true if the cursor is now positioned on the last row and false if the ResultSet object does not contain any rows.
  • beforeFirst: Positions the cursor at the start of the ResultSet object, before the first row. If the ResultSet object does not contain any rows, this method has no effect.
  • afterLast: Positions the cursor at the end of the ResultSet object, after the last row. If the ResultSet object does not contain any rows, this method has no effect.
  • relative(int rows): Moves the cursor relative to its current position.
  • absolute(int row): Positions the cursor on the row specified by the parameter row.

------------------------------------------------------------------------------------------------------------

public void modifyName(String oldName, String newName) throws SQLException {

    Statement statement = null;
    try {
        statement = con.createStatement();
        statement = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,                   ResultSet.CONCUR_UPDATABLE);
        ResultSet resultSet= stmt.executeQuery("SELECT * FROM STUDENT");

        while (resultSet.next()) {
            String name = resultSet.getString("name");
            if(name.equals(oldName)){
                   resultSet.updateString( "name", newName);
                   resultSet.updateRow();
            }
        }

    } catch (SQLException e ) {
        System.out.println(e.getMessage());
    } finally {
        if (stmt != null) { stmt.close(); }
    }
}

No comments:

Post a Comment