he combination of XML features and JDBC has provided Java developers with more powerful choices for manipulating data within their applications. Meanwhile, the evolution of Java has spawned many JDBC driversinterfaces for connecting with a database and manipulating data within a Java applicationto handle the needs of particular applications (see Sidebar 1. JDBC Driver Types
). The version 9 release of IBM's DB2 Universal Database (UDB) combines the best of these drivers with native XML data storage to simplify data-intense Java application development.
Today, pure Java drivers (type 4) are the most popular for Java application development because of their adaptability and natural fit with many application servers and open source tools. As such, IBM promotes the JDBC Universal type 4 driver (popularly termed the JCC driver) as the primary driver for DB2 V9 (see Sidebar 2. JCC Driver for DB2). At the same time, the introduction of native XML data storage in DB2 V9 goes a long way in simplifying XML utilization in Java applications.
This article explains how you can leverage these DB2 V9 features to choose the right JDBC driver (see Sidebar 3. DB2 Driver Connection Options) and easily integrate XML data into your applications.
Utilizing pureXML Data in DB2
The introduction of native XML data storage in DB2 UDB Version 9 goes a long way in simplifying XML utilization in application development. Before DB2 Version 9, the only way to exploit XML data was to store the data in CLOB columns and then decompose or shred it when you needed it. Application development using this shredding method was quite complicated and made getting to the actual XML data you needed difficult.
Now that the new pureXML technology is integrated into DB2, database developers can store the data to and extract it from the database in its purest form, and programmers can use SQL or XML APIs to work with data in either its relational or pureXML format. They then can exploit XML data using XPath/XQuery and SQL/XML.
Exploit XML Data Using XPath/XQuery
XPath is a language for addressing parts of an XML document, which because of its hierarchical nature, represents data in a tree-like format. That, in fact, is what makes XML so powerful (Figure 1
provides an example of the hierarchical nature of XML).
While XPath shows the query format and layout of XML data, XQuery enables you to play around with XML data. XQuery is to XML data what SQL is to relational data.
The basis of most XQuery expressions is the FLWOR expression:
- F or
- L et
- W here
- O rder
- R eturn
FLWOR is a common method for extracting XML data from a table. The following is a basic example of the FLWOR expression in use:
for $d in db2-fn:xmlcolumn('dept.deptdoc')/dept
let $emp := $d//employee/name
where $d/@bldg > 95
order by $d/@bldg
As you can see, the code goes into the dept.deptdoc table and gets EmpList items from the dept column where bldg is greater than 95.
Exploit XML Data Using SQL/XML
SQL/XML allows you to embed the XQuery language within SQL, thereby establishing tight-knit integration between development using relational data and SQL and the new way of querying XML using XQuery. Here is an example of SQL/XML:
xmlquery('for $i in $d/dept
let $j := $i//name
return $j' passing deptdoc as "d")
where deptID LIKE "PR%"
and xmlexists('$d/dept[@bldg = 101]' passing deptdoc as "d")
As you can see, the code embeds an XMLQuery statement in a regular select statement. Within the XMLQuery statement, it uses the FLWOR expression to return the deptdoc from the dept table. In effect, it's mixing and matching XMLQuery and regular SQL.