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 11 – Annotated-Method Programming Style 221<br />

and create the Iterator object of the employees. This is shown in<br />

Listing 11.10.<br />

// Search the database for all of the employees in the company<br />

// and create an Iterator object to retrieve them.<br />

employees = data.selectAllEmployees ();<br />

Listing 11.10 - Modify the displayAllEmployees method to use the annotated<br />

method<br />

11.4.2.3 Close the Iterator object to free the associated database<br />

resources<br />

The user of Human Resources Manager might not view all of the employees. Therefore,<br />

we need to close employees explicitly to free the database resources. Add code to close<br />

employees.<br />

finally {<br />

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

if (null != employees)<br />

((ResultIterator) employees).close ();<br />

}<br />

Listing 11.11 - Add code to the displayAllEmployees method that closes the<br />

Iterator when the application no longer needs it<br />

11.4.2.3.1 Test the "Company Employees" screen<br />

Save and test your application. As before, supply 000030 when the Human Resources<br />

Manager asks for your employee number. On the main menu, select option 4 to display all<br />

the employees in the company. You will then be able to see the employees, one page at a<br />

time.<br />

11.5 Executing SQL statements that have parameters<br />

It is very easy to use parameters in SQL statements when you use the <strong>pureQuery</strong><br />

annotated method style. The four types of parameter markers that are available in the<br />

inline style are also available in the annotated method style. With each of these types,<br />

<strong>pureQuery</strong> automatically populates the SQL parameters with the information that you<br />

provide. When you use multiple parameter markers in an SQL statement, all of the<br />

parameter markers must be of the same type.<br />

In the <strong>pureQuery</strong> method style, you specify the objects that <strong>pureQuery</strong> should use to<br />

populate the parameters of your SQL statement as the parameters of the annotated<br />

methods when you declare the methods.<br />

Table 11.1 shows the four types of parameter markers. References to Map indicate the<br />

class java.util.Map. Listing 11.12 shows the definitions of the objects that the “Sample<br />

usage” column references.

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

Saved successfully!

Ooh no, something went wrong!