Once the basics of Java™ procedures, programming in Java using the JDBC application programming interface (API), and XQuery are understood, you can start creating and using Java procedures that query XML data.
The examples below makes use of a table named xmlDataTable defined and containing data as follows:
CREATE TABLE xmlDataTable
(
num INTEGER,
xdata XML
)@
INSERT INTO xmlDataTable VALUES
(1, XMLPARSE(DOCUMENT '<doc>
<type>car</type>
<make>Pontiac</make>
<model>Sunfire</model>
</doc>' PRESERVE WHITESPACE)),
(2, XMLPARSE(DOCUMENT '<doc>
<type>car</type>
<make>Mazda</make>
<model>Miata</model>
</doc>' PRESERVE WHITESPACE)),
(3, XMLPARSE(DOCUMENT '<doc>
<type>person</type>
<name>Mary</name>
<town>Vancouver</town>
<street>Waterside</street>
</doc>' PRESERVE WHITESPACE)),
(4, XMLPARSE(DOCUMENT '<doc>
<type>person</type>
<name>Mark</name>
<town>Edmonton</town>
<street>Oak</street>
</doc>' PRESERVE WHITESPACE)),
(5, XMLPARSE(DOCUMENT '<doc>
<type>animal</type>
<name>dog</name>
</doc>' PRESERVE WHITESPACE)),
(6, NULL),
(7, XMLPARSE(DOCUMENT '<doc>
<type>car</type>
<make>Ford</make>
<model>Taurus</model>
</doc>' PRESERVE WHITESPACE)),
(8, XMLPARSE(DOCUMENT '<doc>
<type>person</type>
<name>Kim</name>
<town>Toronto</town>
<street>Elm</street>
</doc>' PRESERVE WHITESPACE)),
(9, XMLPARSE(DOCUMENT '<doc>
<type>person</type>
<name>Bob</name>
<town>Toronto</town>
<street>Oak</street>
</doc>' PRESERVE WHITESPACE)),
(10, XMLPARSE(DOCUMENT '<doc>
<type>animal</type>
<name>bird</name>
</doc>' PRESERVE WHITESPACE))@
The example shows a Java procedure implementation. The example consists of two parts: the CREATE PROCEDURE statement and the external Java code implementation of the procedure from which the associated Java class can be built.
The Java source file that contains the procedure implementations of the following examples is named stpclass.java included in a JAR file named myJAR. The file has the following format:
|
The Java class file imports are indicated at the top of the file. The com.ibm.db2.jcc.DB2Xml import is required if any of the procedures in the file contain parameters or variables of type XML will be used.
It is important to note the name of the class file and JAR name that contains a given procedure implementation. These names are important, because the EXTERNAL clause of the CREATE PROCEDURE statement for each procedure must specify this information so that DB2® can locate the class at run time.
|
|