This tutorial is designed to assist JavaTM developers who need to extract information from a database and place it into an XML document.
The tutorial assumes that you are already familiar with Java and XML in general, and the Document Object Model (DOM) in particular. You should be familiar with Java programming, but prior knowledge of database connections using JDBCTM is not required to master the techniques described in this tutorial. The tutorial briefly covers the basics of SQL. GUI programming knowledge is not necessary because application input/output is handled from the command line. The links in Resources include referrals to tutorials on XML and DOM basics, and to a detailed SQL backgrounder.
XML works so well for storing data that it's inevitable that at some point someone will ask you to pull information from a database and manipulate it as though it were XML. JDBC is a vendor-independent method for accessing databases using Java. This tutorial explains how to instantiate and use a JDBC driver to connect to a database in order to retrieve information. It also explains the basics of SQL, and how to create and use the results of a JDBC query.
The goal of the tutorial is to extract data from a database and create a DOM document. The structure of the DOM document is determined by an XML mapping file, which demonstrates one way that XML files can be used for this purpose.
This tutorial will help you understand the topic even if you read the examples rather than trying them out. If you do want to try the examples as you go through this tutorial, make sure you have the following tools installed and working correctly:
- A text editor: XML and Java source files are simply text. To create and read them, a text editor is all you need.
- A Java environment such as the Java 2 SDK, which is available at http://java.sun.com/j2se/1.3/.
- Java APIs for XML Processing: Also known as JAXP 1.1, this is the reference implementation that Sun provides. You can download JAXP in Java Web Services Developer Pack 1.1.
- Any database that understands SQL, as long as you have an ODBC or JDBC driver. You can find a searchable list of more than 150 JDBC drivers at http://industry.java.sun.com/products/jdbc/drivers. (If you have an ODBC driver, you can skip this step, and use The JBDC-ODBC bridge.) This tutorial uses JDataConnect, available at http://www.jnetdirect.com/products.php?op=jdataconnect.
There are several conventions used to reinforce the material in this tutorial:
- Text that needs to be typed is displayed in a
bold monospacefont. In some code examples bold is used to draw attention to a tag or element being referenced in the accompanying text.
- Emphasis/Italics is used to draw attention to windows, dialog boxes, and feature names.
monospacefont presents file and path names.
- Throughout this tutorial, code segments irrelevant to the discussion have been omitted and replaced with ellipses (