SoftXML
           hdmi splitter | XML Press
Suggest Your Service | Add your website            SoftXMLLib | SoftXMLEcartJS
Archive

SoftXML Archives


 
Sign Up for webmaster bulletin and receive a Free Email Marketing Success Strategies e-book.
Name:
Email:

Hosted Exchange 2010





Learn xqj                     Go Back To tutorials List

Preparing XQuery statements

When an XQuery is executed, the query generally is parsed and optimized before it is run. To avoid incurring this overhead each time the XQuery is used, XQJ allows queries to be prepared once and executed multiple times. Here's the code to create a prepared query-only the last line differs from the code used to create a query in our first example.

dataSource = new DDXQDataSource(new FileInputStream(configFile));    
connection = dataSource.getConnection();

FileReader fileReader = new 
        FileReader("xquerySourceFile.xq");
            
preparedExpression = connection.prepareExpression(
        fileReader);

Once the query is prepared, it is executed using the executeQuery() call.

XQSequence xqSequence = preparedExpression.executeQuery();
System.out.println(xqSequence.getSequenceAsString());

Of course, queries often take parameters, and these parameters may need to be changed between executions. For example, we might want to prepare a query that selects items that match a particular value and change that value each time the query is executed. Suppose we want to use a query that returns the stock holdings for a given user. The user changes each time this XQuery is run.

declare variable $l as xs:string external;
       
collection("HOLDINGS")/HOLDINGS[USERID=$l]

The value of $l is set using XQJ. Let's run this twice, each time for different users.

preparedExpression.bindString(new QName("l"), "Jonathan");

xqSequence = preparedExpression.executeQuery();

System.out.println("\n\nHoldings for Jonathan:\n\n");

System.out.println(xqSequence.getSequenceAsString());

preparedExpression.bindString(new QName("l"), "Minollo");

xqSequence = preparedExpression.executeQuery();

System.out.println("\n\nHoldings for Minollo:\n\n");

System.out.println(xqSequence.getSequenceAsString());
Get RSS for xqj


™SoftXML.   Privacy Statement  |  Link to Us  |  Articles Archive  |  Tutorials Archive  |  Portfolio Archive  |  Products Archive  |  XML Books
^Top