This Java example (click here to open the Java code) shows how simple it is to setup and execute an XQuery using DataDirect XQuery™ and its XQJ interface. The example references a 201 ACORD XML message used in Updating a Relational Database, but it can easily be modified/parameterized to execute other examples described in XQuery Operation Examples for an ACORD Insurance Application.
As you can see the core of the Java code is straightforward:
public void run() throws Exception {
// configure datasource
DDXQDataSource ds = new DDXQDataSource();
ds.setBaseUri(Acord201.queryBaseDir);
ds.setJdbcUrl(connectionUrl);
// connect
XQConnection c = ds.getConnection();
// create expression
XQExpression expr = c.createExpression();
// bind input document
String uri = new File(Acord201.queryBaseDir + Acord201.inputDoc).toURI().toString();
SAXSource request = Acord201.getSAXSource(uri);
expr.bindDocument(new QName("request"), request);
// execute XQuery
XQSequence reply = expr.executeQuery(new FileInputStream(new File(Acord201.queryBaseDir + Acord201.inputQuery)));
// dump reply to stdout
Acord201.dumpSequence(reply);
expr.close();
c.close();
}
The example code dumps the XQuery result to stdout, but that can be changed to suit your needs (creating a file, streaming the result to another XQuery processing, creating an XML DOM representation of the result, or feeding the result to an XSL-FO processor, for example).
![]()
Go to Using DataDirect XQuery™ and XQJ with EJB to learn how to use DataDirect XQuery™ and XQJ to create a Message Enterprise Java Bean.
![]()