How to de-register a driver from driver manager’s drivers list using JDBC?


The java.sql.DriverManager class manages JDBC drivers in your application. This class maintains a list of required drivers and load them whenever it is initialized.

Therefore, you need to register the driver class before using it. However, you need to do it only once per application.

You can register a new Driver class in two ways −

  • Using the registerDriver() method of the DriverManager class. To this method you need to pass the Driver object as a parameter.
//Instantiating a driver class Driver driver = new com.mysql.jdbc.Driver();
//Registering the Driver DriverManager.registerDriver(driver);
  • Using the forName() method of the class named Class. To this method you need to pass the name of the Driver as a String parameter.
Class.forName("com.mysql.jdbc.Driver");

Deregistering a Driver

You can remove a particular Driver from the DriverManager’s list using its deregisterDriver() method.

If you invoke this method by passing the object of the required Driver class, the DriverManager simply drops the specified driver from its list.

DriverManager.deregisterDriver(mySQLDriver);

Following JDBC program establishes a connection with MySQL database, displays all the drivers registered with the DriverManager class, de-registers MySQL Driver and, displays the list again.

Example

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.util.Enumeration;
public class DeRegistering_Driver {
   public static void main(String args[])throws Exception {
      //Instantiating a Driver class
      Driver mySQLDriver = new com.mysql.jdbc.Driver();
      //Registering the Driver
      DriverManager.registerDriver(mySQLDriver);
      //Getting the connection
      String mysqlUrl = "jdbc:mysql://localhost/sampledatabase";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established....... ");
      System.out.println();
      System.out.println("List of all the Drivers registered with the DriverManager: ");
      //Retrieving the list of all the Drivers
      Enumeration<Driver> e = DriverManager.getDrivers();
      //Printing the list
      while(e.hasMoreElements()) {
         System.out.println(e.nextElement().getClass());
      }
      System.out.println();
      //De-registering the MySQL Driver
      DriverManager.deregisterDriver(mySQLDriver);
      System.out.println("List of all the Drivers after de-registration:");
      e = DriverManager.getDrivers();
      //Printing the list
      while(e.hasMoreElements()) {
         System.out.println(e.nextElement().getClass());
      }
      System.out.println();
   }
}

Since we have removed the driver from the DriverManager’s list you will not find the name of the MySQL driver in the list second time.

Output

Connection established.......
List of all the Drivers registered with the DriverManager:
class oracle.jdbc.OracleDriver
class org.sqlite.JDBC
class org.apache.derby.jdbc.AutoloadedDriver
class org.apache.derby.jdbc.ClientDriver
class org.hsqldb.jdbc.JDBCDriver
class net.ucanaccess.jdbc.UcanaccessDriver
class com.mysql.jdbc.Driver
List of all the Drivers after de-registration:
class oracle.jdbc.OracleDriver
class org.sqlite.JDBC
class org.apache.derby.jdbc.AutoloadedDriver
class org.apache.derby.jdbc.ClientDriver
class org.hsqldb.jdbc.JDBCDriver
class net.ucanaccess.jdbc.UcanaccessDriver

Updated on: 30-Jul-2019

409 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements