Share on Google+Share on Google+

JDBC DriverManager Example

In this tutorial you will learn java.sql.DriverManager, And how to use it to create a connection to the database.

JDBC Driver Manager

DriverManager is a class of java.sql.*; package. Its main function is to manage the set of JDBC drivers. It is traditional management layer of JDBC   which works between user and Driver. Driver Manager keeps track of driver available and connection between database and driver. It maintains a list of driver classes that has registered them self by calling a method DriverManager.registerDriver. All the driver classes are written in static section, which creates an instance on class and register automatically with DriverManager automatically when it is loaded. Therefore user only call DriverManager, registerDriver is automatically called. You can register a driver manager in two ways

1. By calling the Class.forName("DriverName"); Which explicitly loads the river class and does not depends on external setup.

2.By loading the driver class to java.lang.System property jdbc.drivers, this is the list of driver class names which DriverManager loads. When the DriverManager class is initialized it looks for the driver name which user have entered and load that driver.

If the driver class is loaded and registered with DriverManager now it is ready for establishing connection. When DiverManager.getConnection("ConnectionURL"); is called the driver manager test each driver to establish a connection and finally establishes the connection with the appropriate driver.

Following is the example that loads the driver and establish a connection with MySql database.

onnection con=null;	

All the Methods of DriverManager class are static

A Simple Example of DriverManager is given below

To run this example you must create a databse in MySql database named student and create table of name student as,

CREATE TABLE student (
Name tinytext NOT NULL,
Course varchar(25) NOT NULL,
Address text  );

Then Run The following code


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

public class DriverManagerClaz {
	String driverName = "com.mysql.jdbc.Driver";
	Connection con = null;
	public DriverManagerClaz() {
		try {
			//Loading Driver for MySql
		} catch (ClassNotFoundException e) {
	public Connection createConnection() {
		try {
			String connectionUrl="jdbc:mysql://localhost:3306/student";
			String userName="root";
			String userPass="root";
			con = DriverManager
			System.out.println("******* Connection created successfully........");
			} catch (SQLException e) {
		return con;
	public void closeConnection(){
		System.out.println("******* Connection closed Successfully.........");
		}catch(Exception e){
	public static void main(String args[]){
		DriverManagerClaz con=new DriverManagerClaz();

When you run this application it will display message as shown below:

******* Connection created successfully........
******* Connection closed Successfully.........

Download this example code



Posted on: October 16, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.