Connecting to a Database

Here are the steps that we need to follow to connect to a database.

  • Obtain the JDBC driver and add it to the CLASSPATH environment variable on your machine.
  • Register the JDBC driver with the DriverManager.
  • Construct a connection URL.
  • Use the getConnection() static method of DriverManager to establish a connection.

The following sections describe these steps in detail.

Obtaining the JDBC Driver

You need to have the JDBC driver for your database before you can connect to the database using JDBC. You can geta JDBC driver from the vendor of your database. For example, if you are using the Oracle DBMS, you can downloadthe JDBC driver from its official web site at www.oracle.com. All database vendors that support JDBC will let youdownload the JDBC driver for their DBMS from their official web sites for free. Typically, a JDBC driver is bundled inone or more JAR/ZIP files.If you are using Java DB, the JDBC drivers were copied on your machine when you installed the JDK. You do notneed to download any additional JDBC drivers.

Setting up the CLASSPATH

If you are using a JDBC driver, you need to place the JAR/ZIP files for your JDBC driver in the CLASSPATH on yourmachine, so that your Java programs can use the Java classes that implement the JDBC driver for your database.Thederby.jar file is the JDBC driver needed to use the Java DB in embedded mode. If you are connecting to Java DB overa network, you will need to include the derbyclient.jar file in the CLASSPATH.

Registering a JDBC Driver

You need to register a JDBC driver, which you want to use to connect to a database. A JDBC driver is registered withthe java.sql.DriverManager class.

What is a JDBC driver?

Technically, a JDBC driver is a class that implements the java.sql.Driver interface.DBMS vendors supply the JDBC driver class along with any other classes it uses. You must know the name of the JDBCdriver class before you can register it with the DriverManager class. If you do not know the name of the driver class,please refer to the documentation of the JDBC driver for your DBMS.In the next section, I will list the driver class names for some DBMSs. The name may vary depending on theversion of DBMS or the supplier of the driver class. Sometimes different vendors supply the drivers for the sameDBMS. Different vendors will use different driver class names and different connection URL formats to connect to thesame DBMS.

Why do you need to register a JDBC driver with the DriverManager

Java does not know how to connect toa database. It depends on the JDBC driver to connect to a database. Think of a JDBC driver as a Java class whoseobject will be used by the DriverManager to connect to a database. The question is, “How does the DriverManagerknow about the JDBC driver you want to use to connect to a database?” Of course, it has no way to know about theJDBC driver by itself. Therefore, registering a driver with the DriverManager is simply telling the DriverManagerabout your JDBC driver class name. By registering a JDBC driver, you are telling the DriverManager that if you askthe DriverManager to establish a connection to a database, it needs to try using this driver. Can you register multipleJDBC drivers with the DriverManager? Yes. You can register multiple JDBC drivers. When you need to establish aconnection to a database, you must pass a connection URL to the DriverManager. The DriverManager passes theconnection URL to all registered drivers one by one, and asks them to connect to the database using informationthat you supply in the connection URL. If a driver recognizes the connection URL, it connects to the database andreturns the connection to the DriverManager. An object of the java.sql.Connection interface represents a databaseconnection in a Java program. If none of the registered drivers recognize a connection URL, the DriverManager willthrow a SQLException stating that it could not find a suitable driver.

There are three ways to register a JDBC driver with the DriverManager:

  • By setting the jdbc.drivers system property
  • By loading the driver class into the JVM
  • By using the registerDriver() method of the DriverManager class
Setting the jdbc.drivers System Property

You can register a JDBC driver class name using the jdbc.drivers system property. You can set this property in yourcomputer globally; you can pass this property on the command line when you run your application, or you can set thisproperty in your application using the System.setProperty() method. A colon separates each driver to be registered.

Here are some examples:

//Register Sybase and Oracle drivers in the Java code

String drivers = “com.sybase.jdbc2.jdbc.SybDriver:oracle.jdbc.driver.OracleDriver”;System.setProperty(“jdbc.drivers”, drivers);

//Pass driver names to be registered as command-line arguments.

//The following command is entered in one line.

Java-Djdbc.drivers=com.sybase.jdbc2.jdbc.SybDriver:oracle.jdbc.driver.OracleDrivercom.jdojo.jdbc.Test

Loading the Driver Class

You can create an object of the driver class. When the driver class is loaded in the JVM, it registers itself with theDriverManager. For a class to be loaded, you can use a Class.forName(“driver class name”) method or create anobject of the class as follows:

// Register the Oracle JDBC driver

neworacle.jdbc.driver.OracleDriver();

// Register the Oracle JDBC driver using the Class.forName() method.

// Exception handling has been omitted.

Class.forName(“oracle.jdbc.driver.OracleDriver”)

// Register the Java DB embedded driver

neworg.apache.derby.jdbc.EmbeddedDriver();

// Register the Java DB network client driver

neworg.apache.derby.jdbc.ClientDriver();

You do not need to keep the reference of the driver object because the goal is to load the driver class in the JVM.

When the driver’s class is loaded into the JVM, the static initializer of the driver’s class is executed in which the driver

class registers itself with the DriverManager.

Copyright 1999- Ducat Creative, All rights reserved.