Technical Note

Creating XSQL Queries in Oracle JDeveloper
Author: Deepak Vohra (Web Developer and Sun Certified Java 1.4 programmer)
Date: June 2004

PL/SQL is commonly used to create a connection to a database, generate tables, retrieve result sets, and perform updates. The result set from a SQL query with PL/SQL, of course, is a text table. With the increasing use of XML in J2EE applications, many J2EE developers now require this result to be represented as an XML document for exchange of data.

The Oracle XSQL Servlet tool, which is included in Oracle JDeveloper 10g, supports the processing of SQL queries as well as the output of the result set as XML. This XML output may be required in a different format or the output may need to be modified, so XSQL Servlet supports XSLT transformation as well for those purposes.

An XSQL file (.xsql file) comprises SQL query statements as <xsql:query></xsql:query> tags. XSQL may also be used to create or update a database table with <xsql:dml></xsql:dml> tags. XSQL supports XSLT transformation by specifying a stylesheet in a XSQL page. In this Technical Note, we will query an example database table containing a catalog with XSQL. We'll then display the XML output as an HTML table via XSLT transformation.

Preliminary Setup

The database tables created/queried with XSQL are created in an Oracle database. In this tutorial, Oracle Database 10g is used to create the database tables.

First, create and start an instance of the Oracle Database. Connect to the database with the SCOTT schema (username).

CONNECT SCOTT/<password>

Then use the SQL script below to create a database table that consists of an example Oracle catalog:

CREATE TABLE OracleCatalog(Journal VARCHAR(25), Publisher Varchar(25),
 Edition VARCHAR(25), Title Varchar(45), Author Varchar(25));

INSERT INTO OracleCatalog VALUES('Oracle Magazine',  'Oracle Publishing',
 'November-December 2003', 'Servlets and JSP Step Up', 'Budi Kurniawan');

INSERT INTO OracleCatalog VALUES('Oracle Magazine',    'Oracle Publishing',
 'September-October 2003', 'Parsing XML Efficiently', 'Julie Basu');

We will run the example XSQLs in JDeveloper 10g Production. Download and install JDeveloper 10g Production; then, create a new project as illustrated in Figure 1.

figure 1
Figure 1: XSQL Project

Creating the Database Connection

A database connection is required for an XSQL query. To create a database connection in JDeveloper, select the Connections>Database node in the Connections Navigator as shown in Figure 2.

figure 2
Figure 2: Database Node in Navigator

A Connection Wizard is then displayed, as shown in Figure 3.

figure 3
Figure 3: Connection Wizard:Type

In the Connection Wizard specify a Connection Name and a Connection Type and click the Next button. In the next frame displayed, specify a Username and Password and click the Next button.

In the subsequent Connection frame specify a JDBC Driver, Host Name, JDBC Port, and database SID and click the Next button as shown in Figure 4.

figure 4
Figure 4: Connection Wizard: Connection

In the subsequent Test frame, click the Test Connection button to test the JDBC connection (see Figure 5). Click the Finish button to configure the database connection.

figure 5
Figure 5: Connection Wizard:Test

As Figure 6 shows, you'll see that a connection node has been added to the Connections>Database node in the Applications Navigator.

figure 6
Figure 6: Connection Node

A connection with a database may also be created by modifying the <JDeveloper>/jdev/system9.0.5.1.1605/XSQLConfig.xml file. <JDeveloper> is the directory in which JDeveloper 10g is installed; the XSQLConfig.xml file may be set to have read privileges by the user running the application server. In the <connectiondefs> element in XSQLConfig.xml add a <connection> element specifying the connection to be created. Create a connection with the SCOTT schema.

<connection name="dbConnection">
      <username>SCOTT</username>
      <password></password>
      <dburl>jdbc:oracle:thin:@<HOST>:<PORT>:<SID></dburl>
      <driver>oracle.jdbc.driver.OracleDriver</driver>
</connection>

  • Attribute name is the connection name
  • Element username is the username used to log in to the database
  • Element password is the password used to log in to the database
  • Element dburl is the URL of the database
  • Element driver is the JDBC driver used to connect to the database.
  • <HOST> is the Oracle 10g Production database host.
  • <PORT> is the database port.
  • <SID> is the database SID.

