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.

250<br />

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

analyzed, how many were modified, and a table indicating for each department, the total of<br />

bonuses and whether the bonuses were modified.<br />

Let’s now write the inline style implementation of this method. Edit the<br />

increaseEmployeeBonuses method in<br />

hrm.inProgress.pdqInlineStyle.HumanResourcesManager. We do not need to<br />

use any handlers, so we use this method in Listing 12.2.<br />

In determining how to specify the SQL stored procedure call, we need to choose how we<br />

will specify the parameter markers. Refer to Section 5.4, which describes parameter<br />

markers. We use variables of the type double to store the values of p_bonusFactor<br />

and p_bonusMaxSumForDept, so we must use either the ? or the ?n type. We choose<br />

the ? type. Because the SQL statement contains parameter markers for the OUT<br />

parameters, we must specify corresponding parameters in the varargs parameters. They<br />

are OUT parameters, so there is no input value. Therefore, we specify null.<br />

Add the code in Listing 12.5 to execute the stored procedure in the inline method style:<br />

// Execute the bonus_increase stored procedure.<br />

storedProcedureResult = data.call ("CALL bonus_increase (?, ?, ?, ?, ?,<br />

?)",<br />

bonusFactor, bonusMaxSumForDepartment, null, null, null, null);<br />

Listing 12.5 - Use an inline method to execute the stored procedure<br />

The first thing that the application needs to do after executing the stored procedure call is<br />

retrieve the OUT parameters from storedProcedureResult, since <strong>pureQuery</strong> will<br />

automatically close storedProcedureResult when the last query result is retrieved.<br />

Add the code in Listing 12.6, which retrieves the array of OUT and INOUT parameters, and<br />

assigns the new value of the parameters to variables.<br />

// Get the values of the OUT parameters from bonus_increase.<br />

// Do this before getting the query results, since getting the last<br />

// query result closes storedProcedureResult.<br />

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

String departmentsWithoutNewBonuses = (String) outputParameters[2];<br />

int countDepartmentsViewed = (Integer) outputParameters[3];<br />

int countDepartmentsBonusChanged = (Integer) outputParameters[4];<br />

String errorMessage = (String) outputParameters[5];<br />

Listing 12.6 - Get the updated values of the OUT parameters from the stored<br />

procedure<br />

After we have retrieved the OUT parameters, we access the query result. The query result<br />

contains a row for each employee in the company. At a large company, this could be more<br />

data than we would want to have in memory at any point in time, so we use a<br />

java.util.Iterator object to retrieve one row at a time from the database. Add

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

Saved successfully!

Ooh no, something went wrong!