What is the DriverManager class in Java?

Introduction

The DriverManager class in Java provides the basic service for managing a set of Java Database Connectivity (JDBC) drivers. It maintains a list of database drivers.

The DriverManager class is part of the java.sql package. It is a singleton class. Only one instance of this class is created in a JVM.

The DriverManager class is used as part of the JDBC API via the static getConnection() method.

The JDBC API is the standard for database-independent connectivity between the Java programming language and SQL databases and other tabular data sources, such as spreadsheets or flat files.

How it works

We can register different drivers with the DriverManager class using the registerDriver() method.

When we try to establish a connection with the database, DriverManager looks for a suitable driver from its list. We can search for drivers that are registered with DriverManager using the getDriver(String url) method. Here, url is the connection URL of the database we want to connect to.

If the driver is found, this method uses the driver to connect to the database. If multiple drivers are found, it uses the first driver in the list that is able to connect to the database.

The DriverManager class is used as a connection factory for establishing JDBC connections. This connection object represents a database link that allows us to run SQL queries and perform other database operations.

The DriverManager class is not thread-safe. Hence, we should not use it in a multi-threaded environment. We need to create a new instance of the DriverManager class in each thread.

Connecting to the database using driver manager
Connecting to the database using driver manager

The above sequence diagram shows the use of the DriverManager class in connecting to the database.

Configuring a custom DriverManager

The DriverManager class can be replaced with a custom implementation by setting the jdbc.driver property to the fully qualified name of the replacement class. Let's see the code snippet below:

System.setProperty("jdbc.driver", "com.example.MyDriverManager");

Methods of the DriverManager class

  • getDriver(String url): This accepts the database URL. It returns a java.sql.Driver object that represents the first driver from the list of loaded drivers that can connect to the given database URL.
  • getDrivers(): This returns an enumeration of all loaded drivers.
  • getConnection(String url, String user, String password): This accepts the database URL, username, and password as parameters. It returns a Connection object that represents a connection to the given database URL.
  • getConnection(String url, Properties info): This accepts the database URL and a java.util.Properties object that contains the username and password as parameters. It returns a Connection object that represents a connection to the given database URL.
  • getLoginTimeout(): This returns the maximum time in seconds that a driver will wait while attempting to connect to a database. By default, this timeout is set to 0, which means there is no timeout.
  • setLoginTimeout(int seconds): This sets the maximum time in seconds that a driver will wait while attempting to connect to a database.
  • getLogWriter(): This returns a PrintWriter object that is used by all drivers to log messages.
  • setLogWriter(PrintWriter out): This sets the PrintWriter object to be used by all drivers to log messages.
  • deregisterDriver(Driver driver): This deregisters the given driver with the DriverManager class. Drivers may be deregistered manually or automatically when they are no longer referenced.
  • deregisterDrivers(): This deregisters all the currently-loaded drivers with the DriverManager class.

Conclusion

In this shot, we learned about the working of the java.sql.DriverManager class in Java. We also discussed its various methods.

Free Resources