4.2.3.4.5 Executing the CallableStatement Object
To run a CallableStatement object, three execution methods can be used: executeQuery(), executeUpdate() and execute(). As we discussed in Section 4.2.3.1, the executeQuery() method can return a ResultSet object that contains the run or query results, and the executeUp-date() method can return an integer to indicate the number of rows that have been inserted, updated or deleted against the target database. However, the execute() method cannot return any run result itself, and you need to use the associated getXXX() methods to pick up the query or run result. Another important point of using the execute() method is that it can handle an unknown result with an unde-fined data type. Refer to Section 4.2.3.5 to get more detailed information about the execute() method.
An example of using the execute() method to run the CallableStatement object is shown in Figure 4.10.
After finishing building the query string, creating the CallableStatement object, and set-ting and registering input and output parameters, the execute() method is called to execute the-CallableStatement object to perform stored procedure processing.
Before we can continue withhow to retrieve the run result from the execution of a Statement, PreparedStatement or CallableStatement object, we need to have a closer look at three execution methods.
4.2.3.5 More about the Execution Methods
The three statement objects are used to perform different data actions against the target database, and the type of statement object to be used is determined by the parameters of the Oracle state-ments. To make it simple, the following strategy should be adopted for the given situation:
- For static statements without needing to pass any parameter into the database, a Statement object can be used.
- For dynamic statements with some input parameters that need to be passed into the target database, a PreparedStatement object should be used.
- For stored procedures with both input and output parameters that need to be passed into the target database, a CallableStatement object can be used.
Similarly to statement objects, the execute method to be used is determined by the expected output of the Oracle statement. There are three types of output that can be expected from an Oracle statement:
- A ResultSet containing data in tabular format with rows and columns.
- An integer indicating the number of rows affected by the Oracle statements.
- A combination of a ResultSet and an integer.
Each of these output types requires its own special output handling. Accordingly, three execute methods, executeQuery(), executeUpdate() and execute(), can be used for each type of statement object.