16.10.2015 Views

Getting Startedwith pureQuery

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 12 – Stored Procedures 245<br />

corresponds to one of the parameters to the stored procedure call, and the order of the<br />

parameters in the array is the same as the order of the parameters in the stored procedure<br />

call. <strong>pureQuery</strong> populates the items in the array that correspond to OUT and INOUT<br />

parameters with the updated values of those parameters. <strong>pureQuery</strong> does not return the<br />

IN parameters; instead, it populates the items that correspond to IN parameters with Java<br />

null as a placeholder.<br />

If you want your application to call getOutputParms(), you must have it do so before the<br />

StoredProcedureResult object has been closed. <strong>pureQuery</strong> automatically closes the<br />

object when you retrieve the last query result. Therefore, if you want to get updated values<br />

of OUT and INOUT parameters, have your application call getOutputParms() before it<br />

retrieves any of the query results.<br />

As an example, suppose that the SAMPLE database contains a procedure<br />

EXAMPLE_PROCEDURE, as defined in Listing 12.1:<br />

CREATE PROCEDURE SAMPLE.EXAMPLE_PROCEDURE<br />

(IN parameter1 CHAR(8),<br />

OUT parameter2 SMALLINT,<br />

OUT parameter3 BIGINT)<br />

LANGUAGE SQL<br />

BEGIN<br />

⁄* do something here *⁄<br />

END<br />

Listing 12.1 - The example stored procedure EXAMPLE_PROCEDURE<br />

Assume that you use <strong>pureQuery</strong> to execute the procedure and create a<br />

StoredProcedureResult object named storedProcedureResult containing the<br />

results. You could store the output parameters in an array named outputParameters<br />

like this:<br />

Object[] outputParameters = storedProcedureResult.getOutputParms();<br />

Observe that this stored procedure contains three parameters. Therefore, the array<br />

outputParameters would have a length of three. The first parameter to the stored<br />

procedure, parameter1 is an IN parameter, so the value of outputParameters[0]<br />

would be null. The second parameter is an OUT parameters, so<br />

outputParameters[1] would contain the updated value of parameter2. Similarly, the<br />

third parameter is an OUT parameter, so outputParameters[2] would contain the<br />

updated value of parameter3.<br />

12.2.2 <strong>Getting</strong> the query results of the stored procedure<br />

Stored procedures can have one or more query results. When <strong>pureQuery</strong> creates a<br />

StoredProcedureResult object, it does not retrieve any query results from the<br />

database. The StoredProcedureResult interface contains several getXXX methods,<br />

each of which retrieves the next (or first) query result from the database and returns it in a

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!