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.

290<br />

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

Figure 15.2 - Giving control to the data access API<br />

Examples of Java-XML mapping frameworks are: Castor, JAXB, JiBX, XMLBeans and the<br />

integrated Java-XML mapping support in Java 2 SE v6. In order to avoid extra setup steps,<br />

the examples in this chapter use the mapping support integrated into Java2 SE v6, which is<br />

built on top of JAXB.<br />

In order to map from XML to Java in the data access API, you need to implement a custom<br />

RowHandler that uses JAXB as shown in Listing 15.3:<br />

public class JAXBHandler implements RowHandler {<br />

public Employee handle(ResultSet rs, Object arg1) throws SQLException<br />

{<br />

Employee emp = new Employee();<br />

JAXBContext jContext;<br />

try {<br />

jContext =<br />

JAXBContext.newInstance("pdq.purexml.approach2");<br />

Unmarshaller unmarshaller = jContext.createUnmarshaller();<br />

emp =<br />

(Employee)unmarshaller.unmarshal(rs.getBinaryStream(2));<br />

} catch (JAXBException e) {<br />

e.printStackTrace();<br />

}<br />

emp.setId(rs.getString(1));<br />

return emp;<br />

}<br />

}<br />

Listing 15.3 – Mapping from XML to Java using JAXB

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

Saved successfully!

Ooh no, something went wrong!