Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Using JDBC to insert data from XML into a database

XML data can find a permanent home

Photo of Nicholas Chase
Nicholas Chase has been involved in Web site development for companies including Lucent Technologies, Sun Microsystems, Oracle Corporation, and the Tampa Bay Buccaneers. Nick has been a high school physics teacher, a low-level radioactive waste facility manager, an online science fiction magazine editor, a multimedia engineer, and an Oracle instructor. More recently, he was the Chief Technology Officer of Site Dynamics Interactive Communications in Clearwater, Florida. He is the author of three books on Web development, including Java and XML From Scratch (Que). He loves to hear from readers and can be reached at nicholas@nicholaschase.com.

Summary:  As flexible as XML is with regards to storing data, it is perfect for holding data that is ultimately destined for a database. This tutorial will show you how to access a database using the JDBC API and use SQL to insert data that has been retrieved from an XML file using a predetermined mapping. It also demonstrates the use of updatable ResultSets.

Date:  03 Oct 2001
Level:  Intermediate PDF:  A4 and Letter (111 KB | 32 pages)Get Adobe® Reader®

Activity:  10236 views
Comments:  

Accessing a database through JDBC

What is JDBC?

(If you have already been through the "Using JDBC to extract data into XML" tutorial, feel free to skip ahead to The orders.xml file.)

It was not very long ago that in order to interact with a database, a developer had to use the specific API for that database. This made creating a database-independent application difficult, if not impossible.

JDBC is similar to ODBC, or Open Database Connectivity, in that it provides a standard API intermediary for accessing a database. As seen on the left, the JDBC driver translates standard JDBC commands into the native API for the database.

You will notice that nowhere in this tutorial is a specific database mentioned, because the choice is largely irrelevant. All commands are standard JDBC commands, which are translated by the driver into native commands for whatever database you choose. Because of this API independence, of sorts, you can easily use another database without changing anything in your application except the driver name and possibly the URL of the connection (which you will use when you Create the connection).

See Resources for information on downloading the appropriate JDBC driver for your database. Over 150 JDBC drivers are available, covering virtually every database.

There are even options for databases that do not have an available JDBC driver, as you will see in the next panel.


Figure 1. Example of database without a JDBC driver
Example of database without a JDBC driver

The JBDC-ODBC bridge

It is not necessary to have a specific JDBC driver for a database as long as an ODBC driver is available; a JDBC-ODBC bridge can be used instead. As you can see on the left, the application accesses the bridge via JDBC calls. The bridge then translates the commands into ODBC, whereupon the ODBC driver translates them into the native API.

The JDBC-ODBC bridge is not the recommended way to access a database for reasons involving both performance and configuration -- commands must pass through two APIs, and the ODBC driver must be installed and configured on every client -- but it is acceptable for testing and development if there is no pure JDBC driver available.

If you choose to use the bridge on a Windows system, create a System Data Source Name (DSN) by choosing Start>Settings>Control Panel>ODBC Data Sources . Note the name of the DSN, as it will be referenced when you Create the connection.


Figure 2. Example of database with JDBC and drivers
Example of database with JDBC and drivers

Set up the database and driver

create table orders (
   orderid       numeric 
                   primary key,
   userid        varchar(50),
   productid     varchar(10),
   quantity      numeric,
   unitprice     numeric )
   
create table productOrders (
   productid     varchar(10),
   quantity      numeric,
   revenue       numeric )

First, make sure your database of choice is installed and running, and that the JDBC driver is available. You can download drivers from http://industry.java.sun.com/products/jdbc/drivers.

Once you have created the database, you will need to create the necessary tables. Ultimately, the structure will be unimportant because it can be determined by the XML mapping file. This tutorial assumes the presence of two tables, orders and productOrders, detailed at the left. Create the tables using the appropriate steps for your database.


The process of accessing a database

Using the Java language to interact with a database usually consists of the following steps:

  1. Load the database driver. This can be a JDBC driver or the JDBC-ODBC bridge.
  2. Create a Connection to the database.
  3. Create a Statement object. This object actually executes the SQL or stored procedure.
  4. Create a ResultSet and populate it with the results of an executed query (if the goal is to retrieve or directly update data).
  5. Retrieve or update the data from the ResultSet.

