| Developer's Daily | Java Education |
| front page | java | perl | unix | DevDirectory |
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:
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".
import java.sql.*; class JdbcTest1 {
// Step 1: Load the JDBC driver.
// Step 2: Establish the connection to the database.
} catch (Exception e)
{
|
| 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).
import java.sql.*; class JdbcTest1 {
// Step 1: Load the JDBC driver.
// Step 2: Establish the connection to the database.
} catch (Exception e)
{
|
| 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 JDBC Driver
The username and password are the normal names you use to log into your database. The URL you use will again vary with the database you use. In both examples shown, we're establishing a connection to a database named contact_mgr. (We'll use this database for all of our examples in this series of JDBC articles.)
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:
Copyright 1998-2008 DevDaily Interactive, Inc.
All Rights Reserved.