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.

266<br />

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

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

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

();<br />

}<br />

Listing 13.4 - Close employees. This frees the database resources if an exception<br />

occurs before the application retrieves all of the rows.<br />

Finally, test the application.<br />

13.3 Specifying handlers in the annotated method style<br />

When you use the annotated-method style, you can specify handlers for customizing the<br />

input and output of your annotated methods.<br />

When you specify handlers for annotated methods, make sure that the return type of your<br />

method is compatible with your handler. The Java compiler is not able to recognize if the<br />

type of a handler is not compatible with the return type of the method. If the type is not<br />

compatible, you will not know there is a problem until you generate an implementation<br />

class for your interface and observe that it does not compile.<br />

The annotated-method style provides two ways of specifying handlers. You can specify<br />

handlers either in the com.ibm.pdq.annotation.Handler annotation, or as<br />

parameters to the annotated methods. For an annotated method, you can specify at most<br />

one handler that customizes output for each annotated method. You can also specify the<br />

ParameterHandler, which customizes input, if you want.<br />

13.3.1 Specifying handlers by using the @Handler annotation<br />

<strong>pureQuery</strong> provides the annotation com.ibm.pdq.annotation.Handler, which you<br />

can add to annotated methods to indicate the handlers that you want them to use. The<br />

annotation contains an element for each type of handler. To specify that you want to use a<br />

specific handler, set the appropriate annotation element to the class of the handler that you<br />

want to use.<br />

Listing 13.5 shows an example of specifying the JSONResultHandler for an annotated<br />

method. Note that the return type of the annotated method is String because the<br />

handle method of the JSONResultHandler has a return type of String.<br />

@Handler(resultHandler=JSONResultHandler.class)<br />

@Select(sql = "SELECT * FROM EMPLOYEE ORDER BY LASTNAME, FIRSTNME,<br />

MIDINIT")<br />

String selectAllEmployees ();<br />

Listing 13.5 - An example of using the @Handler annotation to specify a handler<br />

When you specify a handler with the @Handler annotation, the handler must have a public<br />

no-argument constructor. <strong>pureQuery</strong> creates an instance of the handler by using that

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

Saved successfully!

Ooh no, something went wrong!