Using the ResultSet Object 79 Determining the Cursor (Aol web hosting)

Using the ResultSet Object 79 Determining the Cursor Position As we mentioned earlier, when a ResultSet is first instantiated, the internal cursor is positioned just before the first row in the set. You have four methods for monitoring where the cursor is in the set. To determine if it is sitting before the first row, use the method isBeforeFirst(); for example: ResultSet rs = statement.executeQuery(”SELECT * FROM acc_acc”); boolean whereIsIt = rs.isBeforeFirst() The isBeforeFirst() method returns a value of true if the internal cursor is sitting before the first row. In our code example, the value returned will be true. The complement to this method is isAfterLast(). When the cursor has been moved beyond all of the rows in the set, the isAfterLast() method returns a value of true. We can also tell whether the internal cursor has been moved to either the first or the last row of the object. The isFirst() method will return true if the cursor is sitting at the first row, and isLast() returns true if the cursor is sitting on the last row. Finally, you can use the getRow() method to return the current row number from the ResultSet. If you execute the getRow() method just after getting the ResultSet from the executeQuery() method, the value returned will be 0. Thus, the first actual data row in a ResultSet has a value of 1. This is something to remember when using the methods in the next section to move around the object. Moving the Cursor Once you know where the cursor is currently pointing within the set, you can move it anywhere you like. First, let s look at two methods that allow you to move to a specific location within the ResultSet. The first method is based on counting from an absolute position from either the beginning or the end of the rows: boolean absolute(int rows) The absolute() method moves the internal cursor to a specific row in the ResultSet. Thus, the method called rs.absolute(2) moves to the second row in the object. If a value is entered that is outside the bounds of the row count in the ResultSet, a SQLException exception will be thrown. To the method, a positive value indicates that it should count from the beginning of the rows; a negative value indicates that it should count from the end of the rows. The second method counts based on the current cursor position: boolean relative(int rows)
Note: If you are looking for cheapest and affordable webspace to host and run your servlet application check Astra j2ee hosting services

Comments are closed.