Samples Tutorial

     Previous  Next    Open TOC in new window    View as PDF - New Window  Get Adobe Reader - New Window
Content starts here

Running Ad Hoc Queries

Sometimes it is necessary to execute a query on functions associated with an application that is already deployed. Rather than take the application offline to create a new query, ALDSP provides the PreparedExpression class, which lets you create and run ad hoc queries on deployed applications.

 


Objectives

After completing this lesson, you will be able to:

 


Overview

ALDSP includes a PreparedExpression class that lets you build an ad hoc query using remote data sources, and then execute it using the Mediator API or ALDSP Control. Using the methods within the PreparedExpression class, you can build queries on top of existing XDS functions belonging to applications already deployed on an active local or remote server domain.

The process for running an ad hoc query is as follows:

  1. Create a StringBuffer to hold the query.
  2. Create an instance of the PreparedExpression class, using the prepareExpression method.
  3. Create parameters for the ad hoc query, using the bind<DataType> methods.
  4. Submit the query and review the results, using the Mediator API or ALDSP Control.

 


21.1 Creating an Instance of the PreparedExpression Class

The first steps in creating an ad hoc query are to instantiate a StringBuffer and the PreparedExpression class. For the latter instance, you use the prepareExpression method of the DataServiceFactory class, which accepts three parameters:

For example:

PreparedExpression pe = DataServiceFactory.prepareExpression(
getInitialContext(), 
"Evaluation",  
xquery.toString()
     );

Objectives

In this exercise, you will:

