JDBC  «Prev  Next»
Lesson 6Using the JDBC DriverManager
ObjectiveUse the JDBC DriverManager to connect to the DBMS.

Using JDBC DriverManager

Before we can begin to use a JDBC driver, there is a little bit of housekeeping that needs to be performed. As is the case with any Java class that we intend to use, that class has to be loaded and instantiated. The java.sql.DriverManager class, sometimes called the DriverManager, provides an access layer that can be used on top of different database drivers and used with an application.
  • JDBC Connection Process
    The process for using the DriverManager to connect to the DBMS is two-fold. The first step is to load the appropriate driver class, so that the DriverManager can access it. This has to be done only once in the life of the application and must be completed before attempting the first connection.

Class.forName(database_engine_class).newInstance();

The second step is performed each time a new connection is needed.

Get connection using 1)url 2) uid 3) password
Get connection using 1)url 2) uid 3) password
Of course, the code is customized to include the specific JDBC URL, user ID, and password. This simple line of code creates an open connection ready to be used to access the database. The Slideshow below illustrates the JDBC DriverManager connection process and will prepare you for the exercise that follows this lesson.

JDBC DriverManager Connection

1) Load the JDBC Driver object by calling Class.forName(driver).newInstace();   In the main() method of your application; or better, in a static initializer block in one of your classes
1) Load the JDBC Driver object by calling Class.forName(driver).newInstace(); In the main() method of your application; or better, in a static initializer block in one of your classes.

2) To start the JDBC Connection Process, you need the database URL, user ID, and password.
2) To start the JDBC Connection Process, you need the database URL, user ID, and password.


3) Request a java.sql.Connection ojbect from the DriverManager. This request will lead to a search performed by the DriverManager
3) Request a java.sql.Connection object from the DriverManager. This request will lead to a search performed by the DriverManager

4) The DriverManager searchs through all known java.sql.Driver implementations for the one that connects with the URL you specified in step 1.
4) The DriverManager searchs through all known java.sql.Driver implementations for the one that connects with the URL you specified in step 1. The DriverManager keeps a list of classes that implement the java.sql.Driver Interface. JDBC requires the Driver class to register itself (or be registered)

JDBC Metadata
5) The DriverManager reports the results of the search. One of two things can occur: the DriverManager may or may not find a match.
5) The DriverManager reports the results of the search. One of two things can occur: the DriverManager may or may not find a match. If it does not find a match, DriverManager throws an exception back to your application.

6) If the DriverManager finds a driver that it can connect after your URL is recognized, the Driver creates a database connection using the properties specified in your code
6) If the DriverManager finds a driver that it can connect after your URL is recognized, the Driver creates a database connection using the properties specified in your code.

7) Once the database connection is created, the Driver provides the DriverManager with an instance of java.sql.Connection.
7) Once the database connection is created, the Driver provides the DriverManager with an instance of java.sql.Connection. This represents the database connection.

8) The Connection object is passed back to the application by the DriverManager.
8) The Connection object is passed back to the application by the DriverManager. The application can now use the Connection object to create Statement object to query the database.


The use of drivers

You have learned that JDBC achieves its functionality through a set of Java interfaces, each implemented differently by specific database vendors. JDBC includes JDBC drivers, which are a set of classes that implement the JDBC interfaces for a specific database engine.
Now that you have an overall understanding of the JDBC API package, let's turn to the subject of obtaining a database connection. specifically, loading a database driver and opening the connection.
Database Connection
The vendor and the network protocol define the implementation of a database connection. The interface, in the JDBC 2.0 core API, that represents the connection is:
java.sql.Connection
Interestingly, you can use different JDBC drivers in one application to obtain one or more connections for one or more databases. To accomplish this, just instantiate the appropriate database driver for each of the databases, such as:


Use the forName method to load the Driver
Use the forName method to load the Driver

View the code below to see a generic example of using the DriverManager to retrieve a connection object.
Connection con = DriverManager.getConnnection(url, "myUsername", "myPassword");

The two main elements related to obtaining a database connection are the JDBC URLs and the java.sql.DriverManager class, referred to as the DriverManager. You will learn how to modify and compile this code to make it work for your specific customer and user needs.
In the next lesson, you will learn how to put all these pieces together into a working program.

SEMrush Software