What Is the Purpose of 'Class.Forname("My_Jdbc_Driver")'

What is the purpose of 'Class.forName(MY_JDBC_DRIVER)'?

First of: with modern JDBC drivers and a current JDK (at least Java 6) the call to Class.forName() is no longer necessary. JDBC driver classes are now located using the service provider mechanism. You should be able to simply remove that call and leave the rest of the code unchanged and it should continue to work.

If you're not using a current JDK (or if you have a JDBC driver that does not have the appropriate files set up to use that mechanism) then the driver needs to be registered with the DriverManager using registerDriver. That method is usually called from the static initializer block of the actual driver class, which gets triggered when the class is first loaded, so issuing the Class.forName() ensures that the driver registers itself (if it wasn't already done).

And no matter if you use Class.forName() or the new service provider mechanism, you will always need the JDBC driver on the classpath (or available via some ClassLoader at runtime, at least).

tl;dr: yes, the only use of that Class.forName() call is to ensure the driver is registered. If you use a current JDK and current JDBC drivers, then this call should no longer be necesary.

What is the actual use of Class.forName(oracle.jdbc.driver.OracleDriver) while connecting to a database?

It obtains a reference to the class object with the FQCN (fully qualified class name) oracle.jdbc.driver.OracleDriver.

It doesn't "do" anything in terms of connecting to a database, aside from ensure that the specified class is loaded by the current classloader. There is no fundamental difference between writing

Class<?> driverClass = Class.forName("oracle.jdbc.driver.OracleDriver");
// and
Class<?> stringClass = Class.forName("java.lang.String");

Class.forName("com.example.some.jdbc.driver") calls show up in legacy code that uses JDBC because that is the legacy way of loading a JDBC driver.

From The Java Tutorial:

In previous versions of JDBC, to obtain a connection, you first had to initialize your JDBC driver by calling the method Class.forName. This methods required an object of type java.sql.Driver. Each JDBC driver contains one or more classes that implements the interface java.sql.Driver.

...

Any JDBC 4.0 drivers that are found in your class path are automatically loaded. (However, you must manually load any drivers prior to JDBC 4.0 with the method Class.forName.)

Further reading (read: questions this is a dup of)

  • What purpose does Class.forName() serve if you don't use the return value?
  • How does Class.forName() work?
  • What does 'Class.forName("org.sqlite.JDBC");' do?
  • What is the purpose of 'Class.forName("MY_JDBC_DRIVER")'?
  • Loading JDBC driver

What Class.forName() method does for JDBC?

It gets the Class object represented by the given FQN. If not loaded previously, it also loads the class. This has the side effect of initializing the static class variables and running any static blocks.

With recent JDBC versions you don't need Class.forName() to load the driver anymore, with older driver versions it was and is required.

Why does calling Class.forName(com.mysql.jdbc.Driver) register the MySQL for JDBC?

It registers it because it loads the class into memory and runs the class's static initializers. The static initializer code then calls into the JDBC framework to say "Hi there, I'm a JDBC driver" (by calling DriverManager.registerDriver).

E.g., the driver class will look vaguely like this:

package com.example.jdbc;

import java.sql.DriverManager;

public class Driver implements java.sql.Driver {
static {
DriverManager.registerDriver(new Driver());
}

// ...implementation...
}

Then when you do Class.forName("com.example.jdbc.Driver"), it loads the class and runs the static initializer, which creates an instance and registers it with the DriverManager.


I should note that as Andreas says, modern JDBC drivers don't need you to do this.

Class.forName(JDBC_DRIVER) no longer needed?

That has nothing to do with that system property. Java6 (and JDBC4) introduced a concept known as "service provider" where implementations of known interface can be detected by the JVM during startup. A driver that is compliant with that will be registered by the DriverManager automatically. That's why Class.forName() is no longer necessary - but only if the driver supports that.

The service registration is initiated if there is a services directory in the driver's jar file inside the META-INF directory. That directory needs to contain a text file with the name of the interface that is implemented in the case of a JDBC driver that is java.sql.Driver containing the implementing class.

Loading a jdbc driver in a java class

This piece of code isn't really intended to create an instance of the driver but to ensure the driver class is loaded by the class loader. Nowadays this is no longer needed, see:

  • What is the actual use of Class.forName("oracle.jdbc.driver.OracleDriver") while connecting to a database?
  • JDBC Class.forName vs DriverManager.registerDriver
  • Loading JDBC driver
  • What is the purpose of 'Class.forName("MY_JDBC_DRIVER")'?
  • JDBC connection- Class.forName vs Class.forName().newInstance?


Related Topics



Leave a reply



Submit