16.10.2015 Views

Getting Startedwith pureQuery

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 12 – Stored Procedures 251<br />

the code in Listing 12.7, which creates an Iterator object containing<br />

the query result.<br />

// Get the query result, which contains the bonuses for all the<br />

// employees.<br />

// Do this after getting the value of the OUT parameters from<br />

// bonus_increase, since getting the last query result closes<br />

// storedProcedureResult.<br />

employees = storedProcedureResult.getIterator (EmployeeBean.class);<br />

Listing 12.7 - Get the query result from the stored procedure call<br />

<strong>pureQuery</strong> automatically closes the underlying database resources when the application<br />

gets the last query result and when the application gets the last row of the<br />

Iterator object, so in most cases our application will not need to<br />

close employees and storedProcedureResult. However, an exception could occur<br />

before the application retrieves the query result or processes all of the rows, so it would be<br />

good for our application to close employees and storedProcedureResult explicitly.<br />

Add the code in Listing 12.8 to the finally block.<br />

finally {<br />

// Close employees. This frees the associated database resources.<br />

if (null != employees) ((ResultIterator) employees).close<br />

();<br />

// Close storedProcedureResult. This frees the associated database<br />

// resources.<br />

if (null != storedProcedureResult) storedProcedureResult.close ();<br />

}<br />

Listing 12.8 - Close employees and storedProcedureResult<br />

Finally, test the application by logging in with the employee number 000010 and selecting<br />

the option 5 on the main menu.<br />

12.4 Executing SQL stored procedure calls in the annotated-method<br />

style<br />

Annotated methods that execute SQL stored procedure calls must have the @Call<br />

annotation. The return type of the method can be one of the following:<br />

• A com.ibm.pdq.runtime.StoredProcedureResult object<br />

• void<br />

• One of several of the return types allowed with the @Select<br />

annotation<br />

• An object of a specific type, constructed by a<br />

com.ibm.pdq.runtime.handlers.CallHandlerWithParameters<br />

object

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

Saved successfully!

Ooh no, something went wrong!