- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
- Related Articles
- Is it mandatory to register the driver while working with JDBC?
- How to get the properties of a driver using JDBC?
- Among all 4 JDBC driver types, when to use which driver?
- How many ways are there to register a driver in Java?
- Gecko driver, Selecting value from a dropdown list using Selenium
- How to add the JDBC MySQL driver to an Eclipse project?
- What is the command used to register gecko driver in Selenium?
- What is the MySQL JDBC driver connection string?
- How to find a network adapter driver version using PowerShell?
- How to get the list of all drivers registered with the DriverManager using JDBC?
- How to find the device driver version using PowerShell?
- What is Selenium Internet Explorer Driver or IE Driver?
- Using Selenium Web Driver to retrieve value of a HTML input.
- How to change user agent for Selenium driver?
- How do I pass options to the Selenium Chrome driver using Python?
