In the previous lesson,
ResultSetwas briefly mentioned. Now, you will learn the details of theResultSetinterface. TheResultSetinterface provides methods for retrieving and manipulting the results of executed queries, andResultSetobjects can have different functionality and characteristics. These characteristics are result set type, result set concurrency, and cursor holdability. A table of data representing a database result set is usually generated by executing a statement that queries the database.The type of a
ResultSetobject determines the level of its functionality in two areas: the ways in which the cursor can be manipulated, and how concurrent changes made to the underlying data source are reflected by theResultSetobject.The sensitivity of the
ResultSetobject is determined by one of three differentResultSettypes:
TYPE_FORWARD_ONLY— The result set is not scrollable; its cursor moves forward only, from before the first row to after the last row. The rows contained in the result set depend on how the underlying database materializes the results. That is, it contains the rows that satisfy the query at either the time the query is executed or as the rows are retrieved.TYPE_SCROLL_INSENSITIVE— The result set is scrollable; its cursor can move both forward and backward relative to the current position, and it can move to an absolute position.TYPE_SCROLL_SENSITIVE— The result set is scrollable; its cursor can move both forward and backward relative to the current position, and it can move to an absolute position.Now, you'll see how to send the above
SELECTstatements from a program written in the Java™ programming language and how you get the results we showed.JDBC™ returns results in a
ResultSetobject, so we need to declare an instance of the classResultSetto hold our results. In addition, theStatementmethodsexecuteQueryandgetResultSetboth return aResultSetobject, as do variousDatabaseMetaDatamethods. The following code demonstrates declaring theResultSetobjectrsand assigning the results of our earlier query to it by using theexecuteQuerymethod.Before you can take advantage of these features, however, you need to create a scrollable
ResultSetobject. The following line of code illustrates one way to create a scrollableResultSetobject:Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet srs = stmt.executeQuery("SELECT COF_NAME, PRICE FROM COFFEES");This code is similar to what you have used earlier, except that it adds two arguments to the
createStatementmethod. The first argument is one of three constants added to theResultSetAPI to indicate the type of aResultSetobject:TYPE_FORWARD_ONLY,TYPE_SCROLL_INSENSITIVE, andTYPE_SCROLL_SENSITIVE. The second argument is one of twoResultSetconstants for specifying whether a result set is read-only or updatable:CONCUR_READ_ONLYandCONCUR_UPDATABLE. The point to remember here is that if you specify a type, you must also specify whether it is read-only or updatable. Also, you must specify the type first, and because both parameters are of typeint, the compiler will not complain if you switch the order.Specifying the constant
TYPE_FORWARD_ONLYcreates a nonscrollable result set, that is, one in which the cursor moves only forward. If you do not specify any constants for the type and updatability of aResultSetobject, you will automatically get one that isTYPE_FORWARD_ONLYandCONCUR_READ_ONLY.Using the ResultSet Methods
The variable
srs, which is an instance ofResultSet, contains the rows of coffees and prices shown in the result set example above. In order to access the names and prices. AResultSetobject maintains a cursor, which points to its current row of data.When a
ResultSetobject is first created, the cursor is positioned before the first row. To move the cursor, you can use the following methods:
next()- moves the cursor forward one row. Returns true if the cursor is now positioned on a row and false if the cursor is positioned after the last row.previous()- moves the cursor backwards one row. Returns true if the cursor is now positioned on a row and false if the cursor is positioned before the first row.first()- moves the cursor to the first row in theResultSetobject. Returns true if the cursor is now positioned on the first row and false if theResultSetobject does not contain any rows.last()- moves the cursor to the last row in theResultSetobject. Returns true if the cursor is now positioned on the last row and false if theResultSetobject does not contain any rows.beforeFirst()- positions the cursor at the start of theResultSetobject, before the first row. If theResultSetobject does not contain any rows, this method has no effect.afterLast()- positions the cursor at the end of theResultSetobject, after the last row. If theResultSetobject does not contain any rows, this method has no effect.relative(int rows)- moves the cursor relative to its current position.absolute(int row)- positions the cursor on the row-th row of theResultSetobject.Once you have a scrollable
ResultSetobject,srsin the previous example, you can use it to move the cursor around in the result set. Since the cursor is initially positioned just above the first row of aResultSetobject, the first call to the methodnextmoves the cursor to the first row and makes it the current row. Successive invocations of the methodnextmove the cursor down one row at a time from top to bottom.Using the getXXX Methods
The
ResultSetinterface declares getter methods (getBoolean,getLong, and so on) for retrieving column values from the current row. Your application can retrieve values using either the index number of the column or the name of the column. The column index is usually more efficient. Columns are numbered from 1. For maximum portability, result set columns within each row should be read in left-to-right order, and each column should be read only once.Column names used as input to getter methods are case insensitive. When a getter method is called with a column name and several columns have the same name, the value of the first matching column will be returned. The column name option is designed to be used when column names are used in the SQL query that generated the result set. For columns that are NOT explicitly named in the query, it is best to use column numbers. If column names are used, the programmer should take care to guarantee that they uniquely refer to the intended columns, which can be assured with the SQL AS clause.
The
getXXXmethod of the appropriate type retrieves the value in each column. For example, the first column in each row ofsrsisCOF_NAME, which stores a value of SQL typeVARCHAR. The method for retrieving a value of SQL typeVARCHARisgetString. The second column in each row stores a value of SQL typeFLOAT, and the method for retrieving values of that type isgetFloat. The following code accesses the values stored in the current row ofsrsand prints a line with the name followed by three spaces and the price. Each time the methodnextis invoked, the next row becomes the current row, and the loop continues until there are no more rows inrs.The method
getStringis invoked on theResultSetobjectsrs, sogetStringretrieves (gets) the value stored in the columnCOF_NAMEin the current row ofsrs. The value thatgetStringretrieves has been converted from an SQLVARCHARto aStringin the Java programming language, and it is assigned to theStringobjects.Note that although the method
getStringis recommended for retrieving the SQL typesCHARandVARCHAR, it is possible to retrieve any of the basic SQL types with it. (You cannot, however, retrieve the new SQL3 datatypes with it. We will discuss SQL3 types later in this tutorial.) Getting all values withgetStringcan be very useful, but it also has its limitations. For instance, if it is used to retrieve a numeric type,getStringconverts the numeric value to a JavaStringobject, and the value has to be converted back to a numeric type before it can be operated on as a number. In cases where the value is treated as a string anyway, there is no drawback. Further, if you want an application to retrieve values of any standard SQL type other than SQL3 types, use thegetStringmethod.Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet srs = stmt.executeQuery( "SELECT COF_NAME, PRICE FROM COFFEES"); while (srs.next()) { String name = srs.getString("COF_NAME"); float price = srs.getFloat("PRICE"); System.out.println(name + " " + price); }The output will look something like this:
Colombian 7.99 French_Roast 8.99 Espresso 9.99 Colombian_Decaf 8.99 French_Roast_Decaf 9.99You can process all of the rows is srs going backward, but to do this, the cursor must start out located after the last row. You can move the cursor explicitly to the position after the last row with the method
afterLast.Then the methodprevious()moves the cursor from the position after the last row to the last row, and then to the previous row with each iteration through the while loop. The loop ends when the cursor reaches the position before the first row, where the methodprevious()returns false .Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet srs = stmt.executeQuery("SELECT COF_NAME, PRICE FROM COFFEES"); srs.afterLast(); while (srs.previous()) { String name = srs.getString("COF_NAME"); float price = srs.getFloat("PRICE"); System.out.println(name + " " + price); }The printout will look similar to this:
French_Roast_Decaf 9.99 Colombian_Decaf 8.99 Espresso 9.99 French_Roast 8.99 Colombian 7.99As you can see, the printout for each has the same values, but the rows are in the opposite order.
The situation is similar with the method
getFloatexcept that it retrieves the value stored in the columnPRICE, which is an SQLFLOAT, and converts it to a Javafloatbefore assigning it to the variablen.JDBC offers two ways to identify the column from which a
getXXXmethod gets a value. One way is to give the column name, as was done in the example above. The second way is to give the column index (number of the column), with1signifying the first column,2, the second, and so on. Using the column number instead of the column name looks like this:String s = srs.getString(1); float n = srs.getFloat(2);The first line of code gets the value in the first column of the current row of
rs(columnCOF_NAME), converts it to a JavaStringobject, and assigns it tos. The second line of code gets the value stored in the second column of the current row ofrs, converts it to a Javafloat, and assigns it ton. Note that the column number refers to the column number in the result set, not in the original table.You can move the cursor to a particular row in a
ResultSetobject. The methodsfirst,last,beforeFirst, andafterLastmove the cursor to the row indicated in their names. The methodabsolutewill move the cursor to the row number indicated in the argument passed to it. If the number is positive, the cursor moves the given number from the beginning, so callingabsolute(1)puts the cursor on the first row. If the number is negative, the cursor moves the given number from the end, so callingabsolute(-1)puts the cursor on the last row. The following line of code moves the cursor to the fourth row ofsrs:srs.absolute(4);If
srshas 500 rows, the following line of code moves the cursor to row 497:srs.absolute(-4);Three methods move the cursor to a position relative to its current position. As you have seen, the method
nextmoves the cursor forward one row, and the methodpreviousmoves the cursor backward one row. With the methodrelative, you can specify how many rows to move from the current row and also the direction in which to move. A positive number moves the cursor forward the given number of rows; a negative number moves the cursor backward the given number of rows. For example, in the following code fragment, the cursor moves to the fourth row, then to the first row, and finally to the third row:srs.absolute(4); // cursor is on the fourth row . . . srs.relative(-3); // cursor is on the first row . . . srs.relative(2); // cursor is on the third rowThe method
getRowlets you check the number of the row where the cursor is positioned. For example, you can usegetRowto verify the current position of the cursor in the previous example as follows:srs.absolute(4); int rowNum = srs.getRow(); // rowNum should be 4 srs.relative(-3); int rowNum = srs.getRow(); // rowNum should be 1 srs.relative(2); int rowNum = srs.getRow(); // rowNum should be 3Four additional methods let you verify whether the cursor is at a particular position. The position is stated in their names:
isFirst,isLast,isBeforeFirst,isAfterLast. These methods all return abooleanand can therefore be used in a conditional statement. For example, the following code fragment tests to see whether the cursor is after the last row before invoking the methodpreviousin awhileloop. If the methodisAfterLastreturnsfalse, the cursor is not after the last row, so the methodafterLastis invoked. This guarantees that the cursor will be after the last row and that using the methodpreviousin thewhileloop will cover every row insrs.if (srs.isAfterLast() == false) { srs.afterLast(); } while (srs.previous()) { String name = srs.getString("COF_NAME"); float price = srs.getFloat("PRICE"); System.out.println(name + " " + price); }In summary, JDBC allows you to use either the column name or the column number as the argument to a
getXXXmethod. Using the column number is slightly more efficient, and there are some cases where the column number is required. In general, though, supplying the column name is essentially equivalent to supplying the column number.JDBC allows a lot of latitude as far as which
getXXXmethods you can use to retrieve the different SQL types. For example, the methodgetIntcan be used to retrieve any of the numeric or character types. The data it retrieves will be converted to anint; that is, if the SQL type isVARCHAR, JDBC will attempt to parse an integer out of theVARCHAR. The methodgetIntis recommended for retrieving only SQLINTEGERtypes, however, and it cannot be used for the SQL typesBINARY,VARBINARY,LONGVARBINARY,DATE,TIME, orTIMESTAMP.