Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

IBM WebSphere Developer Technical Journal: XML and WebSphere Studio Application Developer

Part 3 -- SQL and XML Together

Joan Haggarty, Staff Software Developer, IBM Toronto Lab
Joan Haggarty is a Staff Software Developer at the IBM Toronto Lab. She is a member of the XML tools team for WebSphere Studio Application Developer, focusing specifically on the SQL Builder. You can contact Joan at joan@ca.ibm.com.

Summary:  This is Part 3 of a series that focuses on the XML tools provided with WebSphere Studio Application Developer. Part 3 focuses on the XML tools provided with Application Developer, and discusses the features available to incorporate data access and XML in your application.

Date:  05 Feb 2002
Level:  Intermediate

Activity:  3041 views
Comments:  

Introduction

IBM ® WebSphere® Studio Application Developer is an application development product that supports the building of a large spectrum of applications using different technologies, such as JSPTM, servlets, HTML, XML, Web services, databases, and EJBs. In particular, Application Developer provides tight integration between XML and relational data.

Application Developer supports all of the databases that WebSphere Application Server supports, including DB2®, Oracle, Sybase, and Microsoft® SQL Server.

This is Part 3 of a series of articles focusing on the XML tools provided with Application Developer. Part 3 discusses the features available to incorporate data access and XML in your application. Using the SQL query that was created in Part 2, I will now demonstrate how to do the following using Application Developer.

In the section, "Generate XML and related files from an SQL statement with the XML from SQL Query wizard," you will see how the SQL to XML generation tool that is included with Application Developer allows you to easily generate XML, DTD, XSD, XSL, and query template (XST) files from SQL queries.

In the section, "Deploy the sample servlet to WebSphere Application Server from a Web project," I describe the dynamic use of SQL queries to generate XML within a servlet and show how to deploy the servlet to WebSphere Application Server within Application Developer. An SQLtoXML JavaTM class library is shipped with Application Developer; you can use this class library in your Java application or servlet to execute your SQL statements and construct the results as XML. The generated XST file is used to execute the SQL statement at run time in WebSphere Application Server using the XMLIntegratorServlet sample. You can modify this servlet for use in your own application.

We recommend that you read the other articles in this series:

  • Part 1 Learn how to use Application Developer to develop XML Schema.
  • Part 2 Learn how to create an SQL query using Application Developer's SQL Builder.
  • Part 3 Learn about the Application Developer features available to incorporate data access and XML in your application.
  • Part 4 Learn how to use the XML Editor, a visual tool for creating and editing XML documents.
  • Part 5 Learn how to use the RDB to XML Mapping Editor to create DAD files for use with DB2 XML Extender.
  • Part 6 Learn how to use the XML Schema Editor and the XML Editor together to develop XML applications that make use of XML namespaces.

Scenario and sample query

The sample application we will be working with is a retail video store application. The application allows the store employees to create, maintain and query customers, inventory and rental information. The store database holds data related to the store customers, inventory, video rentals and employees. The application requires a query that allows the store employees to look up the video titles that a particular customer has rented and what day of the week they are due. The query takes a customer name as a parameter at run time. The query looks as follows in the SQL Builder:


Figure 1. The query as it appears in the SQL Builder
Screen capture of the query as it appears in the SQL Builder

Generate XML from an SQL query

In this section, I will demonstrate how to generate XML and related files based on the customer rentals query that we have just created. The XML from SQL Query wizard is used to accomplish this:

  1. Right-click on the customerRentals statement in the Data view and select Generate new XML from the pop-up menu. The XML From SQL wizard opens as shown in Figure 2 below.
    Figure 2. The XML from SQL Query wizard
    Screen capture of the XML from SQL Query wizard
  2. Select to show the result table columns as elements in the XML that is generated. In addition to the XML document, we will generate a DTD definition for it. As you can see in Figure 2 above, the schema definition generation is optional and can be an XML Schema instead of a DTD if you choose.
  3. Next, select to generate a query template file (XST script). This will later be used by the video store Java application to query a database and retrieve the results as XML. Click Finish.
  4. Switch to the Navigator view and observe the generated HTML, XSD, XST, XSL, and XML files. You can use the XML tools to work further with these files. For example, you could trace through the generated XSL file with the XSL Trace tool by applying it to the XML file. To invoke the XSL Trace tool, select both the XSL and XML file, and select Apply XSL => As XML from the pop-up menu. You can use the XML Schema editor, HTML editor, or the XML editor to browse or edit the XSD, HTML, and XML files respectively.

Applying the XSL file to the XML file as HTML or viewing the HTML file gives us the following results:

