ResultSet
By Tekuru Sireesha
ResultSet class
The executeQuery() method is used to send the query to the
DBMS for processing and returns a ResultSet object that contains data requested by the query.
The ResultSet object contains methods that are used to copy data
from the ResultSet into a Java collection of objects or variable(s) for further processing.
Data in a ResultSet object is logically organized into a virtual table
consisting of rows and columns.
In addition to data, the ResultSet object also contains metadata,
such as column names, column size, and column data type.
next() method
The virtual cursor is positioned above the first row of
data when the ResultSet is returned by the executeQuery() method. This means that the virtual cursor must be moved to the first row using the next() method.
The next() method returns a boolean true if the row
contains data, otherwise a boolean false is returned, indicating that no more rows exist in the ResultSet.
getxxx()
Once the virtual cursor points to a row, the getXXX() method is
used to copy data from the row to a collection, object, or variable.
the getXXX() method is data type specific. The data type of the getXXX() method must be the same data
type of the column in the ResultSet.
The getXXX() method requires one parameter, which is an
integer that represents the number of the column that contains the data.
For example, getString(1) copies thedata from the first column
of the ResultSet.
Scrollable ResultSet
Six methods of the ResultSet object are used to position the
virtual cursor, in addition to the next() method
The first() method moves the virtual cursor to the first row in
the ResultSet.
the last() method positions the virtual cursor at the last row in
the ResultSet.
The previous() method moves the virtual cursor to the
previous row.
The absolute() method positions the virtual cursor at the row
number specified by the integer passed as a parameter.
The relative() method moves the virtual cursor the specified
number of rows contained in the parameter. The parameter is a positive or negative integer, where the sign represents the direction the virtual cursor is moved.
the getRow() method returns an integer that represents the
number of the current row in the ResultSet.
The Statement object that is created using the
createStatement() of the Connection object must be set up to handle a scrollable ResultSet by passing the createStatement() method one of three constants.
The TYPE_FORWARD_ONLY constant restricts the virtual
cursor to downward movement, which is the default setting.
The TYPE_SCROLL_INSENSITIVE constant makes the
ResultSet insensitive to data changes made by another J2ME application in the table whose rows are reflected in the ResultSet.
The TYPE_SCROLL_SENSITIVE constant makes the ResultSet
sensitive to those changes.
Specify Number of Rows to Return The fetch size is set by using the setFetchSize() method
Updatable ResultSet Rows contained in the ResultSet can be updated similar to how rows in a table can be updated.
This is made possible by passing the createStatement()
method of the Connection object the CONCUR_UPDATABLE.
Alternatively, the CONCUR_READ_ONLY constant can be
passed to the createStatement() method to prevent the ResultSet from being updated.
Update a Value in ResultSet
The updateXXX() method is used to change the value of a column in the current row of the ResultSet. The XXX is replaced with the data type of the column that is to be updated. The updateXXX() method requires two parameters. The first is either the number or name of the column of the ResultSet that is being updated, and the second is the value that will replace the value in the column of the ResultSet. A value in a column of the ResultSet can be replaced with a NULL value by using the updateNull() method. The updateNull() method requires one parameter, which is the number of the
column in the current row of the ResultSet.
The updateRow() method is called after all the updateXXX() methods are called.
Delete a Row in the ResultSet
The deleteRow() method is used to remove a row from a ResultSet. The deleteRow() method is passed an integer that contains the number of the row to be deleted. The deleteRow() method is then passed a zero integer indicating that the current row must be deleted
Insert a Row in the ResultSet
Inserting a row into the ResultSet is accomplished using basically the same
technique used to update the ResultSet.
the updateXXX() method is used to specify the column and value that will be placed into the column of the ResultSet.