Next, copy XSQLConfig.xml into the project source directory in JDeveloper. We will use the connection dbConnection configured in the XSQLConfig.xml file to specify the value of the connection attribute in the <page> tag of an XSQL page.

The Oracle 10g version of XSQL (which is part of the Oracle 10g XDK) also supports the new ability to leverage JDBC datasources defined by your J2EE container, which removes the connection definition from the XSQLConfig.xml file and defers the connection pool management to your J2EE container. The datasource used for a connection with the database is specified in the connection attribute of the <page> tag in a XSQL page. Because the J2EE container generally supports the encrypting of passwords for their JDBC connection definitions, this approach may be even more secure in certain situations. (See the "Use Your Servlet Container's DataSource Implementation" section in the Oracle 10g XSQL Release Notes for more details.)

Processing XSQL Query in an XSQL Page

Now let's create an XSQL page (queryDb.xsql) in JDeveloper. To create an XSQL file, select the project node in the Applications Navigator frame. In the next frame, select General>XML. In the Items listed, select XSQL Page. An XSQL page will be generated, as shown in Figure 7.

figure 7
Figure 7: XSQL Page

Next we'll add <xsql:query></xsql:query> tags to the XSQL page to process our XSQL queries. The application of XSQL queries may require the use of bind variables, the variables in a SQL statement. Bind variables can be set using the values of URL parameters, session variables, cookie values, or page parameters. In our example, we'll set XSQL query bind variables with page parameters.

Bind variables, specified with the bind-params attribute in the <xsql:query> tag, are used in the SQL statement within the <xsql:query> tag. The bind variables are represented with '?' in a SQL statement. The value of the bind parameters is specified with the <xsql:set-page-params/> tag.

<page xmlns:xsql="urn:oracle-xsql" connection="dbConnection">
<xsql:set-page-param name="JOURNAL" value="Oracle Magazine"/>
<xsql:set-page-param name="PUBLISHER" value="Oracle Publishing"/>
<xsql:query xmlns:xsql="urn:oracle-xsql"
      bind-params="JOURNAL  PUBLISHER">
          select JOURNAL, PUBLISHER, EDITION, 
               TITLE, AUTHOR from OracleCatalog WHERE JOURNAL=? AND PUBLISHER=?
</xsql:query>
</page>
 

The <xsql:query></xsql:query> tags may be added from the XSQL Component Pallete, as illustrated in Figure 8.

figure 8
Figure 8: Query Component in the XSQL Component Palette

The attribute connection in the page tag specifies the connection used to query the database. Our XSQL page, queryDb.xsql, is shown in Figure 9.

figure 9
Figure 9: queryDb.xsql XSQL Page

To run queryDb.xsql, right-click the queryDb.xsql node in the Applications Navigator and select Run, as shown in Figure 10.

figure 10
Figure 10: Run queryDb.xsql

The result of the XSQL query is an XML document; the XSQL page queryDb.xsql displays the XML output of the XSQL query in the default browser. The result of our XSQL query is illustrated below.

- <page>
 - <ROWSET>
  - <ROW num="1">
    <JOURNAL>Oracle Magazine</JOURNAL>
    <PUBLISHER>Oracle Publishing</PUBLISHER>
    <EDITION>November-December 2003</EDITION>
    <TITLE>Servlets and JSP Step Up</Title>
    <AUTHOR>Budi Kurniawan</AUTHOR>
    </ROW>

 - <ROW num="2">
    <JOURNAL>Oracle Magazine</JOURNAL>
    <PUBLISHER>Oracle Publishing</PUBLISHER>
    <EDITION>September-October 2003</EDITION>
    <TITLE>Parsing XML Efficiently</Title>
    <AUTHOR>Jilua Basu</AUTHOR>
    </ROW>
 </ROWSET>
</page>

Next we'll examine the different <xsql:query> tag attributes used in a XSQL query.

Setting XSQL Query Attributes

The <xsql:query> tag provides XSQL attributes not only to modify the data retrieved with the XSQL query but to modify the XML document produced with the XSQL query. The different XSQL attributes and their applications are listed in Table 1.