TitleNameDue Date
The Adventures of Kernighan and RitchieJohn DoeThursday
The Taming of the FooJohn DoeThursday

Using a query template file in a servlet

This section describes how queries can be executed at run time to dynamically generate XML from SQL. Snippets of code are presented to give you an idea of how to incorporate this into your own servlet. A sample servlet and HTML file are shipped with Application Developer that you can modify for your own purposes. They use the XST file and code described in this section. For an example of how to use the sample servlet, see the following section on deploying the sample servlet.

You can use the query template (XST) file created with the SQL to XML wizard in the video store application at run time to dynamically generate XML from SQL. To do this, use the SQLtoXML Java class shipped with Application Developer. The generated XST file contents are as follows. The XST file provides the SQLtoXML class with database connection information, the SQL query and the options you selected to generate the XML result.

<?xml version="1.0"?>  
<SQLGENERATEINFORMATION>  
  <DATABASEINFORMATION> 
    <LOGINID>JOAN</LOGINID> 
    <PASSWORD><![CDATA[somepassword</PASSWORD> 
    <JDBCDRIVER>COM.ibm.db2.jdbc.app.DB2Driver</JDBCDRIVER> 
    <JDBCSERVER>jdbc:db2:VIDEOS</JDBCSERVER> 
  </DATABASEINFORMATION>  
  <STATEMENT>  
    <![CDATA[ SELECT WSAXML.VIDEOS.TITLE AS VIDEOTITLE,  
    WSAXML.CUSTOMERS.NAME AS CUSTNAME,  
    CASE DAYOFWEEK(WSAXML.RENTALS.DATE)  
    WHEN 1 THEN 'Sunday' WHEN 2 THEN 'Monday' WHEN 3 THEN 'Tuesday'  
    WHEN 4 THEN 'Wednesday' WHEN 5 THEN 'Thursday' WHEN 6 THEN 'Friday' 
    WHEN 7 THEN 'Saturday' END AS DUEDATE FROM WSAXML.VIDEOS,  
    WSAXML.RENTALS, WSAXML.CUSTOMERS WHERE WSAXML.VIDEOS.VID_ID =  
    WSAXML.RENTALS.VID_ID AND WSAXML.RENTALS.CUST_ID =  
    WSAXML.CUSTOMERS.CUST_ID AND WSAXML.CUSTOMERS.NAME =  
    :custName GROUP BY DAYOFWEEK(WSAXML.RENTALS.DATE),  
    WSAXML.VIDEOS.TITLE, WSAXML.CUSTOMERS.NAME 
  </STATEMENT>  
    <OPTIONS>  
    <FORMATOPTION>GENERATE_AS_ELEMENTS</FORMATOPTION>  
    <RECURSE>FALSE</RECURSE>  
  </OPTIONS>  
</SQLGENERATEINFORMATION>

To use the SQLToXML class in a Java servlet, the classpath must be set to include the Xalan processor and the Xerces parser as well as the sqltoxml.jar file. You can create a servlet from scratch or modify the generic servlet (XMLIntegratorServlet.java) that is shipped with Application Developer. You can use WebSphere Application Server or Tomcat to run the servlet. See the next section for more information on servlet deployment.

To load the template file, use the QueryProperties class that is included with the SQLToXML run-time library as follows:

QueryProperties qp = newQueryProperties(); 
qp.load("locateRentedVideo.xst");

The code to instantiate the SQLToXML class is as follows:

SQLToXML sql2xml = new SQLToXML(qp);

You can use the execute methods from the SQLToXML class to generate the files that will be required to display the query results to the user of the application. For our video store query to locate rented videos for a customer, we used a variable in our search condition. The name of the customer will be entered by an employee of the video store to query the customers, videos and rentals stored in the store database. In the Java code, you would pass a parameter containing the value entered by the employee through the parameters list of the execute method to provide the name of the video to the query.

An example of one of the execute methods in use for the video store Java application is shown below. Note that the first argument is passing a value for the :custname variable that we have in our query to the Java application.

sql2xml.execute('John Doe', xmlPrintWriter, "customerRentals.dtd",  
  "customerRentals.xsd", xslPrintWriter);

The results of execute method (XML, XSD, DTD, XSL) are used by the Java application to display the query results to the employee.


Deploy the sample servlet to WebSphere Application Server from a Web project

A generic servlet (XMLIntegratorServlet.java) for WebSphere Application Server, Version 4.0 demonstrates the typical use of the SQLToXML run-time library. You can modify this servlet and use it for other applications such as the video store application. You can then deploy the servlet to WebSphere Application Server from within Application Developer. Here, I will show you how to modify the generic servlet along with the query template (XST) file generated earlier and use it to deploy to WebSphere Application Server. The steps, described in more detail below, are as follows.

  1. Create a Web project
  2. Import the servlet into the Web project
  3. Set classpath variables
  4. Add required libraries to the project build path
  5. Import application sample files
  6. Modify HTML and servlet to point to our application
  7. Modify the servlet and HTML to accept a parameter
  8. Import the SQLToXML JAR file into the WEB-INF directory
  9. Run the application on the server using the query template file

Create a Web project

First, we will create a new Web project for the video store application. To do this:

  1. From the menu, select File => New => Project.
  2. In the New Project wizard, select the Web category on the left side and then Web Project from the right side. Click Next.
  3. Enter VideoWeb for the name of the project and accept all of the other default values. Click Finish.
  4. The Web project is created in the workspace for you and the perspective has changed to the Web perspective.

Import the servlet into the Web project

Next, we need to import the servlet into the project so that we can use it for deployment. To do this:

  1. From the menu, select File => Import.
  2. In the Import wizard, select to import from the File system. Click Next.
  3. For the directory, use the Browse button and navigate to the following path under the directory where Application Developer is installed: plugins\com.ibm.etools.sqltoxml\samples\was4.0. Click OK.
  4. Expand the tree, and select the source check box.
  5. In the Folder field (this is the destination directory for the import), enter VideoWeb.
  6. Click Finish.
  7. Look in the VideoWeb\source directory in the Navigator view and you will see the path com\ibm\etools\sqltoxml\servlet that contains two Java files. Ignore any errors displayed in the Tasks view. They will disappear as we finish setting up the project.

Set classpath variables

The SQLTOXML, WAS_XALAN, and XERCES classpath variables need to be set for the project. The XML perspective automatically initializes these variables. We will switch to the XML Perspective to take advantage of this. From the menu, select Perspective => Open => XML. You can check which classpath variables are defined in the environment by looking at the Java preferences. To do this:

  1. Select Window => Preferences.
  2. In the Preferences wizard, expand the Java node and click on Classpath Variables.
  3. On the right side of the wizard, you will see all of the classpath variables listed.

Add required libraries to the project build path

The SQLToXML, XALAN, and XERCES libraries need to be added to the Java build path for the Web project. Since the libraries are already variables in the classpath, we can simply add the variable to the libraries list for the Web project. This method of adding the JAR files means that the paths are not hardcoded and therefore relocatable. To do this:

  1. Right-click on the Web project and select Properties from the pop-up menu.
  2. In the Properties dialog, select Java Build Path from the list on the left side of the dialog.
  3. On the right side, click the Libraries tab and you will see the libraries already in the project build path.
  4. Click the Add Variable button.
  5. In the Classpath Variable Selection dialog, use the Browse button to select the SQLTOXML variable. Click OK.
  6. Click OK again to close the Classpath Variable Selection dialog.
  7. Repeat the last four steps to add the following two variables to the Libraries list:
    • XERCES
    • WAS_XALAN
  8. Now all of the libraries have been added (see Figure 3 below). Click OK.

    Figure 3. The Java Build Path with all of the libraries added
    Screen capture of the Java Build Path view with all of the libraries added

Import application sample files

To use the sample application files, we need to import them into the Web project:

  1. From the menu, select File => Import. Select to import from File System, and click Next.
  2. Browse to the plugins\com.ibm.etools.sqltoxml\samples\was4.0\ directory.
  3. Expand the directory tree and select the webApplication check box.
  4. In the Folder field (this is the destination directory for the import), enter VideoWeb. Click Finish.
  5. Select Yes to overwrite web.xml with the imported copy.

Modify HTML and servlet to point to our application

Some paths in the sample code need to be modified to match the path for the VideoWeb project. Namely, the context root for the POST command in the HTML and the servlet variable path in the servlet. First, we will modify the path for the context root:

  1. In the Navigator view, expand the webApplication view and find the XMLIntegrator.html file.
  2. Right-click on the XMLIntegrator.html file, and select Open with => Source Editor.
  3. The HTML source is now displayed in the editor.
  4. Change the FORM tag:
    <FORM name="sqltoxmlsample" METHOD="POST"  
      ACTION="/sqltoxml/servlet/XMLIntegratorServlet">


    to:
    <FORM name="sqltoxmlsample" METHOD="POST"  
      ACTION="/VideoWeb/servlet/XMLIntegratorServlet">

  5. Save the file by selecting File => Save XMLIntegrator.html.

Next, we will modify the XMLIntegratorServlet SERVLET variable:

  1. In the Navigator view, expand the VideoWeb project source directories until you find the XMLIntegratorServlet.java file.
  2. Double-click on XMLIntegratorServlet.java to open the file in the Java editor.
  3. Find the line:
    private static final String SERVLET =  
      "/sqltoxml/servlet/XMLIntegratorServlet";


    and change it to:
    private static final String SERVLET =  
      "/VideoWeb/servlet/XMLIntegratorServlet";

  4. Save the file by selecting File => Save XMLIntegratorServlet.java.

Modify the servlet and HTML to accept a parameter

The sample XMLIntegrator.html file has one input field to get the name of the XST file from the user. The customerRentals query has a parameter that also requires customer input: the customer name. To modify the HTML file so that there is an input field for the parameter, do the following:

  1. If the file is not already open, open it by right-clicking and selecting Open with => Source Editor.
  2. Look for the line:
    <input type="Hidden" name="params" VALUE="" SIZE=50 MAXLENGTH=50>

  3. Add a label, change the type to Text, and put the label and text in a table row as follows:
    <tr>  
    <td>  
    <B>Customer Name:</B>  
    </td>  
    <td>  
    <input type="Text" name="params" VALUE="" SIZE=50 MAXLENGTH=50>  
    </td>  
    </tr>

  4. Save the file. The XMLIntegratorServlet already has code to process parameters so this is the only change we need to make.

Import the SQLToXML JAR file into the WEB-INF directory

At run time, the SQLToXML JAR file will be needed to access things like the QueryProperties class that was described in the last section. To make it available at run time, the JAR file needs to be in the Web project's WEB-INF directory. Import the JAR file as follows:

  1. In the Navigator view, select the lib directory under VideoWeb\webApplication\WEB-INF.
  2. From the menu, select File => Import.
  3. Select to import from the File system and click Next.
  4. Browse to plugins\com.ibm.etools.sqltoxml\jars\ under the directory where Application Developer is installed.
  5. Click on jars in the left pane. In the right pane, check the sqltoxml.jar check box.
  6. Set the destination folder to be VideoWeb/webApplication/WEB-INF/lib.
  7. Click Finish. The JAR file appears in the lib directory for the Web application.

Run the application on the server using the query template file

Now we are ready to run the application on the server. I will show you how start the server and then how to run the query using the query template file.

  1. Right-click on the XMLIntegrator.html file in the Navigator view.
  2. Choose Run on Server from the pop-up menu. A publishing message appears followed by a message that the server is starting. The Server perspective now opens.
  3. Messages in the Console at the bottom of the Server perspective show you what is happening with the application server. You will see messages indicating that the server is starting.
  4. Once the server is running, the XMLIntegrator.html file will appear in the Application Developer Web browser. See Figure 4 below.
    Figure 4. The XMLIntegrator.html file as it appears in the Application Developer Web browser
    Screen capture of the XMLIntegrator.html file as it appears in the Application Developer
  5. In the entry field, enter the location of the customerRentals.xst file that was generated earlier.
  6. Enter the parameter 'John Doe' for the query in the parameter entry field.
  7. Click Submit to submit the query. You will see the results in the Web browser as shown in Figure 5 below.
    Figure 5. The results of the query displayed in the Web browser
    Screen capture of the results of the query displayed in the Web browser

To stop the server when you are finished running the application:

  1. Click the Servers tab in the console.
  2. Right-click on the running server, and select Stop from the menu.

Summary

In this article, I discussed the support that WebSphere Studio Application Developer provides for using SQL with XML in a Web application. You have seen how to use Application Developer to:

  • Generate XML from an SQL query and other XML-related files: XSL, DTD, XML Schema and query template file.
  • Dynamically generate XML from SQL queries at run time using the generated query template, DTD and the SQLToXML class library within a servlet.
  • Deploy this servlet from a Web project to WebSphere Application Server using the sample servlet.

In future articles, you will be shown the integration of the SQL Builder with other XML tools. For example, the creation of DAD files using the RDB to XML mapping tool and the use of DAD files for DADX Web services.



Download

NameSizeDownload method
createVideoStore.zip3 KBFTP|HTTP

Information about download methods


About the author

Joan Haggarty is a Staff Software Developer at the IBM Toronto Lab. She is a member of the XML tools team for WebSphere Studio Application Developer, focusing specifically on the SQL Builder. You can contact Joan at joan@ca.ibm.com.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=WebSphere
ArticleID=13887
ArticleTitle=IBM WebSphere Developer Technical Journal: XML and WebSphere Studio Application Developer
publish-date=02052002
author1-email=
author1-email-cc=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Try IBM PureSystems. No charge.

Special offers