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.

190<br />

<strong>Getting</strong> Started with <strong>pureQuery</strong><br />

10.3.3 In practice: Execute a simple SELECT statement that uses cursor<br />

attributes -- Complete the “Company Employees” screen<br />

The Company Employees screen displays all of the employees in the company in pages of<br />

10 employees each. It allows the user to request additional information about any<br />

particular employee on the screen. The displayAllEmployees (Data data) method<br />

creates this screen. Add the <strong>pureQuery</strong> code to it to get the employee information from the<br />

database.<br />

10.3.3.1 Use <strong>pureQuery</strong> to create an Iterator object that<br />

contains the information about the employees<br />

In order to get information about all the employees in the company, we need to execute a<br />

query against the database. We will use the query shown in Listing 10.5, which will select<br />

all the employees and sort them by their names.<br />

SELECT * FROM EMPLOYEE ORDER BY LASTNAME, FIRSTNME, MIDINIT<br />

Listing 10.5 - An SQL query for selecting all the employees in the company<br />

Next, we need to determine which Data method to use. Since we need several pieces of<br />

information about each employee, it would be convenient to retrieve each row as an<br />

instance of EmployeeBean. We therefore need to use a queryXXX method that has a<br />

Class returnClass parameter. We will be selecting all the employees in the<br />

database, but we would prefer not to have to store all of the company's employees in the<br />

application’s memory at once, since that could be quite a lot of memory. As a result, an<br />

Iterator is the most logical object to use to for returning the rows. Therefore, we will<br />

use a queryIterator method that has a Class returnClass parameter.<br />

While users are viewing the list of employees, they can request to view additional<br />

information about an employee and then return to the list. Therefore, we need to set the<br />

cursor holdability to “hold cursors over commit.” As a result, we use a queryIterator<br />

method that has parameters for cursor attributes.<br />

The code to execute our SQL query against the database and create the Iterator is simple.<br />

All we have to do is invoke the queryIterator method, specifying the SQL statement<br />

and EmployeeBean.class. This returns an Iterator object, where<br />

each item is an instance of EmployeeBean. Modify the application to contain this code.<br />

Remember to look for a comment that says TODO: Add <strong>pureQuery</strong> code here. so that<br />

you will know where you need to add the new code. Listing 10.6 shows the updated code.<br />

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

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

// Use "hold cursors over commit" so that if the user goes to another<br />

// screen and comes back, we can continue displaying employees.<br />

employees = data.queryIterator (ResultSet.TYPE_FORWARD_ONLY,<br />

ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT,

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

Saved successfully!

Ooh no, something went wrong!