Table 1. <xsql:query> Attributes
Attribute (data type)Attribute Description
fetch-size (integer)Number of records to fetch in each fetch
Id-attribute (string) XML attribute to use in a row in the result set. The default XML attribute used is 'num'.
max-rows (integer) Maximum number of rows to fetch
null-indicator (boolean) If set to 'true', the NULL columns are included in the result set with a NULL='Y' attribute.
row-element (string) XML element name to be used to replace the default <ROW> element
rowset-element (string) XML element name to be used to replace the default <ROWSET> element
skip-rows (integer) Number of rows to skip before fetching rows from the result set

As described earlier, a <xsql:query> tag with the attributes set may be added to the queryDb.xsql with the Query component in the XSQL Component Palette. First, select the Query Component in the XSQL Component Palette. A Query frame is displayed, as illustrated in Figure 11. Next, specify values for the different <xsql:query> attributes.

figure 11
Figure 11: Query Frame

Now add an SQL query in the <xsql:query/> tag. The queryDb.xsql XSQL page with the query attributes set is illustrated in Figure 12.

figure 12
Figure 12: queryDb.xsql with Query Attributes

Run queryDb.xsql by right-clicking the queryDb.xsql node in the Applications Navigator frame and selecting Run. The result set of the XSQL query is displayed in your default browser as illustrated below.

- <page>
 - <Catalog>
  - <Journal-Edition Id="2">
    <JOURNAL>Oracle Magazine</JOURNAL>
    <PUBLISHER>Oracle Publishing</PUBLISHER>
    <EDITION>September-October 2003</EDITION>
    <TITLE>Parsing XML Efficiently</Title>
    <AUTHOR>Julie Basu</AUTHOR>
   </Journal-Edition>
  </Catalog>
</page>

In the following section, we'll discuss XSLT transformation of the output from an XSQL query.

Transforming Output with XSQL

Now we'll create an XSLT file to transform the XSQL output in JDeveloper. Select File>New and select Categories:General>XML in the Categories frame. Select XSLT Stylesheet from the items listed as shown in Figure 13.

figure 13
Figure 13: Creating an XSLT Stylesheet

Specify Directory Name and an XSLT File Name in the frame displayed. Now, modify the XSLT file in JDeveloper. The XSLT used to generate an HTML table from the XSQL query XML output is illustrated below.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="ISO-8859-1"  method="text/html" />
<xsl:template match="/technology/page">
<html>
  <head>
    <title>Oracle Catalog</title>
  </head>
  <body>
    <table border="1" cellspacing="0">
        <tr>
         <th>Journal</th>
         <th>Publisher</th>
         <th>Edition</th>
         <th>Title</th>
         <th>Author</th>
        </tr>
      <xsl:for-each select="ROWSET/ROW">
        <tr>
         <td><xsl:value-of select="JOURNAL"/></td>
         <td><xsl:value-of select="PUBLISHER"/></td>
         <td><xsl:value-of select="EDITION"/></td>
         <td><xsl:value-of select="TITLE"/></td>
         <td><xsl:value-of select="AUTHOR"/></td>
        </tr>
      </xsl:for-each>
    </table>
  </body>
</html>
</xsl:template>
</xsl:stylesheet>

The XSLT file itself, OracleCatalog.xsl, is illustrated in Figure 14.

figure 14
Figure 14: OracleCatalog.xsl

Now add the following reference to specify that OracleCatalog.xsl will transform the XSQL query output in the XSQL file (queryDb.xsql) used to query the database:

<?xml-stylesheet type="text/xsl" href="OracleCatalog.xsl"?>

The resulting XSQL file is illustrated in Fig 15.

figure 15
Figure 15: queryDb.xsql with stylesheet

Finally, run queryDb.xsql in JDeveloper as illustrated in Figure 16.

figure 16
Figure 16: Running queryDb.xsql

The output of the XSQL query with the XSLT transformation is displayed in an HTML table in the default browser, as illustrated below:

JournalPublisherEditionTitleAuthor
Oracle MagazineOracle PublishingNovember-December 2003Servlets and JSP Step UpBudi Kurniawan
Oracle MagazineOracle PublishingSeptember-October 2003Parsing XML EfficientlyJulie Basu

You have just used the XSQL Servlet tool in JDeveloper 10g to query a database table and then transform the resulting XML output with an XSLT stylesheet.




Please rate this document:

Excellent Good Average Below Average Poor


Send us your comments

E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy