Podcast Beta
Questions and Answers
What is the initial position of the cursor in a ResultSet object?
Which method is used to move the cursor to the next row in a ResultSet?
What type of ResultSet allows for scrolling backward and forward?
How does a JDBC driver handle data retrieval using the getter methods?
Signup and view all the answers
What is the recommended order for reading columns within each row of a ResultSet?
Signup and view all the answers
What does the getClob() method retrieve from the ResultSet object?
Signup and view all the answers
Which method is used to obtain a stream of uninterpreted bytes from the ResultSet?
Signup and view all the answers
What is the primary purpose of the getBigDecimal() method with full precision?
Signup and view all the answers
Which method would be deprecated for retrieving a BigDecimal with a specified scale?
Signup and view all the answers
What does the getDate() method return from the ResultSet object?
Signup and view all the answers
What happens when a getter method is invoked with a column name that is duplicated in a ResultSet?
Signup and view all the answers
Which method call would correctly update a column value in the fifth row of a ResultSet?
Signup and view all the answers
What is the purpose of the SQL AS clause when using column names in SQL queries?
Signup and view all the answers
What occurs to open ResultSet objects when the associated Statement object is closed?
Signup and view all the answers
Which ResultSet type allows for scrolling in both directions and is sensitive to changes in the underlying data?
Signup and view all the answers
What is the return value of the next method when the cursor is positioned after the last row in a ResultSet?
Signup and view all the answers
When a ResultSet is closed, which of the following remains accessible?
Signup and view all the answers
Which statement is true about the wasNull method?
Signup and view all the answers
What happens if you call the close method on a ResultSet that is already closed?
Signup and view all the answers
Which of the following describes the behavior of a ResultSet when the CLOSE_CURSORS_AT_COMMIT constant is used?
Signup and view all the answers
What will be returned when calling getInt() with a SQL NULL value in the designated column?
Signup and view all the answers
Which of the following statements about the getBytes() method is accurate?
Signup and view all the answers
What potential exception is thrown if an invalid column index is used with getLong()?
Signup and view all the answers
What is a major change regarding the getBigDecimal() method with a specific scale?
Signup and view all the answers
Which exception might occur if the method getDate() is called on a closed ResultSet?
Signup and view all the answers
Which statement is true about the getAsciiStream method in a ResultSet?
Signup and view all the answers
What is the behavior of the getBinaryStream method when the column value is SQL NULL?
Signup and view all the answers
Which of the following must be considered when calling the getString method?
Signup and view all the answers
What exception is thrown by the getUnicodeStream method under certain conditions?
Signup and view all the answers
When using getBoolean, how does it interpret a column value of '0'?
Signup and view all the answers
What does the constant FETCH_UNKNOWN indicate about the ResultSet's row processing order?
Signup and view all the answers
Which constant indicates a ResultSet that allows movement only forward without scrolling?
Signup and view all the answers
Which concurrency mode constant allows for updates on a ResultSet object?
Signup and view all the answers
What does the constant TYPE_SCROLL_INSENSITIVE imply about the ResultSet's sensitivity to data changes?
Signup and view all the answers
What is the primary characteristic of FETCH_REVERSE constant in relation to ResultSet processing?
Signup and view all the answers
What is the return value when the designated column in the ResultSet is SQL NULL when using the getFloat method?
Signup and view all the answers
Which of the following exceptions can be thrown by the getter methods of a ResultSet?
Signup and view all the answers
What happens if the getBigDecimal method is called with a deprecated scale parameter?
Signup and view all the answers
Which statement is true regarding the getBytes method in a ResultSet?
Signup and view all the answers
What happens if updateBytes is called with an invalid columnIndex?
Signup and view all the answers
Which of the following is true regarding the updater methods like updateDate?
Signup and view all the answers
Which exception might occur if updateTime is called on a closed ResultSet?
Signup and view all the answers
What parameter does the updateDate method require first when updating a column value?
Signup and view all the answers
If the JDBC driver does not support the updateBytes method, what exception will be thrown?
Signup and view all the answers
What must be done to actually update the database after using updater methods in a ResultSet?
Signup and view all the answers
Which exception is NOT thrown by the updater methods when an invalid column index is specified?
Signup and view all the answers
What parameter is used in the updateShort method to specify the new value for the designated column?
Signup and view all the answers
What occurs if the updateNull method is called on a columnLabel that is not valid?
Signup and view all the answers
In which case will the SQLFeatureNotSupportedException be thrown when using an updater method?
Signup and view all the answers
What is the purpose of calling the updateRow method after using an updater method like updateFloat?
Signup and view all the answers
If a columnLabel provided to an updater method is invalid, which exception is guaranteed to be thrown?
Signup and view all the answers
How should the columnLabel be related to the SQL AS clause when updating a column using methods like updateDouble?
Signup and view all the answers
Which JDBC driver limitation would result in throwing a SQLFeatureNotSupportedException when using an updater method?
Signup and view all the answers
What must be done before an updater method like updateBytes is called to ensure that the changes are accurately applied?
Signup and view all the answers
What is the function of the updateObject
method when an instance of InputStream
is provided as the second argument?
Signup and view all the answers
Which exception is thrown when attempting to update a column in a closed result set?
Signup and view all the answers
When using the updateTimestamp
method, what does it indicate if an SQLFeatureNotSupportedException is thrown?
Signup and view all the answers
What is a consequence of calling the updateRow
method after using any of the updater methods?
Signup and view all the answers
In which case would the updateAsciiStream
method fail with a SQLException?
Signup and view all the answers
What must be executed after using an updater method to update column values in a ResultSet?
Signup and view all the answers
What type of exception is thrown if an invalid columnLabel is provided while using the updater methods?
Signup and view all the answers
In which scenario would a SQLFeatureNotSupportedException be raised when using an updater method?
Signup and view all the answers
What will happen if an updater method is called on a ResultSet with concurrency set to CONCUR_READ_ONLY?
Signup and view all the answers
What does the length parameter specify in the updateAsciiStream and updateBinaryStream methods?
Signup and view all the answers
Match the following methods with their corresponding parameter types:
Signup and view all the answers
Match the following exceptions with their conditions:
Signup and view all the answers
Match the following updater methods to their respective update behavior:
Signup and view all the answers
Match the following method calls with the exception they throw:
Signup and view all the answers
Match the following JDBC version with their introduction:
Signup and view all the answers
Match the following updater methods with their respective parameter types:
Signup and view all the answers
Match the following exceptions with the scenarios they relate to:
Signup and view all the answers
Match the following updater methods with their intended use:
Signup and view all the answers
Match the following updater methods with their respective parameter types:
Signup and view all the answers
Match the following method signatures with their descriptions:
Signup and view all the answers
Match the following updater methods with their description:
Signup and view all the answers
Match the following exceptions with their potential triggers in the updater methods:
Signup and view all the answers
Match the following parameters with their roles in updater methods:
Signup and view all the answers
Match the following updater methods with their specified length parameters:
Signup and view all the answers
Match the updater methods with the type of database update they do:
Signup and view all the answers
Match the following updater methods with their respective value types:
Signup and view all the answers
Match the updater methods with the exceptions they may throw:
Signup and view all the answers
Match the updater methods with their respective parameter descriptions:
Signup and view all the answers
Match the updater methods with their intended uses:
Signup and view all the answers
Match the following parameters of updater methods with their descriptions:
Signup and view all the answers
Match the JDBC updater methods with their corresponding descriptions:
Signup and view all the answers
Match the JDBC updater methods with their parameters:
Signup and view all the answers
Match the exceptions with their descriptions when calling updater methods:
Signup and view all the answers
Match the JDBC updater methods with their functionalities:
Signup and view all the answers
Match the parameters used in JDBC updater methods:
Signup and view all the answers
Study Notes
ResultSet Interface
- An interface representing a database result set, typically generated by executing a query.
- Maintains a cursor that points to the current row of data.
- Initially, the cursor is positioned before the first row.
- The
next()
method moves the cursor to the next row and returnsfalse
when no more rows exist. - A default ResultSet object is read-only and its cursor moves forward only.
- Supports iteration through the result set from the first row to the last row only once.
- Can create scrollable and/or updatable ResultSet objects.
- Provides getter methods to retrieve column values from the current row using either column index or name.
- Column numbers start from 1.
- For maximum portability, it's recommended to read columns from left to right and only once.
- JDBC drivers try to convert underlying data to the specified Java type.
- Column names are case-insensitive. If multiple columns have the same name, the value of the first matching column is returned.
- Offers updater methods to update column values in the current row and insert values into a staging row for insertion.
- The
insertRow()
method inserts the staging row into the ResultSet and the data source table. - Automatically closed when the Statement object that generated it is closed, re-executed, or used for the next result in a sequence of results.
-
ResultSetMetaData
object, retrieved viagetMetaData()
, provides information about the ResultSet object's columns.
ResultSet Interface Fields
-
CLOSE_CURSORS_AT_COMMIT
: Indicates that open ResultSets with this holdability will be closed when the current transaction commits. -
CONCUR_READ_ONLY
: Indicates that the ResultSet object is not updatable. -
CONCUR_UPDATABLE
: Indicates that the ResultSet object is updatable. -
FETCH_FORWARD
: Indicates that the rows in a result set will be processed in a forward-only direction (first-to-last). -
FETCH_REVERSE
: Indicates that the rows in a result set will be processed in reverse direction (last-to-first). -
FETCH_UNKNOWN
: Indicates that the order in which rows in a result set will be processed is unknown. -
HOLD_CURSORS_OVER_COMMIT
: Indicates that open ResultSets with this holdability will remain open when the current transaction is committed. -
TYPE_FORWARD_ONLY
: Indicates a ResultSet object whose cursor can only move forward. -
TYPE_SCROLL_INSENSITIVE
: Indicates a scrollable ResultSet object that is usually not sensitive to changes in the underlying data. -
TYPE_SCROLL_SENSITIVE
: Indicates a scrollable ResultSet object that is sensitive to changes in the underlying data.
ResultSet Interface Methods
-
absolute(int)
: Moves the cursor to the specified row number. -
afterLast()
: Moves the cursor to the end of the ResultSet object, just after the last row. -
beforeFirst()
: Moves the cursor to the front of the ResultSet object, just before the first row. -
cancelRowUpdates()
: Cancels updates made to the current row. -
clearWarnings()
: Clears all warnings reported on the ResultSet object. -
close()
: Releases the ResultSet object's resources immediately. -
deleteRow()
: Deletes the current row from the ResultSet object and the underlying database. -
findColumn(String)
: Maps a given column label to its index in the ResultSet object. -
first()
: Moves the cursor to the first row. -
getArray(int)
: Retrieves the value of the designated column as an Array object. -
getArray(String)
: Retrieves the value of the designated column as an Array object using the column label. -
getAsciiStream(int)
: Retrieves the column value as a stream of ASCII characters. -
getAsciiStream(String)
: Retrieves the column value as a stream of ASCII characters using the column label. -
getBigDecimal(int)
: Retrieves the column value as a BigDecimal with full precision. -
getBigDecimal(int, int)
: Retrieves the column value as a BigDecimal with specified scale (deprecated). -
getBigDecimal(String)
: Retrieves the column value as a BigDecimal with full precision using the column label. -
getBigDecimal(String, int)
: Retrieves the column value as a BigDecimal with specified scale (deprecated). -
getBinaryStream(int)
: Retrieves the column value as a stream of uninterpreted bytes. -
getBinaryStream(String)
: Retrieves the column value as a stream of uninterpreted bytes using the column label. -
getBlob(int)
: Retrieves the column value as a Blob object. -
getBlob(String)
: Retrieves the column value as a Blob object using the column label. -
getBoolean(int)
: Retrieves the column value as a boolean. -
getBoolean(String)
: Retrieves the column value as a boolean using the column label. -
getByte(int)
: Retrieves the column value as a byte. -
getByte(String)
: Retrieves the column value as a byte using the column label. -
getBytes(int)
: Retrieves the column value as a byte array. -
getBytes(String)
: Retrieves the column value as a byte array using the column label. -
getCharacterStream(int)
: Retrieves the column value as a Reader object. -
getCharacterStream(String)
: Retrieves the column value as a Reader object using the column label. -
getClob(int)
: Retrieves the column value as a Clob object. -
getClob(String)
: Retrieves the column value as a Clob object using the column label. -
getConcurrency()
: Retrieves the concurrency mode of the ResultSet object. -
getCursorName()
: Retrieves the name of the SQL cursor used by the ResultSet object. -
getDate(int)
: Retrieves the column value as a java.sql.Date object. -
getDate(int, Calendar)
: Retrieves the column value as a java.sql.Date object with a specified calendar. -
getDate(String)
: Retrieves the column value as a java.sql.Date object using the column label. -
getDate(String, Calendar)
: Retrieves the column value as a java.sql.Date object with a specified calendar using the column label. -
getDouble(int)
: Retrieves the column value as a double. -
getDouble(String)
: Retrieves the column value as a double using the column label. -
getFetchDirection()
: Retrieves the fetch direction of the ResultSet object. -
getFetchSize()
: Retrieves the fetch size of the ResultSet object. -
getFloat(int)
: Retrieves the column value as a float. -
getFloat(String)
: Retrieves the column value as a float using the column label. -
getHoldability()
: Retrieves the holdability of the ResultSet object. -
getInt(int)
: Retrieves the column value as an int. -
getInt(String)
: Retrieves the column value as an int using the column label. -
getLong(int)
: Retrieves the column value as a long. -
getLong(String)
: Retrieves the column value as a long using the column label. -
getMetaData()
: Retrieves the ResultSetMetaData object containing information about columns. -
getNCharacterStream(int)
: Retrieves the column value as a Reader object. -
getNCharacterStream(String)
: Retrieves the column value as a Reader object using the column label. -
getNClob(int)
: Retrieves the column value as an NClob object. -
getNClob(String)
: Retrieves the column value as an NClob object using the column label. -
getNString(int)
: Retrieves the column value as a String. -
getNString(String)
: Retrieves the column value as a String using the column label. -
getObject(int)
: Retrieves the column value as an Object. -
getObject(int, Class)
: Retrieves the column value as an Object with a specified type. -
getObject(int, Map)
: Retrieves the column value as an Object with a type map. -
getRef(int)
: Retrieves the column value as a Ref object. -
getRef(String)
: Retrieves the column value as a Ref object using the column label. -
getRow()
: Retrieves the current row number. -
getRowId(int)
: Retrieves the column value as a java.sql.RowId object. -
getRowId(String)
: Retrieves the column value as a java.sql.RowId object using the column label. -
getShort(int)
: Retrieves the column value as a short. -
getShort(String)
: Retrieves the column value as a short using the column label. -
getSQLXML(int)
: Retrieves the column value as a java.sql.SQLXML object. -
getSQLXML(String)
: Retrieves the column value as a java.sql.SQLXML object using the column label. -
getStatement()
: Retrieves the Statement object that generated this ResultSet object. -
getString(int)
: Retrieves the column value as a String. -
getString(String)
: Retrieves the column value as a String using the column label. -
getTime(int)
: Retrieves the column value as a java.sql.Time object. -
getTime(int, Calendar)
: Retrieves the column value as a java.sql.Time object with a specified calendar. -
getTime(String)
: Retrieves the column value as a java.sql.Time object using the column label. -
getTime(String, Calendar)
: Retrieves the column value as a java.sql.Time object with a specified calendar using the column label. -
getTimestamp(int)
: Retrieves the column value as a java.sql.Timestamp object. -
getTimestamp(int, Calendar)
: Retrieves the column value as a java.sql.Timestamp object with a specified calendar. -
getTimestamp(String)
: Retrieves the column value as a java.sql.Timestamp object using the column label. -
getTimestamp(String, Calendar)
: Retrieves the column value as a java.sql.Timestamp object with a specified calendar using the column label. -
getType()
: Retrieves the type of the ResultSet object. -
insertRow()
: Inserts the row being built in the staging area into ResultSet and data source table. -
last()
: Moves the cursor to the last row. -
moveToCurrentRow()
: Moves the cursor back to the current row. -
moveToInsertRow()
: Moves the cursor to the staging area row for building a new row to be inserted. -
next()
: Moves the cursor to the next row, returningfalse
when there are no more rows. -
previous()
: Moves the cursor to the previous row. -
relative(int)
: Moves the cursor to the position relative to the current row. -
rowDeleted()
: Indicates whether the current row has been deleted. -
rowInserted()
: Indicates whether the current row has been inserted. -
rowUpdated()
: Indicates whether the current row has been updated. -
updateArray(int, Array)
: Updates the value of the designated column with an Array object. -
updateArray(String, Array)
: Updates the value of the designated column with an Array object using the column label. -
updateAsciiStream(int, InputStream)
: Updates the designated column with a stream of ASCII characters. -
updateAsciiStream(int, InputStream, int)
: Updates the designated column with a stream of ASCII characters with a specified length. -
updateAsciiStream(String, InputStream)
: Updates the designated column with a stream of ASCII characters using the column label. -
updateAsciiStream(String, InputStream, int)
: Updates the designated column with a stream of ASCII characters with a specified length using the column label. -
updateBigDecimal(int, BigDecimal)
: Updates the designated column with a BigDecimal value. -
updateBigDecimal(String, BigDecimal)
: Updates the designated column with a BigDecimal value using the column label. -
updateBinaryStream(int, InputStream)
: Updates the designated column with a stream of uninterpreted bytes. -
updateBinaryStream(int, InputStream, int)
: Updates the designated column with a stream of uninterpreted bytes with a specified length. -
updateBinaryStream(String, InputStream)
: Updates the designated column with a stream of uninterpreted bytes using the column label. -
updateBinaryStream(String, InputStream, int)
: Updates the designated column with a stream of uninterpreted bytes with a specified length using the column label. -
updateBlob(int, Blob)
: Updates the designated column with a Blob object. -
updateBlob(int, InputStream)
: Updates the designated column with a stream of uninterpreted bytes from an InputStream. -
updateBlob(int, InputStream, long)
: Updates the designated column with a stream of uninterpreted bytes from an InputStream with a specified length. -
updateBlob(String, Blob)
: Updates the designated column with a Blob object using the column label. -
updateBlob(String, InputStream)
: Updates the designated column with a stream of uninterpreted bytes from an InputStream using the column label. -
updateBlob(String, InputStream, long)
: Updates the designated column with a stream of uninterpreted bytes from an InputStream with a specified length using the column label. -
updateBoolean(int, boolean)
: Updates the designated column with a boolean value. -
updateBoolean(String, boolean)
: Updates the designated column with a boolean value using the column label. -
updateByte(int, byte)
: Updates the designated column with a byte value. -
updateByte(String, byte)
: Updates the designated column with a byte value using the column label. -
updateBytes(int, byte[])
: Updates the designated column with a byte array. -
updateBytes(String, byte[])
: Updates the designated column with a byte array using the column label. -
updateCharacterStream(int, Reader)
: Updates the designated column with a stream of characters from a Reader object. -
updateCharacterStream(int, Reader, int)
: Updates the designated column with a stream of characters from a Reader object with a specified length. -
updateCharacterStream(String, Reader)
: Updates the designated column with a stream of characters from a Reader object using the column label. -
updateCharacterStream(String, Reader, int)
: Updates the designated column with a stream of characters from a Reader object with a specified length using the column label. -
updateClob(int, Clob)
: Updates the designated column with a Clob object. -
updateClob(int, Reader)
: Updates the designated column with a stream of characters from a Reader object. -
updateClob(int, Reader, long)
: Updates the designated column with a stream of characters from a Reader object with a specified length. -
updateClob(String, Clob)
: Updates the designated column with a Clob object using the column label. -
updateClob(String, Reader)
: Updates the designated column with a stream of characters from a Reader object using the column label. -
updateClob(String, Reader, long)
: Updates the designated column with a stream of characters from a Reader object with a specified length using the column label. -
updateDate(int, Date)
: Updates the designated column with a java.sql.Date object. -
updateDate(String, Date)
: Updates the designated column with a java.sql.Date object using the column label. -
updateDouble(int, double)
: Updates the designated column with a double value. -
updateDouble(String, double)
: Updates the designated column with a double value using the column label. -
updateFloat(int, float)
: Updates the designated column with a float value. -
updateFloat(String, float)
: Updates the designated column with a float value using the column label. -
updateInt(int, int)
: Updates the designated column with an int value. -
updateInt(String, int)
: Updates the designated column with an int value using the column label. -
updateLong(int, long)
: Updates the designated column with a long value. -
updateLong(String, long)
: Updates the designated column with a long value using the column label. -
updateNCharacterStream(int, Reader)
: Updates the designated column with a stream of characters from a Reader object. -
updateNCharacterStream(int, Reader, long)
: Updates the designated column with a stream of characters from a Reader object with a specified length. -
updateNCharacterStream(String, Reader)
: Updates the designated column with a stream of characters from a Reader object using the column label. -
updateNCharacterStream(String, Reader, long)
: Updates the designated column with a stream of characters from a Reader object with a specified length using the column label. -
updateNClob(int, NClob)
: Updates the designated column with an NClob object. -
updateNClob(int, Reader)
: Updates the designated column with a stream of characters from a Reader object. -
updateNClob(int, Reader, long)
: Updates the designated column with a stream of characters from a Reader object with a specified length. -
updateNClob(String, NClob)
: Updates the designated column with an NClob object using the column label. -
updateNClob(String, Reader)
: Updates the designated column with a stream of characters from a Reader object using the column label. -
updateNClob(String, Reader, long)
: Updates the designated column with a stream of characters from a Reader object with a specified length using the column label. -
updateNString(int, String)
: Updates the designated column with a String value. -
updateNString(String, String)
: Updates the designated column with a String value using the column label. -
updateObject(int, Object)
: Updates the designated column with an object value. -
updateObject(int, Object, int)
: Updates the designated column with a Object value with a specified SQL type. -
updateObject(int, Object, int, int)
: Updates the designated column with a Object value with a specified SQL type and scale. -
updateObject(String, Object)
: Updates the designated column with an object value using the column label. -
updateObject(String, Object, int)
: Updates the designated column with a Object value with a specified SQL type using the column label. -
updateObject(String, Object, int, int)
: Updates the designated column with a Object value with a specified SQL type and scale using the column label. -
updateRef(int, Ref)
: Updates the designated column with a Ref object. -
updateRef(String, Ref)
: Updates the designated column with a Ref object using the column label. -
updateRow()
: Updates the underlying data source table with the updates made to the current row. -
updateRowId(int, RowId)
: Updates the designated column with a java.sql.RowId object. -
updateRowId(String, RowId)
: Updates the designated column with a java.sql.RowId object using the column label. -
updateShort(int, short)
: Updates the designated column with a short value. -
updateShort(String, short)
: Updates the designated column with a short value using the column label. -
updateSQLXML(int, SQLXML)
: Updates the designated column with an SQLXML object. -
updateSQLXML(String, SQLXML)
: Updates the designated column with an SQLXML object using the column label. -
updateString(int, String)
: Updates the designated column with a String value. -
updateString(String, String)
: Updates the designated column with a String value using the column label. -
updateTime(int, Time)
: Updates the designated column with a java.sql.Time object. -
updateTime(String, Time)
: Updates the designated column with a java.sql.Time object using the column label. -
updateTimestamp(int, Timestamp)
: Updates the designated column with a java.sql.Timestamp object. -
updateTimestamp(String, Timestamp)
: Updates the designated column with a java.sql.Timestamp object using the column label. -
wasNull()
: Indicates whether the last column read had a value of SQL NULL.
ResultSet Methods
-
getUnicodeStream(int columnIndex)
andgetUnicodeStream(String columnLabel)
are deprecated, usegetCharacterStream
instead -
getURL(int columnIndex)
andgetURL(String columnLabel)
retrieve the value of the specified column in the current row as ajava.net.URL
object -
getWarnings()
retrieves the first warning reported by calls on theResultSet
object -
insertRow()
inserts the contents of the insert row into theResultSet
object and the database -
isAfterLast()
determines if the cursor is after the last row in theResultSet
object -
isBeforeFirst()
determines if the cursor is before the first row in theResultSet
object -
isClosed()
determines if theResultSet
object has been closed -
isFirst()
determines if the cursor is on the first row of theResultSet
object -
isLast()
determines if the cursor is on the last row of theResultSet
object -
last()
moves the cursor to the last row in theResultSet
object -
moveToCurrentRow()
moves the cursor to the remembered cursor position, usually the current row -
moveToInsertRow()
moves the cursor to the insert row -
next()
moves the cursor forward one row from its current position. It returnstrue
if a new current row is available, otherwisefalse
. -
previous()
moves the cursor to the previous row in theResultSet
object -
refreshRow()
refreshes the current row with its most recent value in the database -
relative(int rows)
moves the cursor a relative number of rows, either positive or negative. -
rowDeleted()
determines if a row has been deleted -
rowInserted()
determines if the current row has had an insertion -
rowUpdated()
determines if the current row has been updated -
setFetchDirection(int direction)
gives a hint to the driver about the direction in which rows in theResultSet
object will be processed. Possible values are:-
FETCH_FORWARD
-
FETCH_REVERSE
-
FETCH_UNKNOWN
-
-
setFetchSize(int rows)
gives a hint to the driver about the number of rows to fetch from the database when more rows are needed. -
updateArray(int columnIndex, Array x)
andupdateArray(String columnLabel, Array x)
update the designated column with ajava.sql.Array
value -
updateAsciiStream(int columnIndex, InputStream x)
and its variations update the designated column with anascii stream
value. -
updateBigDecimal(int columnIndex, BigDecimal x)
andupdateBigDecimal(String columnLabel, BigDecimal x)
update the designated column with ajava.math.BigDecimal
value -
updateBinaryStream(int columnIndex, InputStream x)
and its variations update the designated column with abinary stream
value -
updateBlob(int columnIndex, Blob x)
and its variations update the designated column with ajava.sql.Blob
value -
updateBoolean(int columnIndex, boolean x)
andupdateBoolean(String columnLabel, boolean x)
update the designated column with aboolean
value -
updateByte(int columnIndex, byte x)
andupdateByte(String columnLabel, byte x)
update the designated column with abyte
value -
updateBytes(int columnIndex, byte[] x)
andupdateBytes(String columnLabel, byte[] x)
update the designated column with abyte array
value -
updateCharacterStream(int columnIndex, Reader x)
and its variations update the designated column with acharacter stream
value -
updateClob(int columnIndex, Clob x)
and its variations update the designated column with ajava.sql.Clob
value -
updateDate(int columnIndex, Date x)
andupdateDate(String columnLabel, Date x)
update the designated column with ajava.sql.Date
value -
updateDouble(int columnIndex, double x)
andupdateDouble(String columnLabel, double x)
update the designated column with adouble
value -
updateFloat(int columnIndex, float x)
andupdateFloat(String columnLabel, float x)
update the designated column with afloat
value -
updateInt(int columnIndex, int x)
andupdateInt(String columnLabel, int x)
update the designated column with anint
value -
updateLong(int columnIndex, long x)
andupdateLong(String columnLabel, long x)
update the designated column with along
value -
updateNCharacterStream(int columnIndex, Reader x)
and its variations update the designated column with acharacter stream
value -
updateNClob(int columnIndex, NClob nClob)
and its variations update the designated column with ajava.sql.NClob
value -
updateNString(int columnIndex, String nString)
andupdateNString(String columnLabel, String nString)
update the designated column with aString
value -
updateNull(int columnIndex)
andupdateNull(String columnLabel)
update the designated column with anull
value -
updateObject(int columnIndex, Object x)
and its variations update the designated column with anObject
value -
updateRef(int columnIndex, Ref x)
andupdateRef(String columnLabel, Ref x)
update the designated column with ajava.sql.Ref
value -
updateRow()
updates the underlying database with the new contents of the current row of theResultSet
object -
updateRowId(int columnIndex, RowId x)
andupdateRowId(String columnLabel, RowId x)
update the designated column with aRowId
value -
updateShort(int columnIndex, short x)
andupdateShort(String columnLabel, short x)
update the designated column with ashort
value -
updateSQLXML(int columnIndex, SQLXML xmlObject)
andupdateSQLXML(String columnLabel, SQLXML xmlObject)
update the designated column with ajava.sql.SQLXML
value -
updateString(int columnIndex, String x)
andupdateString(String columnLabel, String x)
update the designated column with aString
value -
updateTime(int columnIndex, Time x)
andupdateTime(String columnLabel, Time x)
update the designated column with ajava.sql.Time
value -
updateTimestamp(int columnIndex, Timestamp x)
andupdateTimestamp(String columnLabel, Timestamp x)
update the designated column with ajava.sql.Timestamp
value -
wasNull()
reports whether the last column read had a value of SQL NULL.
ResultSet Fields
-
FETCH_FORWARD
,FETCH_REVERSE
,FETCH_UNKNOWN
: constants indicating the direction in which rows will be processed in the result set -
TYPE_FORWARD_ONLY
,TYPE_SCROLL_INSENSITIVE
,TYPE_SCROLL_SENSITIVE
: constants indicating the type ofResultSet
object based on its cursor movement abilities and sensitivity to underlying data changes -
CONCUR_READ_ONLY
,CONCUR_UPDATABLE
: constants indicating the concurrency mode of aResultSet
object (whether it is updatable or not) -
HOLD_CURSORS_OVER_COMMIT
,CLOSE_CURSORS_AT_COMMIT
: constants indicating howResultSet
objects with this holdability will be handled when the current transaction is committed (whether they remain open or are closed)
ResultSet Object
-
ResultSet
objects are automatically closed by theStatement
object that generated it when thatStatement
object is closed, re-executed, or is used to retrieve the next result from a sequence of multiple results. - Calling
close()
on aResultSet
object that is already closed is a no-op, which means it will have no effect. - Blob, Clob, and NClob objects created by the
ResultSet
remain valid for at least the duration of the transaction in which they are created, unless theirfree()
method is invoked. -
ResultSetMetaData
instances created by calling thegetMetaData()
method remain accessible after theResultSet
is closed.
ResultSet Getter Methods
-
wasNull()
returnstrue
if the last column value read wasSQL NULL
, otherwisefalse
. -
wasNull()
throws anSQLException
if a database access error occurs or the method is called on a closed result set. - The
getString(int columnIndex)
method retrieves the value of the designated column in the current row of theResultSet
object as aString
. -
getString()
accepts an integer representing the column index (1 for the first column, 2 for the second, etc.) -
getString()
returnsnull
if the value isSQL NULL
. -
getString()
throws anSQLException
if an invalidcolumnIndex
is provided, if a database access error occurs or this method is called on a closed result set. -
getBoolean(int columnIndex)
retrieves the value of the designated column as aboolean
in the Java programming language. -
getBoolean()
returnsfalse
if the column has a datatype ofCHAR
orVARCHAR
and contains "0", or has a datatype ofBIT
,TINYINT
,SMALLINT
,INTEGER
orBIGINT
and contains 0. -
getBoolean()
returnstrue
if the column has a datatype ofCHAR
orVARCHAR
and contains "1" or has a datatype ofBIT
,TINYINT
,SMALLINT
,INTEGER
orBIGINT
and contains 1. -
getBoolean()
accepts an integer representing the column index. -
getBoolean()
returnsfalse
if the value isSQL NULL
. -
getBoolean()
throws anSQLException
if an invalidcolumnIndex
is provided, if a database access error occurs or this method is called on a closed result set. -
getByte(int columnIndex)
retrieves the value of the designated column as abyte
in Java. -
getByte()
accepts an integer representing the column index. -
getByte()
returns 0 if the value isSQL NULL
. -
getByte()
throws anSQLException
if an invalidcolumnIndex
is provided, if a database access error occurs or this method is called on a closed result set. -
getShort(int columnIndex)
retrieves the value of the designated column as ashort
in Java. -
getShort()
accepts an integer representing the column index. -
getShort()
returns 0 if the value isSQL NULL
. -
getShort()
throws anSQLException
if an invalidcolumnIndex
is provided, if a database access error occurs or this method is called on a closed result set. -
getInt(int columnIndex)
retrieves the value of the designated column as anint
in Java. -
getInt()
accepts an integer representing the column index. -
getInt()
returns 0 if the value isSQL NULL
. -
getInt()
throws anSQLException
if an invalidcolumnIndex
is provided, if a database access error occurs or this method is called on a closed result set. -
getLong(int columnIndex)
retrieves the value of the designated column as along
in Java. -
getLong()
accepts an integer representing the column index. -
getLong()
returns 0 if the value isSQL NULL
. -
getLong()
throws anSQLException
if an invalidcolumnIndex
is provided, if a database access error occurs or this method is called on a closed result set. -
getFloat(int columnIndex)
retrieves the value of the designated column as afloat
in Java. -
getFloat()
accepts an integer representing the column index. -
getFloat()
returns 0 if the value isSQL NULL
. -
getFloat()
throws anSQLException
if an invalidcolumnIndex
is provided, if a database access error occurs or this method is called on a closed result set. -
getDouble(int columnIndex)
retrieves the value of the designated column as adouble
in Java. -
getDouble()
accepts an integer representing the column index. -
getDouble()
returns 0 if the value isSQL NULL
. -
getDouble()
throws anSQLException
if an invalidcolumnIndex
is provided, if a database access error occurs or this method is called on a closed result set. -
getBigDecimal(int columnIndex, int scale)
retrieves the value of the designated column as ajava.sql.BigDecimal
in Java. - The
getBigDecimal(int columnIndex, int scale)
method is deprecated. UsegetBigDecimal(int columnIndex)
orgetBigDecimal(String columnLabel)
instead. -
getBigDecimal()
accepts an integer representing the column index and ascale
value. -
getBigDecimal()
returnsnull
if the value isSQL NULL
. -
getBigDecimal()
throws anSQLException
if an invalidcolumnIndex
is provided, if a database access error occurs or this method is called on a closed result set. -
getBigDecimal()
throws anSQLFeatureNotSupportedException
if the JDBC driver does not support this method. -
getBytes(int columnIndex)
retrieves the value of the designated column as abyte[]
array. -
getBytes()
accepts an integer representing the column index. -
getBytes()
returnsnull
if the value isSQL NULL
. -
getBytes()
throws anSQLException
if an invalidcolumnIndex
is provided, if a database access error occurs or this method is called on a closed result set. -
getDate(int columnIndex)
retrieves the value of the designated column as ajava.sql.Date
object. -
getDate()
accepts an integer representing the column index. -
getDate()
returnsnull
if the value isSQL NULL
. -
getDate()
throws anSQLException
if an invalidcolumnIndex
is provided, if a database access error occurs or this method is called on a closed result set. -
getTime(int columnIndex)
retrieves the value of the designated column as ajava.sql.Time
object. -
getTime()
accepts an integer representing the column index. -
getTime()
returnsnull
if the value isSQL NULL
. -
getTime()
throws anSQLException
if an invalidcolumnIndex
is provided, if a database access error occurs or this method is called on a closed result set. -
getTimestamp(int columnIndex)
retrieves the value of the designated column as ajava.sql.Timestamp
object. -
getTimestamp()
accepts an integer representing the column index. -
getTimestamp()
returnsnull
if the value isSQL NULL
. -
getTimestamp()
throws anSQLException
if an invalidcolumnIndex
is provided, if a database access error occurs or this method is called on a closed result set. -
getAsciiStream(int columnIndex)
retrieves the value of the designated column as a stream of ASCII characters. -
getAsciiStream()
accepts an integer representing the column index. -
getAsciiStream()
returnsnull
if the value isSQL NULL
. -
getAsciiStream()
throws anSQLException
if an invalidcolumnIndex
is provided, if a database access error occurs or this method is called on a closed result set. -
getUnicodeStream(int columnIndex)
retrieves the value of the designated column as a stream of two-byte Unicode characters. This method is deprecated. UsegetCharacterStream
instead. -
getUnicodeStream()
accepts an integer representing the column index. -
getUnicodeStream()
returnsnull
if the value isSQL NULL
. -
getUnicodeStream()
throws anSQLException
if an invalidcolumnIndex
is provided, if a database access error occurs or this method is called on a closed result set. -
getUnicodeStream()
throws anSQLFeatureNotSupportedException
if the JDBC driver does not support this method. -
getBinaryStream(int columnIndex)
retrieves the value of the designated column as a stream of uninterpreted bytes. -
getBinaryStream()
accepts an integer representing the column index. -
getBinaryStream()
returnsnull
if the value isSQL NULL
. -
getBinaryStream()
throws anSQLException
if an invalidcolumnIndex
is provided, if a database access error occurs or this method is called on a closed result set.
ResultSet Getter Methods with Column Labels
- The
getString(String columnLabel)
method retrieves the value of the designated column in the current row of theResultSet
object as aString
. -
getString()
accepts aString
representing the column label specified with the SQLAS
clause. If theAS
clause was not specified, then the label is the name of the column. -
getString()
returnsnull
if the value isSQL NULL
. -
getString()
throws anSQLException
if an invalidcolumnLabel
is provided, if a database access error occurs or this method is called on a closed result set. -
getBoolean(String columnLabel)
retrieves the value of the designated column as aboolean
in the Java programming language. -
getBoolean()
returnsfalse
if the column has a datatype ofCHAR
orVARCHAR
and contains "0", or has a datatype ofBIT
,TINYINT
,SMALLINT
,INTEGER
orBIGINT
and contains 0. -
getBoolean()
returnstrue
if the column has a datatype ofCHAR
orVARCHAR
and contains "1" or has a datatype ofBIT
,TINYINT
,SMALLINT
,INTEGER
orBIGINT
and contains 1. -
getBoolean()
accepts aString
representing the column label. -
getBoolean()
returnsfalse
if the value isSQL NULL
. -
getBoolean()
throws anSQLException
if an invalidcolumnLabel
is provided, if a database access error occurs or this method is called on a closed result set. -
getByte(String columnLabel)
retrieves the value of the designated column as abyte
in Java. -
getByte()
accepts aString
representing the column label. -
getByte()
returns 0 if the value isSQL NULL
. -
getByte()
throws anSQLException
if an invalidcolumnLabel
is provided, if a database access error occurs or this method is called on a closed result set. -
getShort(String columnLabel)
retrieves the value of the designated column as ashort
in Java. -
getShort()
accepts aString
representing the column label. -
getShort()
returns 0 if the value isSQL NULL
. -
getShort()
throws anSQLException
if an invalidcolumnLabel
is provided, if a database access error occurs or this method is called on a closed result set. -
getInt(String columnLabel)
retrieves the value of the designated column as anint
in Java. -
getInt()
accepts aString
representing the column label. -
getInt()
returns 0 if the value isSQL NULL
. -
getInt()
throws anSQLException
if an invalidcolumnLabel
is provided, if a database access error occurs or this method is called on a closed result set. -
getLong(String columnLabel)
retrieves the value of the designated column as along
in Java. -
getLong()
accepts aString
representing the column label. -
getLong()
returns 0 if the value isSQL NULL
. -
getLong()
throws anSQLException
if an invalidcolumnLabel
is provided, if a database access error occurs or this method is called on a closed result set. -
getFloat(String columnLabel)
retrieves the value of the designated column as afloat
in Java. -
getFloat()
accepts aString
representing the column label. -
getFloat()
returns 0 if the value isSQL NULL
. -
getFloat()
throws anSQLException
if an invalidcolumnLabel
is provided, if a database access error occurs or this method is called on a closed result set. -
getDouble(String columnLabel)
retrieves the value of the designated column as adouble
in Java. -
getDouble()
accepts aString
representing the column label. -
getDouble()
returns 0 if the value isSQL NULL
. -
getDouble()
throws anSQLException
if an invalidcolumnLabel
is provided, if a database access error occurs or this method is called on a closed result set. -
getBigDecimal(String columnLabel, int scale)
retrieves the value of the designated column as ajava.math.BigDecimal
in Java. - The
getBigDecimal(String columnLabel, int scale)
method is deprecated. UsegetBigDecimal(int columnIndex)
orgetBigDecimal(String columnLabel)
instead. -
getBigDecimal()
accepts aString
representing the column label and ascale
value. -
getBigDecimal()
returnsnull
if the value isSQL NULL
. -
getBigDecimal()
throws anSQLException
if an invalidcolumnLabel
is provided, if a database access error occurs or this method is called on a closed result set. -
getBigDecimal()
throws anSQLFeatureNotSupportedException
if the JDBC driver does not support this method.
ResultSet Methods
-
getAsciiStream
retrieves the value of the designated column as a stream of ASCII characters. -
getBinaryStream
retrieves the value of the designated column as a stream of uninterpreted bytes. -
getBigDecimal
retrieves the value of the designated column as a java.math.BigDecimal with full precision. -
getCharacterStream
retrieves the value of the designated column as a java.io.Reader object. -
getBytes
retrieves the value of the designated column as a byte array in the Java programming language. -
getDate
retrieves the value of the designated column as a java.sql.Date object. -
getMetaData
retrieves the number, types, and properties of the ResultSet object's columns. -
getObject
retrieves the value of the designated column as an Object in the Java programming language. -
getTime
retrieves the value of the designated column as a java.sql.Time object. -
getTimestamp
retrieves the value of the designated column as a java.sql.Timestamp object. -
getUnicodeStream
is deprecated; use getCharacterStream instead. -
getWarnings
retrieves the first warning reported by calls on the ResultSet object. -
clearWarnings
clears all warnings reported on the ResultSet object. -
findColumn
maps the given ResultSet column label to its ResultSet column index. -
getCursorName
retrieves the name of the SQL cursor used by the ResultSet object. -
isAfterLast
retrieves whether the cursor is after the last row in the ResultSet object. -
isBeforeFirst
retrieves whether the cursor is before the first row in the ResultSet object. -
isFirst
retrieves whether the cursor is on the first row of the ResultSet object. -
isLast
retrieves whether the cursor is on the last row of the ResultSet object. -
getString
retrieves the value of the designated column as a String in the Java programming language.
Methods Parameters
-
columnLabel
is the label for the column specified with the SQL AS clause. - If the SQL AS clause was not specified, then the label is the name of the column.
-
columnIndex
is the column index, starting from 1.
Method Errors
-
SQLException
is thrown if the columnLabel or columnIndex is invalid, a database access error occurs, or the method is called on a closed result set. -
SQLFeatureNotSupportedException
is thrown if the JDBC driver does not support the method.
ResultSet Methods
-
beforeFirst()
: Moves the cursor to the first position in theResultSet
. No effect if theResultSet
is empty. -
afterLast()
: Moves the cursor to the last position in theResultSet
. No effect if theResultSet
is empty. -
first()
: Moves the cursor to the first row of theResultSet
. Returnstrue
if the cursor is on a valid row,false
if theResultSet
is empty. -
last()
: Moves the cursor to the last row of theResultSet
. Returnstrue
if the cursor is on a valid row,false
if theResultSet
is empty. -
getRow()
: Retrieves the current row number. The first row is number 1. -
absolute(int row)
: Moves the cursor to a specified row number. Positiverow
values count from the beginning, negative values count from the end, and0
positions the cursor before the first row. -
relative(int rows)
: Moves the cursor a relative number of rows, either positive or negative. -
previous()
: Moves the cursor to the previous row in theResultSet
. Returnstrue
if the cursor is now on a valid row,false
if the cursor is positioned before the first row. -
setFetchDirection(int direction)
: Provides a hint to the JDBC driver about how rows should be processed. The fetch direction may be changed at any time. -
getFetchDirection()
: Retrieves the current fetch direction of theResultSet
. -
setFetchSize(int rows)
: Hints to the JDBC driver how many rows should be fetched when more rows are needed. -
getFetchSize()
: Retrieves the current fetch size of theResultSet
. -
getType()
: Retrieves the type of theResultSet
. This is determined by theStatement
object that creates theResultSet
. -
getConcurrency()
: Retrieves the concurrency mode of theResultSet
. This is determined by theStatement
object that creates theResultSet
. -
rowUpdated()
: Retrieves whether the current row has been updated. This method is optional with aResultSet
concurrency ofCONCUR_READ_ONLY
. -
rowInserted()
: Retrieves whether the current row has been inserted. This method is optional with aResultSet
concurrency ofCONCUR_READ_ONLY
. -
rowDeleted()
: Retrieves whether the current row has been deleted. This method is optional with aResultSet
concurrency ofCONCUR_READ_ONLY
. -
updateNull(int columnIndex)
: Updates the designated column with a null value. The update methods do not update the underlying database untilupdateRow
orinsertRow
is called. -
updateBoolean(int columnIndex, boolean x)
: Updates the designated column with aboolean
value. The update methods do not update the underlying database untilupdateRow
orinsertRow
is called. -
updateByte(int columnIndex, byte x)
: Updates the designated column with abyte
value. The update methods do not update the underlying database untilupdateRow
orinsertRow
is called.
Exception Handling
-
SQLException
: Thrown when a database access error occurs, or if a method is called on a closedResultSet
. -
SQLFeatureNotSupportedException
: Thrown if the JDBC driver does not support the method.
Additional Information
-
ResultSet.TYPE_FORWARD_ONLY
: TheResultSet
can only be navigated forward. Some methods related to navigation or row updates may be unsupported or behave differently with this type. -
ResultSet.FETCH_FORWARD
: Indicates that the rows in theResultSet
should be processed in forward order. -
ResultSet.CONCUR_READ_ONLY
: TheResultSet
is read-only. -
ResultSet.CONCUR_UPDATABLE
: TheResultSet
can be updated. -
Statement.setFetchDirection(int direction)
: Allows setting the fetch direction for theStatement
. -
DatabaseMetaData.updatesAreDetected(int)
: Reports whether visible updates are detected by the database. -
DatabaseMetaData.insertsAreDetected(int)
: Reports whether visible inserts are detected by the database. -
DatabaseMetaData.deletesAreDetected(int)
: Reports whether visible deletes are detected by the database. -
insertRow()
: Inserts the current row into the database. -
updateRow()
: Updates the row at the current position of the cursor.
ResultSet Object
-
FETCH_FORWARD
,FETCH_REVERSE
,FETCH_UNKNOWN
: Used for setting the direction of processing rows in a result set. -
TYPE_FORWARD_ONLY
,TYPE_SCROLL_INSENSITIVE
,TYPE_SCROLL_SENSITIVE
: Used for setting the type, determining cursor movement and sensitivity to data changes. -
CONCUR_READ_ONLY
,CONCUR_UPDATABLE
: Used for setting the concurrency mode, which determines if data can be updated. -
HOLD_CURSORS_OVER_COMMIT
,CLOSE_CURSORS_AT_COMMIT
: Used for setting the holdability, determining if openResultSet
objects remain open after commit.
ResultSet.next()
method
- Moves cursor forward one row.
- First call to
next()
makes the first row the current row, and so on. - When
next()
returnsfalse
, the cursor is after the last row. -
SQLException
is thrown if a current row is required but the cursor is not on a row. - For
TYPE_FORWARD_ONLY
result sets, whethernext()
returnsfalse
or throws anSQLException
is vendor-specific. - An open input stream for the current row is implicitly closed.
- The warning chain of
ResultSet
object is cleared when a new row is read.
ResultSet.close()
method
- Releases resources associated with the
ResultSet
. - Blobs, Clobs, or NClobs created by the
ResultSet
stay open until the transaction in which they were created ends, unless theirfree
method is invoked. -
ResultSetMetaData
instances created bygetMetaData
remain accessible. - Automatically called when the
Statement
that created it is closed, re-executed, or retrieves the next result from a sequence of multiple results.
ResultSet.wasNull()
method
- Determines whether the last column accessed was
SQL NULL
. - Requires calling a getter method first followed by
wasNull()
.
Column Retrieval Methods
- All getter methods for different data types (
getString()
,getInt()
, etc.,) returnnull
if the value isSQL NULL
. - These methods take either a
columnIndex
(first column is 1) or acolumnLabel
(specified withSQL AS
clause or the column name). -
getBigDecimal()
is deprecated in favor of using eithergetBigDecimal(int columnIndex)
orgetBigDecimal(String columnLabel)
.
Stream Retrieval Methods
-
Provide access to data in a stream format.
-
Useful for retrieving large values like
LONGVARCHAR
orLONGVARBINARY
. -
The stream must be fully read before accessing any further data.
-
getCharacterStream()
should be used instead ofgetUnicodeStream()
, which is deprecated. -
All stream methods implicitly close the stream upon calling another getter method.### ResultSet Methods for Data Retrieval
-
getByte(String columnLabel): Retrieves the designated column's value as a byte and returns it as a short. Returns 0 if the value is null.
-
getShort(String columnLabel): Retrieves the designated column's value as a short. Returns 0 if the value is null.
-
getInt(String columnLabel): Retrieves the designated column's value as an integer. Returns 0 if the value is null.
-
getLong(String columnLabel): Retrieves the designated column's value as a long. Returns 0 if the value is null.
-
getFloat(String columnLabel): Retrieves the designated column's value as a float. Returns 0 if the value is null.
-
getDouble(String columnLabel): Retrieves the designated column's value as a double. Returns 0 if the value is null.
-
getBigDecimal(String columnLabel, int scale): Retrieves the designated column's value as a BigDecimal with the specified scale. Returns null if the value is null.
-
getBytes(String columnLabel): Retrieves the designated column's value as a byte array. Returns null if the value is null.
-
getDate(String columnLabel): Retrieves the designated column's value as a java.sql.Date object. Returns null if the value is null.
-
getTime(String columnLabel): Retrieves the designated column's value as a java.sql.Time object. Returns null if the value is null.
-
getTimestamp(String columnLabel): Retrieves the designated column's value as a java.sql.Timestamp object. Returns null if the value is null.
-
getAsciiStream(String columnLabel): Retrieves the designated column's value as a stream of ASCII characters. Returns null if the value is null.
-
getUnicodeStream(String columnLabel): Retrieves the designated column's value as a stream of two-byte Unicode characters. Returns null if the value is null.
-
getBinaryStream(String columnLabel): Retrieves the designated column's value as a stream of uninterpreted bytes. Returns null if the value is null.
-
getObject(int columnIndex): Retrieves the designated column's value as an Object in Java, using the default Java object type based on the column's SQL type.
-
getObject(String columnLabel): Retrieves the designated column's value as an Object in Java, using the default Java object type based on the column's SQL type.
-
findColumn(String columnLabel): Maps the given column label to its corresponding index.
-
getCharacterStream(int columnIndex): Retrieves the designated column's value as a java.io.Reader object.
-
getCharacterStream(String columnLabel): Retrieves the designated column's value as a java.io.Reader object.
-
getBigDecimal(int columnIndex): Retrieves the designated column's value as a java.math.BigDecimal with full precision.
Handling Errors
- These methods throw an SQLException if the column label is invalid, a database access error occurs, or the method is called on a closed ResultSet.
- The getBigDecimal(String columnLabel, int scale) method throws a SQLFeatureNotSupportedException if the JDBC driver does not support this method.
ResultSet Properties
- getWarnings(): Retrieves the first logged warning associated with the ResultSet.
- clearWarnings(): Clears all warnings reported for the ResultSet.
- getCursorName(): Retrieves the name of the SQL cursor used by the ResultSet object.
- getMetaData(): Retrieves information about each column of the ResultSet, such as its name, type, and properties.
Additional Notes
- The columnLabel parameter refers to the column's name, which can be either the actual column name or an alias assigned using the SQL AS clause.
- Most of these methods can be called with either a column index (starting from 1) or a column label.
- When reading data from a stream, it is crucial to read all data before accessing other columns or methods. The next getter method call implicitly closes the stream.
- These methods support reading both standard SQL data types and database-specific abstract data types.
ResultSet Methods
- Methods are used to interact with result sets from database queries, representing the data retrieved.
-
getBigDecimal method retrieves a value as a
BigDecimal
with full precision and returnsnull
if the corresponding database value isNULL
. -
isBeforeFirst method checks if the cursor is before the first row - the method is optional for
TYPE_FORWARD_ONLY
result sets. -
isAfterLast method checks if the cursor is after the last row - the method is optional for
TYPE_FORWARD_ONLY
result sets. -
isFirst method checks if the cursor is on the first row - the method is optional for
TYPE_FORWARD_ONLY
result sets. - isLast method checks if the cursor is on the last row - the method might be expensive because it may need to fetch ahead one row to check.
-
beforeFirst method moves the cursor to the position before the first row, having no effect if the result set is empty - the method is optional for
TYPE_FORWARD_ONLY
result sets. -
afterLast method moves the cursor to the position after the last row, having no effect if the result set is empty - the method is optional for
TYPE_FORWARD_ONLY
result sets. -
first method moves the cursor to the first row and returns
true
if successful, orfalse
if the result set is empty. -
last method moves the cursor to the last row and returns
true
if successful, orfalse
if the result set is empty. - getRow method retrieves the current row number.
-
absolute method moves the cursor to a specific row number, accepting positive or negative values relative to the beginning or end of the result set respectively - the method is optional for
TYPE_FORWARD_ONLY
result sets. -
relative method moves the cursor a relative number of rows forward or backward, accepting a number of rows as parameter - the method is optional for
TYPE_FORWARD_ONLY
result sets. -
previous method moves the cursor to the previous row and returns
true
if successful, orfalse
if the cursor is already before the first row. -
setFetchDirection method provides a hint to the database driver about the expected direction of row processing - the method is optional for
TYPE_FORWARD_ONLY
result sets. - getFetchDirection method retrieves the current fetch direction set for the result set.
-
setFetchSize method provides a hint to the database driver about the number of rows to fetch in batches - the method is optional for
TYPE_FORWARD_ONLY
result sets. - getFetchSize method retrieves the current fetch size set for the result set.
-
getType method retrieves the type of the result set, which is determined by the statement that generated it - the method can return
TYPE_FORWARD_ONLY
,TYPE_SCROLL_INSENSITIVE
orTYPE_SCROLL_SENSITIVE
. - getConcurrency method retrieves the concurrency mode of the result set.
-
rowUpdated method checks if the current row has been updated - the method is optional for
CONCUR_READ_ONLY
result sets. -
rowInserted method checks if the current row has been newly inserted into the database - the method is optional for
CONCUR_READ_ONLY
result sets. -
rowDeleted method checks if the current row has been deleted from the database - the method is optional for
CONCUR_READ_ONLY
result sets. -
updateNull method updates the designated column of the current row to
NULL
. - updateBoolean method updates the designated column of the current row to a boolean value.
- updateByte method updates the designated column of the current row to a byte value.
- updateShort method updates the designated column of the current row to a short value.
Updater methods
-
Updater methods are used to update column values in the current row or the insert row.
-
Updater methods do not update the underlying database; they are used to set new values to be used in the
updateRow
orinsertRow
methods. -
The first column is 1, the second is 2, and so on.
-
These methods are called with a
columnIndex
(int) and a newx
(value) as parameters. -
Each method allows for updating the current row with a specific data type:
-
updateBoolean
(boolean) -
updateByte
(byte) -
updateShort
(short) -
updateInt
(int) -
updateLong
(long ) -
updateFloat
(float) -
updateDouble
(double) -
updateBigDecimal
(BigDecimal) -
updateString
(String) -
updateBytes
(byte[]) -
updateDate
(Date) -
updateTime
(Time) -
updateTimestamp
(Timestamp) -
updateAsciiStream
(InputStream, int length) -
updateBinaryStream
(InputStream, int length) -
updateCharacterStream
(Reader, int length) -
updateObject
(Object, int scaleOrLength) -
updateObject
(Object) -
updateNull
(String columnLabel)
-
-
All methods throw an exception if:
- The
columnIndex
is not valid. - A database access error occurs.
- The result set concurrency is
CONCUR_READ_ONLY
. - The method is called on a closed result set.
- The JDBC driver does not support the method (
SQLFeatureNotSupportedException
).
- The
-
All methods were added in Java 1.2.###
ResultSet
Updater Methods -
These methods update column values in the current row or insert row.
-
They do not update the underlying database; you must call
updateRow
orinsertRow
to update the database. -
These methods throw a
SQLException
if thecolumnLabel
is invalid, a database access error occurs, the result set concurrency isCONCUR_READ_ONLY
, or the method is called on a closed result set. -
They can also throw a
SQLFeatureNotSupportedException
if the JDBC driver does not support the method.
updateFloat
Method
- Sets the designated column with a
float
value.
updateDouble
Method
- Sets the designated column with a
double
value.
updateBigDecimal
Method
- Sets the designated column with a
java.sql.BigDecimal
value.
updateString
Method
- Sets the designated column with a
String
value.
updateBytes
Method
- Sets the designated column with a
byte[]
value.
updateDate
Method
- Sets the designated column with a
java.sql. Date
value.
updateTime
Method
- Sets the designated column with a
java.sql.Time
value.
updateTimestamp
Method
- Sets the designated column with a
java.sql.Timestamp
value.
updateAsciiStream
Method
- Sets the designated column with an
ascii
stream value with specified number of bytes.
updateBinaryStream
Method
- Sets the designated column with a
binary
stream value with specified number of bytes.
updateCharacterStream
Method
- Sets the designated column with a
character
stream value with specified number of bytes.
updateObject
Method
- Sets the designated column with an
Object
value. - If the second argument is an
InputStream
, the stream must contain the number of bytes specified byscaleOrLength
. - If the second argument is a
Reader
, the reader must contain the number of characters specified byscaleOrLength
. - If these conditions are not true, the driver will generate a
SQLException
when the statement is executed.
insertRow
Method
- Inserts the contents of the insert row into the
ResultSet
object and into the database. - The cursor must be on the insert row when this method is called.
updateRow
Method
- Updates the underlying database with the new contents of the current row of the
ResultSet
. - This method cannot be called when the cursor is on the insert row.
deleteRow
Method
- Deletes the current row from the
ResultSet
object and from the underlying database. - This method cannot be called when the cursor is on the insert row.
refreshRow
Method
- Refreshes the current row with its most recent value in the database.
- This method cannot be called when the cursor is on the insert row.
- It provides a way for an application to explicitly tell the JDBC driver to refetch a row(s) from the database.
- The JDBC driver may actually refresh multiple rows at once if the fetch size is greater than one.
- All values are refetched subject to the transaction isolation level and cursor sensitivity.
- If
refreshRow
is called after calling an updater method, but before calling theupdateRow
method, then the updates made to the row are lost. - Calling the
refreshRow
method frequently will likely slow performance.
ResultSet Updater Methods
- Methods are used to update values in the current row or insert row
- Do not update underlying database - instead,
updateRow
orinsertRow
methods must be called to update database - Methods throw an exception if you call them on a closed result set, the result set concurrency is
CONCUR_READ_ONLY
, or thecolumnIndex
is not valid - Methods throw an exception if there is a database access error or the driver does not support the method
updateBytes
- Takes
columnIndex
andbyte[]
as arguments - Updates designated column with a byte array value
updateDate
- Takes
columnIndex
andDate
as arguments - Updates designated column with a
java.sql.Date
value
updateTime
- Takes
columnIndex
andTime
as arguments - Updates designated column with a
java.sql.Time
value
updateTimestamp
- Takes
columnIndex
andTimestamp
as arguments - Updates designated column with a
java.sql.Timestamp
value
updateAsciiStream
- Takes
columnIndex
,InputStream
, andlength
as arguments - Updates designated column with an ascii stream value of specified length
updateBinaryStream
- Takes
columnIndex
,InputStream
, andlength
as arguments - Updates designated column with a binary stream value of specified length
updateCharacterStream
- Takes
columnIndex
,Reader
, andlength
as arguments - Updates designated column with a character stream value of specified length
updateObject
- Takes
columnIndex
,Object
, andscaleOrLength
as arguments - Updates designated column with an
Object
value - if theObject
is anInputStream
orReader
then the stream or reader must contain the number of bytes or characters specified in thescaleOrLength
parameters - Takes
columnIndex
andObject
as arguments - Updates designated column with an
Object
value
updateNull
- Takes
columnLabel
as arguments - Updates the designated column with a null value
updateBoolean
- Takes
columnLabel
andboolean
as arguments - Updates the designated column with a boolean value
updateByte
- Takes
columnLabel
andbyte
as arguments - Updates designated column with a byte value
updateShort
- Takes
columnLabel
andshort
as arguments - Updates designated column with a short value
updateInt
- Takes
columnLabel
andint
as arguments - Updates designated column with an int value
updateLong
- Takes
columnLabel
andlong
as arguments - Updates designated column with a long value
updateFloat
- Takes
columnLabel
andfloat
as arguments - Updates designated column with a float value
updateDouble
- Takes
columnLabel
anddouble
as arguments - Updates designated column with a double value
updateBigDecimal
- Takes
columnLabel
andBigDecimal
as arguments - Updates designated column with a
java.sql.BigDecimal
value
updateString
- Takes
columnLabel
andString
as arguments - Updates designated column with a String value
ResultSet Interface Methods
- The
ResultSet
interface represents a database result set, allowing you to retrieve and manipulate data. - The
ResultSet
object is created by executing aStatement
object, which can be used to execute SQL queries. - The
ResultSet
object provides methods to move through the result set, access data using labels or column indices, and update database content. -
updateXXX
methods are used to change data in the current row, which is then updated using theupdateRow
method. - Data is updated within the result set and does not automatically update the underlying database.
- The
updateXXX
methods do not directly modify the database; instead, they modify the content within theResultSet
object. - The
insertRow
method inserts the contents of the insert row into theResultSet
object and the database. - The
deleteRow
method removes the current row from theResultSet
object and the database. - The
refreshRow
method refetches the latest data for the current row from the database, as if it had been newly retrieved. - The
cancelRowUpdates
method discards any changes made to the current row since it was last read from the database. - The
moveToInsertRow
method places the cursor on a special "insert row", allowing for data to be prepared to be inserted into the database. - The
moveToCurrentRow
method moves the cursor back to the original position after working with the insert row. - The
getStatement
method retrieves theStatement
object that produced thisResultSet
object. If the result set was generated some other way, such as by a DatabaseMetaData method, then this method can potentially return null. - The
getObject
method retrieves the value of the designated column as anObject
in the Java programming language. - The
getRef
,getBlob
,getClob
, andgetArray
methods retrieve data from the current row and return it as aRef
,Blob
,Clob
, orArray
object, respectively. - The
getDate
method retrieves the value of the designated column as ajava.sql.Date
object in the Java programming language. This method uses the given calendar to construct an appropriate millisecond value for the date if the underlying database does not store timezone information.
ResultSet Methods
-
getByte(int columnIndex)
: Retrieves the value of the designated column in the current row as a Java byte. -
getByte(String columnLabel)
: Retrieves the value of the designated column in the current row as a Java byte. -
getShort(int columnIndex)
: Retrieves the value of the designated column in the current row as a Java short. -
getShort(String columnLabel)
: Retrieves the value of the designated column in the current row as a Java short. -
getInt(int columnIndex)
: Retrieves the value of the designated column in the current row as a Java integer. -
getInt(String columnLabel)
: Retrieves the value of the designated column in the current row as a Java integer. -
getLong(int columnIndex)
: Retrieves the value of the designated column in the current row as a Java long. -
getLong(String columnLabel)
: Retrieves the value of the designated column in the current row as a Java long. -
getFloat(int columnIndex)
: Retrieves the value of the designated column in the current row as a Java float. -
getFloat(String columnLabel)
: Retrieves the value of the designated column in the current row as a Java float. -
getDouble(int columnIndex)
: Retrieves the value of the designated column in the current row as a Java double. -
getDouble(String columnLabel)
: Retrieves the value of the designated column in the current row as a Java double. -
getBigDecimal(int columnIndex)
: Retrieves the value of the designated column in the current row as a Java BigDecimal. -
getBigDecimal(String columnLabel)
: Retrieves the value of the designated column in the current row as a Java BigDecimal. -
getString(int columnIndex)
: Retrieves the value of the designated column in the current row as a Java String. -
getString(String columnLabel)
: Retrieves the value of the designated column in the current row as a Java String. -
getBytes(int columnIndex)
: Retrieves the value of the designated column in the current row as a Java array of bytes. -
getBytes(String columnLabel)
: Retrieves the value of the designated column in the current row as a Java array of bytes. -
getDate(int columnIndex)
: Retrieves the value of the designated column in the current row as a java.sql.Date object. -
getDate(int columnIndex, Calendar cal)
: Retrieves the value of the designated column in the current row as a java.sql.Date object. Uses a Calendar object to construct an appropriate millisecond value for the date if the underlying database does not store timezone information. -
getDate(String columnLabel)
: Retrieves the value of the designated column in the current row as a java.sql.Date object. -
getDate(String columnLabel, Calendar cal)
: Retrieves the value of the designated column in the current row as a java.sql.Date object. Uses a Calendar object to construct an appropriate millisecond value for the date if the underlying database does not store timezone information. -
getTime(int columnIndex)
: Retrieves the value of the designated column in the current row as a java.sql.Time object. -
getTime(int columnIndex, Calendar cal)
: Retrieves the value of the designated column in the current row as a java.sql.Time object. Uses a Calendar object to construct an appropriate millisecond value for the time if the underlying database does not store timezone information. -
getTime(String columnLabel)
: Retrieves the value of the designated column in the current row as a java.sql.Time object. -
getTime(String columnLabel, Calendar cal)
: Retrieves the value of the designated column in the current row as a java.sql.Time object. Uses a Calendar object to construct an appropriate millisecond value for the time if the underlying database does not store timezone information. -
getTimestamp(int columnIndex)
: Retrieves the value of the designated column in the current row as a java.sql.Timestamp object. -
getTimestamp(int columnIndex, Calendar cal)
: Retrieves the value of the designated column in the current row as a java.sql.Timestamp object. Uses a Calendar object to construct an appropriate millisecond value for the timestamp if the underlying database does not store timezone information. -
getTimestamp(String columnLabel)
: Retrieves the value of the designated column in the current row as a java.sql.Timestamp object. -
getTimestamp(String columnLabel, Calendar cal)
: Retrieves the value of the designated column in the current row as a java.sql.Timestamp object. Uses a Calendar object to construct an appropriate millisecond value for the timestamp if the underlying database does not store timezone information. -
getURL(int columnIndex)
: Retrieves the value of the designated column in the current row as a java.net.URL object. -
getURL(String columnLabel)
: Retrieves the value of the designated column in the current row as a java.net.URL object. -
updateRef(int columnIndex, Ref x)
: Updates the designated column with a java.sql.Ref value. -
updateRef(String columnLabel, Ref x)
: Updates the designated column with a java.sql.Ref value. -
updateBlob(int columnIndex, Blob x)
: Updates the designated column with a java.sql.Blob value. -
updateBlob(String columnLabel, Blob x)
: Updates the designated column with a java.sql.Blob value. -
updateClob(int columnIndex, Clob x)
: Updates the designated column with a java.sql.Clob value. -
updateClob(String columnLabel, Clob x)
: Updates the designated column with a java.sql.Clob value. -
updateArray(int columnIndex, Array x)
: Updates the designated column with a java.sql.Array value. -
updateArray(String columnLabel, Array x)
: Updates the designated column with a java.sql.Array value. -
getRowId(int columnIndex)
: Retrieves the value of the designated column in the current row as a java.sql.RowId object. -
getRowId(String columnLabel)
: Retrieves the value of the designated column in the current row as a java.sql.RowId object. -
updateRowId(int columnIndex, RowId x)
: Updates the designated column with a RowId value. -
updateRowId(String columnLabel, RowId x)
: Updates the designated column with a RowId value. -
getHoldability()
: Retrieves the holdability of this ResultSet object. -
isClosed()
: Retrieves whether this ResultSet object has been closed. -
updateNString(int columnIndex, String nString)
: Updates the designated column with a String value. -
updateNString(String columnLabel, String nString)
: Updates the designated column with a String value. -
updateNClob(int columnIndex, NClob nClob)
: Updates the designated column with a java.sql.NClob value. -
updateNClob(String columnLabel, NClob nClob)
: Updates the designated column with a java.sql.NClob value.
Notes about ResultSet
-
The label of the column is used when referencing columns by name using the SQL AS clause, if not specified it defaults to the name of the column.
-
The result set concurrency is CONCUR_READ_ONLY if the result set is read-only.
-
The
updateRow
method commits the changes to the database. -
The
insertRow
method inserts a new row into the database. -
The
updateRow
andinsertRow
methods are used to update the database. -
The
updateRow
andinsertRow
methods are called to update the database. -
The
getHoldability()
method returns either ResultSet.HOLD_CURSORS_OVER_COMMIT or ResultSet.CLOSE_CURSORS_AT_COMMIT. -
The
isClosed()
method returns true if the ResultSet object is closed and false if it is still open. -
The
updateNString()
andupdateNClob()
methods are intended for use when updating NCHAR, NVARCHAR, and LONGNVARCHAR columns.### ResultSet Updates -
Updating a ResultSet is a multi-step process:
- Update methods modify the data within the ResultSet, not the database itself.
-
updateRow
orinsertRow
methods are used to apply the modifications to the underlying database.
-
updateNClob(String columnLabel, NClob nClob)
- Updates a column with a
java.sql.NClob
value. - The
columnLabel
argument can be the column name itself or the label defined by the SQL AS clause. - Throws an
SQLException
if the columnLabel is invalid, the driver doesn't support national character sets, a data conversion error is detected, the result set is closed, or a database access error occurs. - Additionally throws
SQLFeatureNotSupportedException
if the JDBC driver doesn't support the method.
- Updates a column with a
-
updateSQLXML(int columnIndex, SQLXML xmlObject)
- Updates a column with a
java.sql.SQLXML
value. - The
columnIndex
argument starts at 1 for the first column, 2 for the second, and so on. - Throws
SQLException
for invalid column index, database access errors, errors processing the XML value, or if the result set is closed. - Also throws
SQLFeatureNotSupportedException
if the driver doesn't support the method.
- Updates a column with a
-
updateNCharacterStream(String columnLabel, Reader reader, long length)
- Updates a column with a character stream value.
- The
length
argument specifies the number of bytes in the stream. - It's used for updating
NCHAR
,NVARCHAR
, andLONGNVARCHAR
columns. - Throws
SQLException
for invalid column label, database access errors, or if the result set is closed. - Also throws
SQLFeatureNotSupportedException
if the driver doesn't support the method.
-
updateAsciiStream(int columnIndex, InputStream x, long length)
- Updates a column with an ascii stream value.
- The
length
argument defines the number of bytes in the stream. - Throws
SQLException
for invalid column index, database access errors, or if the result set is closed. - Also throws
SQLFeatureNotSupportedException
if the driver doesn't support the method.
-
updateBinaryStream(String columnLabel, InputStream x, long length)
- Updates a column with a binary stream value.
- The
length
argument specifies the number of bytes in the stream. - Throws
SQLException
for invalid column label, database access errors, or if the result set is closed. - Also throws
SQLFeatureNotSupportedException
if the driver doesn't support the method.
-
updateCharacterStream(String columnLabel, Reader reader, long length)
- Updates a column with a character stream value.
- The
length
argument defines the number of bytes in the stream. - Throws
SQLException
for invalid columnLabel, database access errors, or if the result set is closed. - Also throws
SQLFeatureNotSupportedException
if the driver doesn't support the method.
-
updateBlob(int columnIndex, InputStream inputStream, long length)
- Updates a column using a given input stream.
- The
length
argument specifies the number of bytes in the stream. - Throws
SQLException
for invalid column index, database access errors, or if the result set is closed. - Also throws
SQLFeatureNotSupportedException
if the driver doesn't support the method.
ResultSet Retrieving Data
-
getNClob(String columnLabel)
- Retrieves a value from the current row as a
java.sql.NClob
object. - The
columnLabel
argument can be the column name or the label defined in the SQL AS clause. - Throws
SQLException
for invalid columnLabel, database access errors, or if the result set is closed. - Also throws
SQLFeatureNotSupportedException
if the driver doesn't support the method.
- Retrieves a value from the current row as a
-
getSQLXML(int columnIndex)
- Retrieves a value from the current row as a
java.sql.SQLXML
object. - The
columnIndex
argument starts from 1, with 2 for the second column, and so on. - Throws
SQLException
for invalid column index, database access errors, or if the result set is closed. - Also throws
SQLFeatureNotSupportedException
if the driver doesn't support the method.
- Retrieves a value from the current row as a
-
getNString(String columnLabel)
- Retrieves a value from the current row as a
java.lang.String
object. - The
columnLabel
argument can be the column name or the label defined in the SQL AS clause. - Used for retrieving data from
NCHAR
,NVARCHAR
, andLONGNVARCHAR
columns. - Throws
SQLException
for invalid columnLabel, database access errors, or if the result set is closed. - Also throws
SQLFeatureNotSupportedException
if the driver doesn't support the method.
- Retrieves a value from the current row as a
-
getNCharacterStream(int columnIndex)
- Retrieves a value from the current row as a
java.io.Reader
object. - The
columnIndex
argument starts from 1, with 2 for the second column, and so on. - Used for retrieving data from
NCHAR
,NVARCHAR
, andLONGNVARCHAR
columns. - Throws
SQLException
for invalid column index, database access errors, or if the result set is closed. - Also throws
SQLFeatureNotSupportedException
if the driver doesn't support the method.
- Retrieves a value from the current row as a
Updating ResultSet Data
-
ResultSet
objects can be used to update data in a database in a row-by-row manner -
Update methods are available for various data types including:
-
updateBlob()
- for updating BLOB columns using anInputStream
object -
updateClob()
- for updating CLOB columns using aReader
object -
updateNClob()
- for updating NCLOB columns using aReader
object -
updateNCharacterStream()
- for updating NCHAR, NVARCHAR and LONGNVARCHAR columns using aReader
object -
updateAsciiStream()
- for updating columns using anInputStream
object -
updateBinaryStream()
- for updating columns using anInputStream
object -
updateCharacterStream()
- for updating columns using aReader
object
-
-
updateRow()
orinsertRow()
methods are then used to apply the updates to the actual database -
All updater methods are not available for
ResultSet
objects that have CONCUR_READ_ONLY concurrency
Updating with Input/Reader Objects
-
updateBlob()
methods take anInputStream
object as input, which contains the complete data for the corresponding column -
updateClob()
andupdateNClob()
methods take aReader
object as input, which contains the character data for the corresponding column -
Each
updateXyz()
method provides two versions:- One version uses the column index, starting from 1
- The other version uses the column label, which can be specified in the SQL query using the
AS
clause
-
In all
update()
methods,length
(number of bytes/characters) can be specified as a parameter, making it more efficient for handling large data -
If no length is specified, the entire contents of the
InputStream
orReader
object will be read
Handling Large Data
- When dealing with large amounts of data, particularly
BLOB
andCLOB
values, usingInputStream
andReader
objects is more efficient than storing the data directly in memory - The JDBC driver handles reading the data from the stream as needed, reducing memory usage
Updating Columns with Streams
- When updating columns using streams, the entire contents of the stream are read until the end-of-stream is reached
- If the driver supports it, there might be more efficient versions of
updateXyz()
methods that take a length parameter
Example Code - Updating a Blob Column
// Update a Blob column using InputStream
ResultSet resultSet = ... ;
InputStream inputStream = ... ;
int columnIndex = 3;
// Set the Blob data
resultSet.updateBlob(columnIndex, inputStream);
// Apply the update to the database
resultSet.updateRow();
ResultSet
Updater Methods
- These methods are used to update column values in the current row or the insert row.
- They do not update the underlying database; instead,
updateRow
orinsertRow
methods are called to update the database. - The methods take a
columnIndex
orcolumnLabel
as input, indicating the column to be updated. - The methods throw
SQLException
if the providedcolumnIndex
orcolumnLabel
is invalid, or if a database access error occurs. - They also throw
SQLException
if the result set concurrency isCONCUR_READ_ONLY
or the method is called on a closed result set. - If the JDBC driver does not support the method, it throws a
SQLFeatureNotSupportedException
.
Updater Methods for Different Data Types
- The
updateBytes
method updates a column with abyte
array value. - The
updateDate
method updates a column with ajava.sql.Date
value. - The
updateTime
method updates a column with ajava.sql.Time
value. - The
updateTimestamp
method updates a column with ajava.sql.Timestamp
value. - The
updateAsciiStream
method updates a column with an ASCII stream value, taking a stream and its specified length. - The
updateBinaryStream
method updates a column with a binary stream value, taking a stream and its specified length. - The
updateCharacterStream
method updates a column with a character stream value, taking a reader and its specified length. - The
updateObject
method updates a column with an object value, taking the object, and an optionalscaleOrLength
parameter depending on the object type (e.g.,BigDecimal
,InputStream
,Reader
). - The
updateNull
method updates a column with a null value, taking thecolumnLabel
as input. - The
updateBoolean
method updates a column with a boolean value, taking thecolumnLabel
and the boolean value. - The
updateByte
,updateShort
,updateInt
,updateLong
,updateFloat
,updateDouble
,updateBigDecimal
, andupdateString
methods update a column with corresponding data type values, taking acolumnLabel
and the value as input. - All these methods have versions that take a
columnIndex
and the value as input, similar to the ones described above. - These methods are generally used to update individual column values, but can also be applied to insert rows.
ResultSet Updaters
- Update methods are used to change values for the current or insert rows, but do not update the database itself.
-
updateRow()
updates the database with the changes made in the current row. -
insertRow()
inserts the values from the insert row into the database. -
moveToInsertRow()
moves the cursor to the insert row allowing for a new row to be constructed. -
moveToCurrentRow()
returns the cursor back to the remembered position usually the current row. -
cancelRowUpdates()
rolls back updates made to the current row.
Getters
- Getters allow you to access values from within the ResultSet at a specific column.
- When calling the SQL AS clause, the label is the name of the column.
-
getStatement()
returns the Statement that produced the ResultSet, or null if produced in another way. -
getObject()
returns an Object representing the SQL value in the specified column. -
getRef()
returns aRef
object representing the SQL REF value. -
getBlob()
returns aBlob
object representing the SQL BLOB value. -
getClob()
returns aClob
object representing the SQL CLOB value. -
getArray()
returns anArray
object representing the SQL ARRAY value. -
getDate()
returns aDate
object representing the SQL Date value.
Exceptions
-
SQLException
will be thrown if:- The
columnLabel
is invalid. - A database access error occurs.
- The result set concurrency is CONCUR_READ_ONLY.
- The method is called on a closed result set.
- The
-
SQLFeatureNotSupportedException
will be thrown if the JDBC driver does not support the called method.
ResultSet Methods
-
getDate()
retrieves the value of a designated column as ajava.sql.Date
object.- Accepts a
columnIndex
or acolumnLabel
as parameters. - If no SQL
AS
clause was defined, the column label is the name of the column. - Returns
null
if the value is SQLNULL
. - Throws an
SQLException
if the column is not valid, a database error occurs, or the method is called on a closedResultSet
.
- Accepts a
-
getTime()
retrieves the value of a designated column as ajava.sql.Time
object.- Accepts a
columnIndex
, acolumnLabel
, and aCalendar
object. - Uses the given
Calendar
object to construct an appropriate millisecond value if the underlying database doesn't store timezone information. - Returns
null
if the value is SQLNULL
. - Throws an
SQLException
if the column is not valid, a database error occurs, or the method is called on a closedResultSet
.
- Accepts a
-
getTimestamp()
retrieves the value of a designated column as ajava.sql.Timestamp
object.- Accepts a
columnIndex
, acolumnLabel
, and aCalendar
object. - Uses the given
Calendar
object to construct an appropriate millisecond value if the underlying database doesn't store timezone information. - Returns
null
if the value is SQLNULL
. - Throws an
SQLException
if the column is not valid, a database error occurs, or the method is called on a closedResultSet
.
- Accepts a
-
getURL()
retrieves the value of a designated column as ajava.net.URL
object.- Accepts a
columnIndex
or acolumnLabel
. - Returns
null
if the value is SQLNULL
. - Throws an
SQLException
if the column is not valid, a database error occurs, theResultSet
is closed, or the URL is malformed. - Throws an
SQLFeatureNotSupportedException
if the JDBC driver doesn't support this method.
- Accepts a
-
updateRef()
updates the designated column with ajava.sql.Ref
value.- Accepts a
columnIndex
or acolumnLabel
and aRef
object. - Doesn't update the database directly;
updateRow
orinsertRow
methods are used for that purpose. - Throws an
SQLException
if the column is not valid, a database error occurs, theResultSet
concurrency isCONCUR_READ_ONLY
, or the method is called on a closedResultSet
. - Throws an
SQLFeatureNotSupportedException
if the JDBC driver doesn't support this method.
- Accepts a
-
updateBlob()
updates the designated column with ajava.sql.Blob
value.- Accepts a
columnIndex
or acolumnLabel
and aBlob
object. - Doesn't update the database directly;
updateRow
orinsertRow
methods are used for that purpose. - Throws an
SQLException
if the column is not valid, a database error occurs, theResultSet
concurrency isCONCUR_READ_ONLY
, or the method is called on a closedResultSet
. - Throws an
SQLFeatureNotSupportedException
if the JDBC driver doesn't support this method.
- Accepts a
-
updateClob()
updates the designated column with ajava.sql.Clob
value.- Accepts a
columnIndex
or acolumnLabel
and aClob
object. - Doesn't update the database directly;
updateRow
orinsertRow
methods are used for that purpose. - Throws an
SQLException
if the column is not valid, a database error occurs, theResultSet
concurrency isCONCUR_READ_ONLY
, or the method is called on a closedResultSet
. - Throws an
SQLFeatureNotSupportedException
if the JDBC driver doesn't support this method.
- Accepts a
-
updateArray()
updates the designated column with ajava.sql.Array
value.- Accepts a
columnIndex
or acolumnLabel
and anArray
object. - Doesn't update the database directly;
updateRow
orinsertRow
methods are used for that purpose. - Throws an
SQLException
if the column is not valid, a database error occurs, theResultSet
concurrency isCONCUR_READ_ONLY
, or the method is called on a closedResultSet
. - Throws an
SQLFeatureNotSupportedException
if the JDBC driver doesn't support this method.
- Accepts a
-
getRowId()
retrieves the value of a designated column as ajava.sql.RowId
object.- Accepts a
columnIndex
or acolumnLabel
. - Returns
null
if the value is SQLNULL
. - Throws an
SQLException
if the column is not valid, a database error occurs, or the method is called on a closedResultSet
. - Throws an
SQLFeatureNotSupportedException
if the JDBC driver doesn't support this method.
- Accepts a
-
updateRowId()
updates the designated column with aRowId
value.- Accepts a
columnIndex
or acolumnLabel
and aRowId
object. - Doesn't update the database directly;
updateRow
orinsertRow
methods are used for that purpose. - Throws an
SQLException
if the column is not valid, a database error occurs, theResultSet
concurrency isCONCUR_READ_ONLY
, or the method is called on a closedResultSet
. - Throws an
SQLFeatureNotSupportedException
if the JDBC driver doesn't support this method.
- Accepts a
-
getHoldability()
retrieves the holdability of aResultSet
.- Returns
ResultSet.HOLD_CURSORS_OVER_COMMIT
orResultSet.CLOSE_CURSORS_AT_COMMIT
. - Throws an
SQLException
if a database error occurs, or the method is called on a closedResultSet
.
- Returns
-
isClosed()
checks if aResultSet
has been closed.- Returns
true
if theResultSet
is closed, otherwise returnsfalse
. - Throws an
SQLException
if a database error occurs.
- Returns
-
updateNString()
updates the designated column with aString
value, mainly for updatingNCHAR
,NVARCHAR
, andLONGNVARCHAR
columns.- Accepts a
columnIndex
or acolumnLabel
and aString
object. - Doesn't update the database directly;
updateRow
orinsertRow
methods are used for that purpose. - Throws an
SQLException
if the column is not valid, the driver doesn't support national character sets, the driver detects a potential data conversion error, the method is called on a closedResultSet
, theResultSet
concurrency isCONCUR_READ_ONLY
, or a database error occurs. - Throws an
SQLFeatureNotSupportedException
if the JDBC driver doesn't support this method.
- Accepts a
-
updateNClob()
updates the designated column with ajava.sql.NClob
value.- Accepts a
columnIndex
or acolumnLabel
, and aNClob
object. - Doesn't update the database directly;
updateRow
orinsertRow
methods are used for that purpose. - Throws an
SQLException
if the column is not valid, a database error occurs, theResultSet
concurrency isCONCUR_READ_ONLY
, or the method is called on a closedResultSet
. - Throws an
SQLFeatureNotSupportedException
if the JDBC driver doesn't support this method.
- Accepts a
ResultSet Methods
-
updateNClob(String columnLabel, NClob nClob): Updates a column in the current row or insert row with a
java.sql.NClob
value. -
getNClob(int columnIndex): Retrieves the value of the designated column in the current row as a
NClob
object. -
getNClob(String columnLabel): Retrieves the value of a column using its label as a
NClob
object. -
getSQLXML(int columnIndex): Retrieves the value of a column by its index as a
java.sql.SQLXML
object. -
getSQLXML(String columnLabel): Retrieves the value of a column by its label as a
java.sql.SQLXML
object. -
updateSQLXML(int columnIndex, SQLXML xmlObject): Updates the value of a column with a
java.sql.SQLXML
object. -
updateSQLXML(String columnLabel, SQLXML xmlObject): Updates the value of a column with a
java.sql.SQLXML
object using its label. -
getNString(int columnIndex): Retrieves the value of a column, intended for use with
NCHAR
,NVARCHAR
, andLONGNVARCHAR
columns, as aString
object. -
getNString(String columnLabel): Retrieves the value of a column, intended for use with
NCHAR
,NVARCHAR
, andLONGNVARCHAR
columns, as aString
object using its label. -
getNCharacterStream(int columnIndex): Retrieves the value of a column as a
java.io.Reader
object, intended for use withNCHAR
,NVARCHAR
, andLONGNVARCHAR
columns. -
getNCharacterStream(String columnLabel): Retrieves the value of a column as a
java.io.Reader
object using its label, intended for use withNCHAR
,NVARCHAR
, andLONGNVARCHAR
columns. -
updateNCharacterStream(int columnIndex, Reader x, long length): Updates the value of a column using a
Reader
object, intended for use withNCHAR
,NVARCHAR
, andLONGNVARCHAR
columns. -
updateNCharacterStream(String columnLabel, Reader reader, long length): Updates the value of a column using a
Reader
object and its label, intended for use withNCHAR
,NVARCHAR
, andLONGNVARCHAR
columns.
Updating Columns
-
updateAsciiStream(int columnIndex, InputStream x, long length), updateAsciiStream(String columnLabel, InputStream x, long length): Updates a column with an ASCII stream using an
InputStream
object. -
updateBinaryStream(int columnIndex, InputStream x, long length), updateBinaryStream(String columnLabel, InputStream x, long length): Updates a column with a binary stream using an
InputStream
object. -
updateCharacterStream(int columnIndex, Reader x, long length), updateCharacterStream(String columnLabel, Reader reader, long length): Updates a column with a character stream using a
Reader
object. -
updateBlob(int columnIndex, InputStream inputStream, long length): Updates a column with a
BLOB
value using anInputStream
object.
Exceptions
- SQLException: Thrown for errors related to invalid column indices, unsupported features, data conversion errors, closed result sets, database access errors, or read-only result set concurrencies.
- SQLFeatureNotSupportedException: Thrown if the JDBC driver does not support the specific method.
Notes
- The
update
methods update column values on the current row or insert row. - They don't update the database directly; you need to call
updateRow
orinsertRow
to apply changes. - The
get
methods retrieve values from the current row. - Java character format is converted to the database's national character set by the driver.
- The
length
parameter in update methods specifies the length of the stream.
JDBC Updater Methods
-
updateBlob(): Updates a designated column using an input stream. This method is used to update column values in the current row or the insert row.
-
Parameters:
- columnIndex: The column index, starting from 1.
- columnLabel: The label for the column, specified with the SQL AS clause. If not specified, the label is the column name.
- inputStream: An object that contains the data to set the parameter value to.
- length: The number of bytes in the parameter data. This parameter is optional and can be omitted.
-
Throws:
- SQLException: If the columnIndex or columnLabel is invalid, a database access error occurs, the result set concurrency is CONCUR_READ_ONLY, or the method is called on a closed result set.
- SQLFeatureNotSupportedException: If the JDBC driver does not support this method.
-
Parameters:
-
updateClob(): Updates a designated column using a Reader object. This method is used for updating columns with large UNICODE values.
-
Parameters:
- columnIndex: The column index, starting from 1.
- columnLabel: The label for the column, specified with the SQL AS clause. If not specified, the label is the column name.
- reader: An object that contains the data to set the parameter value to.
- length: The number of characters in the parameter data. This parameter is optional and can be omitted.
-
Throws:
- SQLException: If the columnIndex or columnLabel is invalid, a database access error occurs, the result set concurrency is CONCUR_READ_ONLY, or the method is called on a closed result set.
- SQLFeatureNotSupportedException: If the JDBC driver does not support this method.
-
Parameters:
-
updateNClob(): Updates a designated column using a Reader object with national character support.
-
Parameters:
- columnIndex: The column index, starting from 1.
- columnLabel: The label for the column, specified with the SQL AS clause. If not specified, the label is the column name.
- reader: An object that contains the data to set the parameter value to.
- length: The number of characters in the parameter data. This parameter is optional and can be omitted.
-
Throws:
- SQLException: If the columnIndex or columnLabel is invalid, the driver does not support national character sets, a data conversion error could occur, the method is called on a closed result set, or a database access error occurs.
- SQLFeatureNotSupportedException: If the JDBC driver does not support this method.
-
Parameters:
-
updateNCharacterStream(): Updates a designated column with a character stream value.
-
Parameters:
- columnIndex: The column index, starting from 1.
- columnLabel: The label for the column, specified with the SQL AS clause. If not specified, the label is the column name.
- x: The new column value.
-
Throws:
- SQLException: If the columnIndex or columnLabel is invalid, a database access error occurs, the result set concurrency is CONCUR_READ_ONLY, or the method is called on a closed result set.
- SQLFeatureNotSupportedException: If the JDBC driver does not support this method.
-
Parameters:
-
updateAsciiStream(): Updates a designated column with an ASCII stream value.
-
Parameters:
- columnIndex: The column index, starting from 1.
- columnLabel: The label for the column, specified with the SQL AS clause. If not specified, the label is the column name.
- x: The new column value.
-
Throws:
- SQLException: If the columnIndex or columnLabel is invalid, a database access error occurs, the result set concurrency is CONCUR_READ_ONLY, or the method is called on a closed result set.
- SQLFeatureNotSupportedException: If the JDBC driver does not support this method.
-
Parameters:
-
updateBinaryStream(): Updates a designated column with a binary stream value.
-
Parameters:
- columnIndex: The column index, starting from 1.
- columnLabel: The label for the column, specified with the SQL AS clause. If not specified, the label is the column name.
- x: The new column value.
-
Throws:
- SQLException: If the columnIndex or columnLabel is invalid, a database access error occurs, the result set concurrency is CONCUR_READ_ONLY, or the method is called on a closed result set.
- SQLFeatureNotSupportedException: If the JDBC driver does not support this method.
-
Parameters:
-
updateCharacterStream(): Updates a designated column with a character stream value.
-
Parameters:
- columnIndex: The column index, starting from 1.
- columnLabel: The label for the column, specified with the SQL AS clause. If not specified, the label is the column name.
- x: The new column value.
-
Throws:
- SQLException: If the columnIndex or columnLabel is invalid, a database access error occurs, the result set concurrency is CONCUR_READ_ONLY, or the method is called on a closed result set.
- SQLFeatureNotSupportedException: If the JDBC driver does not support this method.
-
Parameters:
-
Key Points:
- The updater methods do not directly update the database. The updateRow() or insertRow() methods are required for database updates.
- Each updater method has a version that takes both the columnIndex and the columnLabel as parameters.
- Most updater methods have a version that takes a length parameter for efficiency.
- These methods offer flexibility to update columns with various data types and large data volumes.
Update Methods
-
updateRow(): Applies the changes made to the current row of the ResultSet to the database.
-
Throws:
- SQLException: If the result set concurrency is CONCUR_READ_ONLY, the method is called on a closed result set, or a database access error occurs.
-
Throws:
-
insertRow(): Inserts the data in the insert row of the ResultSet into the database.
-
Throws:
- SQLException: If the result set concurrency is CONCUR_READ_ONLY, the method is called on a closed result set, or a database access error occurs.
-
Throws:
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Test your knowledge of JDBC ResultSet concepts! This quiz covers the initial cursor position, methods for navigating rows, types of ResultSet, and best practices for data retrieval using getter methods. Perfect for students and professionals looking to enhance their understanding of JDBC.