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 13 – Handlers 275<br />

package hrm.inProgress;<br />

import java.sql.PreparedStatement;<br />

import java.sql.SQLException;<br />

import com.ibm.pdq.runtime.handlers.ParameterHandler;<br />

public class SimpleSelectEmployeeParameterHandler implements<br />

ParameterHandler<br />

{<br />

public void handleParameters (PreparedStatement stmt, Object...<br />

parameters)<br />

throws SQLException<br />

{<br />

System.out.println ("Using " +<br />

SimpleSelectEmployeeParameterHandler.class.getCanonicalName () +<br />

"!");<br />

String employeeNumber = (String) parameters[0];<br />

stmt.setString (1, employeeNumber);<br />

}<br />

}<br />

Listing 13.21 - The implementation of SimpleSelectEmployeeParameterHandler<br />

Now, we need to specify SimpleSelectEmployeeParameterHandler for the<br />

selectEmployee method in<br />

hrm.inProgress.pdqMethodStyleWithHandlers.HumanResourcesData.<br />

Because the handler has an implicit public no-argument constructor, we can use the<br />

@Handler annotation to specify the handler. Add the annotation to your method, providing<br />

the class of the handler.<br />

@Select(sql = "SELECT * FROM EMPLOYEE WHERE EMPNO = ?")<br />

@Handler(parameterHandler = SimpleSelectEmployeeParameterHandler.class)<br />

EmployeeBean selectEmployee (String employeeNumber);<br />

Listing 13.22 - The selectEmployee method with<br />

SimpleSelectEmployeeParameterHandler<br />

Build the project to regenerate the implementation class and test the application. Look for<br />

the message that SimpleSelectEmployeeParameterHandler prints.<br />

13.5 Summary<br />

<strong>pureQuery</strong> provides handlers through which you can customize how <strong>pureQuery</strong> sets the<br />

values of SQL parameters and how it returns query results. Implementations of the<br />

com.ibm.pdq.runtime.handlers.ParameterHandler interface customize how the<br />

values of parameters are set. Implementations of the<br />

com.ibm.pdq.runtime.handlers.ResultHandler interface customize the

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

Saved successfully!

Ooh no, something went wrong!