4.2.4.2 Getting and Processing the ResultSet Object
When an Oracle data query is executed, the returned result is stored in a ResultSet object, and this ResultSet object can be created by one of the following two methods:
- The executeQuery() method
- The getResultSet() method
When an executeQuery() method is executed, the result of the queried data is stored in a ResultSet object and returned. However, when an execute() method is used to retrieve a data query result, it will not return any result directly; instead, you need to use the getResultSet() method to create a ResultSet to pick up the returned result.
Once the ResultSet object is created by using either method, an appropriate getXXX() method defined in the ResultSet interface can be used to access and retrieve data. Since the data is in a tabular format, any data can be retrieved by using the column and row ordinals. Two different ways can be used to select and access each column and row in a ResultSet object:
1) Using either the column index or column name to select the desired column
2) Using the cursor that points to the current row to select a desired row
In order to scan the entire Table in a ResultSet object, you can use the next() method defined in the ResultSet interface to move the cursor row by row until the last record. To pick up a specified column from a given row, you can use an appropriate getXXX() method defined in the ResultSet interface with a column index or column name as the argument.
Let’s have a closer look at accessing and processing each row and column from a ResultSet object with a little more discussion in the following sections.
4.2.4.2.1 Fetching by Row
In a ResultSet object, a cursor is used as a pointer to point to each row, and each row of data must be processed in the order in which it can be returned. At the beginning time, after an execu-tion method is executed and a ResultSet object is returned, the cursor points to the initial row, which is an empty row (refer to Figure 4.15). To move the cursor to point to the first row of data, as we mentioned, the next() method can be used. Then an appropriate getXXX() method can be used to pick up the desired column from the current row based on the column index or the column name as the argument of that method. Figure 4.15 shows a structure of a ResultSet object with a row pointer positioning diagram.