Chapter 11. Generating Datagrams with Java

Java programmers using Oracle8i have a myriad of options for outputting database information as XML. This chapter covers all of the available approaches, proceeding in order from the most manual to the most automatic, with examples every step of the way.

Generating XML Using Java

In this section, we’ll learn the basics of using Java to generate XML datagrams containing database query results. The two basic programmatic techniques for accessing SQL query results from Java are the JDBC API and SQLJ.

Generating XML with JDBC

The most basic way in Java to produce XML from database information is to use a JDBC ResultSet to execute a SQL statement and loop over its results. Developers familiar with database programming using the JDBC interface will find this technique to be second nature. Example 11.1 issues a query to retrieve current stock quotes for all the positions in a particular customer’s portfolio. Most interesting queries in an application depend on some kind of context information being supplied. Example 11.1 shows how to use a bind variable in the SQL statement, setting the value of the bind variable to the customer id passed in as a command-line argument. This allows the same SQL statement to be used for retrieving the appropriate stock quotes in any customer’s portfolio.

Example 11-1. Using a JDBC Result Set to Produce XML

import java.sql.*; class StockQuotesXmlJdbc { public static void main (String arg[]) throws Exception { // Connect ...

Get Building Oracle XML Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.