Java DriverManager Class

Last Updated : 8 Apr 2026

DriverManager is an important part of JDBC that helps Java applications connect to databases. It manages database drivers and makes it easy to establish connections.

In this chapter, you will learn about the DriverManager class, how it works, and how it is used to connect Java applications with databases.

What is DriverManager Class in Java?

The DriverManager class is a part of the JDBC API and belongs to the java.sql package. It acts as a bridge between the Java application and database drivers.

It keeps track of all available drivers and helps in establishing a connection between the application and the correct database. It also provides methods to register and deregister drivers and to create connections.

Before connecting to a database, the driver must be registered; otherwise, an exception will occur.

Importing / Using DriverManager Class

To use the DriverManager class in a program, we need to import it from the java.sql package. This package contains all the classes and interfaces required for database connectivity.

Here is the syntax to import DriverManager class:

In most cases, we use:

This imports all JDBC classes, including DriverManager, Connection, Statement, and ResultSet, making it easier to work with databases.

Methods of the DriverManager Class

The following table lists the important methods of the DriverManager class used to manage database drivers and establish connections.

MethodDescription
public static synchronized void registerDriver(Driver driver):is used to register the given driver with DriverManager. No action is performed by the method when the given driver is already registered.
public static synchronized void deregisterDriver(Driver driver):is used to deregister the given driver (drop the driver from the list) with DriverManager. If the given driver has been removed from the list, then no action is performed by the method.
public static Connection getConnection(String url) throws SQLException:is used to establish the connection with the specified url. The SQLException is thrown when the corresponding Driver class of the given database is not registered with the DriverManager.
public static Connection getConnection(String url,String userName,String password) throws SQLException:is used to establish the connection with the specified url, username, and password. The SQLException is thrown when the corresponding Driver class of the given database is not registered with the DriverManager.
public static Driver getDriver(String url)Those drivers that understand the mentioned URL (present in the parameter of the method) are returned by this method provided those drivers are mentioned in the list of registered drivers.
pubic static int getLoginTimeout()The duration of time a driver is allowed to wait in order to establish a connection with the database is returned by this method.
pubic static void setLoginTimeout(int sec)The method provides the time in seconds. sec mentioned in the parameter is the maximum time that a driver is allowed to wait in order to establish a connection with the database. If 0 is passed in the parameter of this method, the driver will have to wait infinitely while trying to establish the connection with the database.
public static Connection getConnection(String URL, Properties prop) throws SQLExceptionA connection object is returned by this method after creating a connection to the database present at the mentioned URL, which is the first parameter of this method. The second parameter, which is "prop", fetches the authentication details of the database (username and password.). Similar to the other variation of the getConnection() method, this method also throws the SQLException, when the corresponding Driver class of the given database is not registered with the DriverManager.

Example of DriverManager Class

The following example demonstrates a real-world example where a Java application connects to a MySQL database and fetches student details.

Output:

ID: 1
Name: Rahul
ID: 2
Name: Priya
ID: 3
Name: Amit