Instructions

  1. Create a new Java project in the Evaluation application, and name it AdHocClient.
  2. Create a new Java class in the AdHocClient project, and name it AdHocQuery.
  3. Open AdHocQuery.java.
  4. Import the following Java classes:
  5. import com.bea.ld.dsmediator.client.DataServiceFactory;
    import com.bea.ld.dsmediator.client.PreparedExpression;
    import com.bea.xml.XmlObject;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    import javax.xml.namespace.QName;
    import weblogic.jndi.Environment;
    Note: You can also import the necessary Java classes by first adding the code specified below, and then pressing Alt + Enter.
  6. Specify the initial context for the query, by adding the following code after the first curly brace:
  7. public static InitialContext getInitialContext() throws NamingException {
       		Environment env = new Environment();
        		env.setProviderUrl("t3://localhost:7001");
       env.setInitialContextFactory("weblogic.jndi.WLInitialContextFactory");
        	env.setSecurityPrincipal("weblogic");
        	env.setSecurityCredentials("weblogic");
        	return new InitialContext(env.getInitialContext().getEnvironment());
      }
  8. Add the main argument, by adding the following code after the initial context:
  9. public static void main (String args[]) {
    System.out.println("========== Ad Hoc Client =============");
    		try {
    			} catch (Exception e) {
    			e.printStackTrace();
    		}
      }
  10. Build a StringBuffer instance to hold your query. For example, add the following code after the line:
  11. try {:
    StringBuffer xquery = new StringBuffer();
         
    xquery.append("declare variable $p_firstname as xs:string external; \n");
    xquery.append("declare variable $p_lastname as xs:string external;  \n");
            
    xquery.append("declare namespace ns1=\"ld:DataServices/MyQueries/XQueries\"; \n");
    xquery.append("declare namespace ns0=\"ld:DataServices/CustomerDB/CUSTOMER\"; \n\n");
    xquery.append("<ns1:RESULTS>                                    \n");
    xquery.append("{                                                \n");
    xquery.append("    for $customer in ns0:CUSTOMER()              \n");
    xquery.append("    where ($customer/FIRST_NAME eq $p_firstname  \n");
    xquery.append("       and $customer/LAST_NAME eq $p_lastname)   \n");
    xquery.append("    return                                       \n");
    xquery.append("      $customer                                  \n");
    xquery.append(" }                                               \n");
    xquery.append("</ns1:RESULTS>                                   \n");
  12. Use the prepareExpression method of the Mediator API's DataServiceFactory class to create an instance of the PreparedExpression class, by adding the following code:
  13. PreparedExpression pe = DataServiceFactory.prepareExpression(
    getInitialContext(), "Evaluation",  xquery.toString());

 


21.2 Defining Ad Hoc Query Parameters

After you create an instance of the PreparedExpression class, you need to specify the parameters that will be passed when the ad hoc query is submitted. To pass parameters, you use one or more bind<DataType> methods, such as bindString and bindInt.

Objectives

In this exercise, you will:

Instructions

  1. Pass parameters by using the bindString method of the PreparedExpression instance. For example, add the following code to the AdHocQuery.java file:
  2. pe.bindString(new QName("p_firstname"), "Jack");
    pe.bindString(new QName("p_lastname"), "Black");
  3. Invoke the executeQuery method to return the query results in an XmlObject.
  4. XmlObject obj = pe.executeQuery();
  5. Enter the code necessary to return the XmlObject and display the XML. For example:
  6. System.out.println(obj.toString());

 


21.3 Testing the Ad Hoc Query

You are now ready to test the ad hoc query, which is set to return information for Jack Black.

Objectives

In this exercise, you will:

Instructions

  1. Build the AdHocClient project.
  2. In the AdHocQuery.java application, click the Start icon (or press Ctrl + F5).
  3. Confirm that you can retrieve customer profile information for Jack Black.
  4. Figure 21-1 Results of Ad-Hoc Query () Function


    Results of Ad-Hoc Query () Function

Code Reference for an Ad Hoc Query

import com.bea.ld.dsmediator.client.DataServiceFactory;
   import com.bea.ld.dsmediator.client.PreparedExpression;
   import com.bea.xml.XmlObject;
   import javax.naming.InitialContext;
   import javax.naming.NamingException;
   import javax.xml.namespace.QName;
   import weblogic.jndi.Environment;
   public class AdHocQuery 
   {
   public static InitialContext getInitialContext() throws NamingException {
   		Environment env = new Environment();
    		env.setProviderUrl("t3://localhost:7001");
   		env.setInitialContextFactory("weblogic.jndi.WLInitialContextFactory");
    		env.setSecurityPrincipal("weblogic");
    		env.setSecurityCredentials("weblogic");
    		return new InitialContext(env.getInitialContext().getEnvironment());
   }
     public static void main (String args[]) {
		System.out.println("==================== Ad Hoc Client ====================");
		try {
			StringBuffer xquery = new StringBuffer();
     
xquery.append("declare variable $p_firstname as xs:string external; \n");
xquery.append("declare variable $p_lastname as xs:string external;  \n");
        
xquery.append("declare namespace ns1=\"ld:DataServices/MyQueries/XQueries\"; \n");
xquery.append("declare namespace ns0=\"ld:DataServices/CustomerDB/CUSTOMER\"; \n\n");
xquery.append("<ns1:RESULTS>                                    \n");
xquery.append("{                                                \n");
xquery.append("    for $customer in ns0:CUSTOMER()              \n");
xquery.append("    where ($customer/FIRST_NAME eq $p_firstname  \n");
xquery.append("       and $customer/LAST_NAME eq $p_lastname)   \n");
xquery.append("    return                                       \n");
xquery.append("      $customer                                  \n");
xquery.append(" }                                               \n");
xquery.append("</ns1:RESULTS>                                   \n");
PreparedExpression pe = DataServiceFactory.prepareExpression(getInitialContext(), "Evaluation",  xquery.toString());             
pe.bindString(new QName("p_firstname"), "Jack");
pe.bindString(new QName("p_lastname"), "Black");
XmlObject results = pe.executeQuery();
System.out.println(results);
} catch (Exception e) {
			e.printStackTrace();
		}
  }
}

 


Lesson Summary

In this lesson, you learned how to:


  Back to Top       Previous  Next