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.
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.
Cursor Objects
next
: Moves the cursor forward one row. Returnstrue
if the cursor is now positioned on a row andfalse
if the cursor is positioned after the last row.previous
: Moves the cursor backward one row. Returnstrue
if the cursor is now positioned on a row andfalse
if the cursor is positioned before the first row.first
: Moves the cursor to the first row in theResultSet
object. Returnstrue
if the cursor is now positioned on the first row andfalse
if theResultSet
object does not contain any rows.last:
: Moves the cursor to the last row in theResultSet
object. Returnstrue
if the cursor is now positioned on the last row andfalse
if theResultSet
object does not contain any rows.beforeFirst
: Positions the cursor at the start of theResultSet
object, before the first row. If theResultSet
object does not contain any rows, this method has no effect.afterLast
: Positions the cursor at the end of theResultSet
object, after the last row. If theResultSet
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 parameterrow
.
------------------------------------------------------------------------------------------------------------
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