Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JDBC Class.forName vs DriverManager.registerDriver

Tags:

java

jdbc

Which is the difference from forName method vs registerDriver to load and register a JDBC driver?

like image 387
xdevel2000 Avatar asked Mar 30 '11 09:03

xdevel2000


People also ask

What does class forName do in JDBC?

Use the Class. forName() method to load the driver. The forName() method dynamically loads a Java class at runtime. When an application calls the forName() method, the Java Virtual Machine (JVM) attempts to find the compiled form (the bytecode) that implements the requested class.

What is DriverManager in JDBC?

DriverManager manages the set of Java Database Connectivity (JDBC) drivers that are available for an application to use. Applications can use multiple JDBC drivers concurrently if necessary. Each application specifies a JDBC driver by using a Uniform Resource Locator (URL).

Why do we use class forName com mysql JDBC driver?

It will create a new instance of the com. mysql. jdbc. Driver class and hence call the static initialization which will register the driver with the DriverManager so you can create mysql connections based on the URL you use in the second line.

What is DriverManager getConnection in Java?

The getConnection(String url, Properties info) method of Java DriverManager class attempts to establish a connection to the database by using the given database url. The appropriate driver from the set of registered JDBC drivers is selected. Properties are implementation-defined as to which value will take precedence.


2 Answers

Class.forName() is not directly related to JDBC at all. It simply loads a class.

Most JDBC Driver classes register themselves in their static initializers by calling registerDriver().

registerDriver() is the real call that you hardly ever need to call yourself (unless you write your own JDBC driver).

Note that in JDBC 4 you should not need either of those if your JDBC driver is up-to-date, as drivers can be found using the service location mechanisms instead (i.e. simply leave out that call and open your connection as usual). See the documentaton of DriverManager for details:

The DriverManager methods getConnection and getDrivers have been enhanced to support the Java Standard Edition Service Provider mechanism. JDBC 4.0 Drivers must include the file META-INF/services/java.sql.Driver. This file contains the name of the JDBC drivers implementation of java.sql.Driver. For example, to load the my.sql.Driver class, the META-INF/services/java.sql.Driver file would contain the entry:

my.sql.Driver 

Applications no longer need to explictly load JDBC drivers using Class.forName(). Existing programs which currently load JDBC drivers using Class.forName() will continue to work without modification.

like image 133
Joachim Sauer Avatar answered Oct 08 '22 05:10

Joachim Sauer


Never call DriverManager.registerDriver() method manually. The JDBC spec requires a driver to register itself when the class is loaded, and the class is loaded via Class.forName(). In JDBC 4 the drivers are able to be loaded automatically just by being on the class path.

DriverManager.registerDriver() manually is potentially dangerous since it actually causes the Driver to be registered twice. If your code requires you to deregister a Driver to prevent a memory leak then you would only end up deregistering it once and leave a second instance registered.

like image 32
David O'Meara Avatar answered Oct 08 '22 05:10

David O'Meara