ResultSet Methods
These methods require a valid ResultSet object, returned from the getResultSet() method.
absolute
resultset.absolute(int row)
Moves the cursor to the specified row from the result set (rows start at 1). You can use a negative number to move to a row starting from the end of the result set. Returns true if the cursor is on the row, false if not.
afterLast
resultset.afterlast()
Moves the cursor to the end of the result set.
beforeFirst
resultset.beforeFirst()
Moves the cursor to the start of the result set.
cancelRowUpdates
resultset.cancelRowUpdates()
Cancels updates made to the current row in the result set.
close
resultset.close()
Closes the result set and frees all associated resources.
deleteRow
resultset.deleteRow()
Deletes the current ResultSet row from the database (and the result set).
findColumn
resultset.findColumn(String field_name)
Maps the fieldname to the column in the result set and returns the column index as an integer.
first
resultset.first()
Moves the cursor to the first row in the result set. Returns true if there is a valid first row, false if not.
getXXX
resultset.getXXX(String fieldname | int fieldindex)
Returns the contents of a field of the specified type. You can identify the field by its name or its position.
Table F.3 shows the SQL types and their equivalent Java methods.
SQL Type | Java Method |
---|---|
BIGINT | getLong() |
BINARY | getBytes() |
BIT | getBoolean() |
BLOB | getBlob() |
CHAR | getString() |
DATE | getDate() |
DECIMAL | getBigDecimal() |
DOUBLE | getDouble() |
FLOAT | getDouble() |
INTEGER | getInt() |
LONGVARBINARY | getBytes() |
LONGVARCHAR | getString() |
NUMERIC | getBigDecimal() |
OTHER | getObject() |
REAL | getFloat() |
SMALLINT | getShort() |
TIME | getTime() |
TIMESTAMP | getTimestamp() |
TINYINT | getByte() |
VARBINARY | getBytes() |
VARCHAR | getString() |
getCursorName
resultset.getCursorName()
Returns the name of the cursor used by the result set.
getFetchSize
resultset.getFetchSize()
Returns the fetch size for the result set object.
getMetaData
resultset.getMetaData()
Returns a ResultSetMetaData object with the number, type, and properties of the result set's columns.
getRow
resultset.getRow()
Returns an integer containing the current row number.
getStatement
resultset.getStatement()
Returns the statement object that created the result set.
getType
resultset.getType()
Returns the type of the result set object.
getWarnings
resultset.getWarnings()
Returns the first Warning triggered by a call from a ResultSet method on this result set.
insertRow
resultset.insertRow()
Inserts the contents of the insert row into the database (and the result set).
isAfterLast
resultet.isAfterLast()
Returns true if the cursor is after the last row in the result set, false if not.
isBeforeFirst
resultset.isBeforeFirst()
Returns true if the cursor is before the first row in the result set, false if not.
isFirst
resultset.isFirst()
Returns true if the cursor is at the first row in the result set, false if not.
isLast
resultset.isLast()
Returns true if the cursor is at the last row in the result set, false if not.
last
resultset.last()
Moves the cursor to the last row in the result set. Returns true if there is a valid last row, false if not.
moveToCurrentRow
resultset.moveToCurrentRow()
Moves the cursor to the remembered cursor position, which is usually the current row. This has no effect if the cursor is not on the insert row. See the moveToInsertRow() method.
moveToInsertRow
resultset.moveToInsertRow()
Moves the cursor to the insert row (a buffer where a new row can be placed with an update method). It remembers the current cursor position, which can be returned to with the moveToCurrentRow() method.
next
resultset.next()
Moves the cursor to the next row in the result set and returns true if there is a next row, false if there is not (the end has been reached).For example:
connection = DriverManager.getConnection(url, "guru2b", "g00r002b");
statement = connection.createStatement();
resultset = statement.executeQuery("SELECT first_
name,surname FROM customer");
while(resultset.next()) {
String first_name = resultset.getString("first_name");
String surname = resultset.getString("surname");
System.out.print("Name: " + first_name + " " + surname);
}
previous
resultset.previous()
Moves the cursor to the previous row in the result set and returns true if there is a previous row, false if there is not (the start has been reached).For example:
while(resultset.previous()) {
...
}
refreshRow
resultset.refreshRow()
Refreshes the current result set row with the most recent value in the database.
relative
resultset.relative(int rows)
Moves the cursor forward (if rows is positive) or backward (if rows is negative) by rows number of positions.
rowDeleted
resultset.rowDeleted()
Returns true if a row has been detected as deleted in the result set, false if not.
rowInserted
resultset.rowInserted()
Returns true if a row has been detected as inserted in the result set, false if not.
rowUpdated
resultset.rowUpdated()
Returns true if a row has been detected as updated in the result set, false if not.
setFetchSize
resultset.setFetchSize(int rows)
Gives the driver an idea of how many rows should be returned from the database when more rows are needed for the result set.
updateXXX
Updates the field with a value of the specified type. Table F.4 shows the SQL types and their equivalent Java methods.
SQL Type | Java Method |
---|---|
BIGINT | updateLong() |
BINARY | updateBytes() |
BIT | updateBoolean() |
BLOB | updateBlob() |
CHAR | updateString() |
DATE | updateDate() |
DECIMAL | updateBigDecimal() |
DOUBLE | updateDouble() |
FLOAT | updateDouble() |
INTEGER | updateInt() |
LONGVARBINARY | updateBytes() |
LONGVARCHAR | updateString() |
NUMERIC | updateBigDecimal() |
NULL | updateNull() |
OTHER | updateObject() |
REAL | updateFloat() |
SMALLINT | updateShort() |
TIME | updateTime() |
TIMESTAMP | updateTimestamp() |
TINYINT | updateByte() |
VARBINARY | updateBytes() |
VARCHAR | updateString() |
updateRow
resultset.updateRow()
Updates the database with the contents of the current row of the result set.
wasNull
resultSet.wasNull()
Returns true if the previous field read was a SQL NULL, false if not.