4.2.4 Retrieving Results
Based on the different Oracle statements, three execution methods can be used to run an associated Oracle statement. As we discussed in Section 4.2.3.1, each execution method performs different data actions:
- The executeQuery() method is used to run a data query, and the expected returned result is a result set stored in a ResultSet object.
- The executeUpdate() method is used to perform a insert, update or delete data action, and the returned result should be an integer that equals the number of rows that have been affected by running this data manipulation.
- The execute() method can be used in either way, but this method never returns any result, and you need to use special methods to pick up the run results.
To pick up the run results for different methods, the following rules should be observed:
1) For the executeQuery() method, the getResultSet() method defined in the Statement interface should be used since the run result is a result set stored in a
ResultSet object.
2) For the executeUpdate() method, the getUpdateCount() method defined in the Statement interface should be used since the run result is an integer that equals the number of rows that have been affected.
3) For the execute() method, since this method can handle both ResultSets and inte-gers, and it never returns any result, you need to use special methods to retrieve the run result for the execution of this method.
Relatively speaking, for the first two methods, it is relatively easy to pick up the run result, since the result is known and definite. The challenge is the third method, execute(), since the result of execution of this method can be either a ResultSet or an integer. Another challenge is that this method can be used where the Oracle statement to be executed is not known at compile time or there is a possibility of multiple results being returned by a stored procedure. Unlike the first two methods, the execute() method never returns any result, and you must use either the getRe-sultSet() or getUpdateCount() method to retrieve the run results.
To distinguish what kind of result is returned, we can use the method we discussed in the last sec-tion. To handle multiple results, we need to use the getMoreResults() method defined in the Statement interface (refer to Table 4.4). When executing this method, True will be returned if a ResultSet object is returned. If the result retrieved is an integer, then the getMoreResults() method returns False. The confusing issue is that this method will also return False if no result is received. In order to solve this confusion, you must use the getUpdateCount() method to test the possible results. Table 4.8 shows a full picture of the associated test conditions and possible test results.
It is easy to get the result of the execution of the execute() method, since only an integer is returned as the result for this method. However, the result of the execution of the execute-Query() and execute() methods needs more work, since a ResultSet object that contains a tabular set is returned. We will concentrate on the methods used to retrieve and process the actual data contained in the ResultSet object. First let’s have a closer look at the ResultSet interface.