The java.sql package contains the JDBC 2.0 Core API for accessing a database as part of the Java 2 SDK, Standard Edition distribution. The javax.sql package that is distributed as part of the Java 2 SDK, Enterprise Edition contains the JDBC 2.0 Optional Package API.

This tutorial uses only classes in the JDBC 2.0 Core API.


Instantiate the driver

To access the database, first load the JDBC driver. A number of different drivers may be available at any given time; it is the DriverManager that decides which one to use by attempting to create a connection with each driver it knows about. The first one that successfully connects will be used by the application. There are two ways the DriverManager can know a driver exists.

The first way is to load it directly using Class.forName(), as shown in this example. When the driver class is loaded, it registers with the DriverManager, as shown here:

public class SaveOrders extends Object {

   public static void main (String args[]){

      //For the JDBC-ODBC bridge, use
      //driverName = "sun.jdbc.odbc.JdbcOdbcDriver"
      String driverName = "JData2_0.sql.$Driver";

      try {
         Class.forName(driverName);
      } catch (ClassNotFoundException e) {
         System.out.println("Error creating class: "+e.getMessage());
      }
   }
}

With a successfully loaded driver, the application can connect to the database.

The second way the DriverManager can locate a driver is to cycle through any drivers found in the sql.drivers system property. The sql.drivers property is a colon-delimited list of potential drivers. This list is always checked prior to classes being loaded dynamically, so if you want to use a particular driver make sure that the sql.drivers property is either empty or starts with your desired driver.


Create the connection

Once you load the driver, the application can connect to the database.

The DriverManager makes a connection through the static getConnection() method, which takes the URL of the database as an argument. The URL is typically referenced as:

jdbc:<sub-protocol>:databasename

However, the reference URL can be written in any format the active driver understands. Consult the documentation for your JDBC driver for the URL format.

One occasion where the subprotocol comes into play is in connecting via ODBC. If the sample database, with its DSN orders, were accessed directly via ODBC, the URL might be:

odbc:orders

This means that to connect via JDBC, the URL would be:

jdbc:odbc:orders

The actual connection is created below:

                    import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class SaveOrders extends Object {

   public static void main (String args[]){

      //For the JDBC-ODBC bridge, use
      //driverName = "sun.jdbc.odbc.JdbcOdbcDriver"
      //and
      //connectURL = "jdbc:odbc:orders"
      String driverName = "JData2_0.sql.$Driver";
      String connectURL = "jdbc:JDataConnect://127.0.0.1/orders";
      Connection db = null;	   
      try {
         Class.forName(driverName);
        db = DriverManager.getConnection(connectURL);
      } catch (ClassNotFoundException e) {
         System.out.println("Error creating class: "+e.getMessage());
      } catch (SQLException e) {
        System.out.println("Error creating connection: "+e.getMessage());
      }
   }
}

Once a connection is successfully made, it is possible to perform any required database operations (for example, inserting or updating data).


Closing the connection

Because the Statement and Connection are objects, Java will garbage collect them, freeing the database resources they take up. This may lull you into thinking you do not have to worry about closing these objects, but that is not true.

It is entirely possible that the Java application itself has plenty of resources available, which means less-frequent garbage collection. At the same time, there may be limited database resources available, many of which may be taken up by Java objects that can easily be closed by the application.

It is important to make sure that these objects are closed, whether or not there are any errors, so add a finally block to the try-catch block already in place:

...
      Connection db = null;	  
      try {
         Class.forName(driverName);
         db = DriverManager.getConnection(connectURL);
      } catch (ClassNotFoundException e) {
         System.out.println("Error creating class: "+e.getMessage());
      } catch (SQLException e) {
         System.out.println("Error creating connection: "+e.getMessage());
      } finally {
         System.out.println("Closing connections...");
         try {
            db.close();
         } catch (SQLException e) {
            System.out.println("Can't close connection.");
         }
      }
   }
}

Ironically, the close() method itself can throw a SQLException, so it needs its own try-catch block.

2 of 10 | Previous | Next

Comments



Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=XML
ArticleID=138240
TutorialTitle=Using JDBC to insert data from XML into a database
publish-date=10032001
author1-email=nicholas@nicholaschase.com
author1-email-cc=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Try IBM PureSystems. No charge.