Developer's Daily Java Education
  front page | java | perl | unix | DevDirectory
   
Front Page
Java
Education
Pure Java
Articles
   
 
JDBC 101: How to connect to an SQL database with JDBC
 

Introduction

If you're interested in connecting your Java applets and applications to standard SQL databases like Oracle, Informix, Sybase, and others, JDBC is your ticket to paradise.  The combination of Java's JDBC and standard SQL makes a simple and powerful database solution. JDBC makes the simple things easy -- without making the complex tasks too difficult either.

In this first article in our series, we'll show you step-by-step how to establish a connection from your Java programs to an SQL database using JDBC. In the process we'll show you how to connect to two different databases -- Mini SQL (mSQL), and Interbase -- just so you can see how the code changes when you switch from one database to another.
 

Obtaining the JDBC driver

Before you start working with JDBC, you'll need a copy of the Java JDK. If you don't have it already, you can get the JDK for free at Sun's Java web site, or it will also be included with many IDE's that you can purchase, such as JBuilder or Visual Cafe.

Once you have the JDK, the next thing you need to do is to get the correct JDBC driver for your database. In most cases the JDBC driver will be provided by your database vendor. For instance, if you purchase the Interbase database, the driver will be provided with the software, or you can obtain the most recent version at http://www.interbase.com.
 
(An exception to this rule is Mini SQL, or mSQL. Because it's a very low-cost database, the JDBC driver has actually been developed by a separate group of people, led by George Reese at imaginary.com. You can download the mSQL JDBC driver from the imaginary.com web site.)

Once you have the correct JDBC driver for your database, install it according to the instructions that came with it. Installation instructions will vary somewhat for each vendor.
 

Establishing a connection is a two-step process

Once you have the correct JDBC driver installed, establishing a connection from your Java programs to your SQL database is pretty easy.

Regardless of whether you're trying to connect to Oracle, Sybase, Informix, mSQL, or Interbase (or any other JDBC data source), establishing a connection to an SQL database with Java JDBC is a simple two-step process:

    1. Load the JDBC driver.
    2. Establish the connection.
We'll show you two examples just so you can see how easy it is, and how little the code changes when you migrate from one database server to another.
 

A Mini SQL Example

Listing 1 provides the full source code required to establish a connection to a mSQL database on a server named "www.myserver.com".
 
 
    //  Establish a connection to a mSQL database using JDBC. 

    import java.sql.*; 

    class JdbcTest1 { 
         
        public static void main (String[] args) { 
            try { 

                // Step 1: Load the JDBC driver. 
                Class.forName("com.imaginary.sql.msql.MsqlDriver"); 

                // Step 2: Establish the connection to the database. 
                String url = "jdbc:msql://www.myserver.com:1114/contact_mgr"; 
                Connection conn = DriverManager.getConnection(url,"user1","password");  

            } catch (Exception e) { 
                System.err.println("Got an exception! "); 
                System.err.println(e.getMessage()); 
            } 
        } 
    } 

 
Listing 1: This source code example shows the two steps required to establish a connection to a Mini SQL (mSQL) database using JDBC. 
   
 
 

An Interbase Example

Listing 2 provides the full source code required to establish a connection to an Interbase database. In this example, we're connecting to a local Interbase server (i.e., the server is running on the same PC that we're running the Java code on).
 
 
    //  Establish a connection to an Interbase database using JDBC. 

    import java.sql.*; 

    class JdbcTest1 { 
         
        public static void main (String[] args) { 
            try { 

                // Step 1: Load the JDBC driver. 
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 

                // Step 2: Establish the connection to the database. 
                String url = "jdbc:odbc:contact_mgr"; 
                Connection conn = DriverManager.getConnection(url,"user1","password");  

            } catch (Exception e) { 
                System.err.println("Got an exception! "); 
                System.err.println(e.getMessage()); 
            } 
        } 
    } 

 
Listing 2: This source code example shows the two steps required to establish a connection to an Interbase database using JDBC. 
   
 

What's the difference?

The difference between the two source code listings is very small, so we highlighted them in a dark blue color. The only difference between connecting to the two databases is:

    1. The name of the JDBC driver.
    2. The URL used to connect to the database.
Everything else in the two source code listings -- except for the comment at the top -- is identical. Here's a slightly more detailed discussion of the two differences:
 

1. The JDBC Driver

2. The URL

If you stick with standard SQL commands, it can be very easy to switch from one database server to another. In fact, I've heard from several developers who are using mSQL to prototype their software (because it's so inexpensive), and then switching to another commercial vendor when it's time to take their product "live".
 


Conclusion

Establishing a connection to an SQL database with Java JDBC is a simple, two-step process. The process is nearly identical for all SQL databases, and the only real differences are (a) the driver name, and (b) the URL used to connect to the database. Your database vendor will provide this information in their documentation.
 

Resources mentioned in this article

Here are a few links to resources we mentioned in this article:

 

What's Related


Copyright 1998-2008 DevDaily Interactive, Inc.
All Rights Reserved.