What is the purpose of 'Class.forName("MY_JDBC_DRIVER")'?
I understand that class loading is useful for load the class at runtime with its class name.
However while using JDBC in our project we know which driver we are going to use and mostly driver manager string is hard coded.
My question is: Why are we loading driver using Class.forName("JDBC_DRIVER")
here?
Why can't we go ahead adding the driver in class path? since we know which driver jar we are going to use.
I believe Class.forName(JDBC_DRIVER)
will load the Driver into DriverManager
. Is it the only reason?
Edit 1:
The DriverManager
API doc states that
As part of its(DriverManager) initialization, the DriverManager class will attempt to load the driver classes referenced in the "jdbc.drivers" system property.
Applications no longer need to explictly load JDBC drivers using
Class.forName()
. Existing programs which currently load JDBC drivers usingClass.forName()
will continue to work without modification.
Then when I use other than oracle driver; do I need to change the driver name string in system property?
Solution 1:
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.
Solution 2:
The Class.forName(JDBC_DRIVER) call will register your JDBC driver in the DriverManager, so you can address it by url, such as "jdbc:odbc:Database" and so on...
Usually the driver class has static initialization code like this, which is invoked on Class.forName():
public class Driver implements java.sql.Driver {
static {
try {
DriverManager.registerDriver(new Driver());
} catch (SQLException E) {
throw new RuntimeException("Can't register driver!");
}
}
}
You still have to put JDBC driver jar into the classpath.
As an alternative, you can use database specific DataSource, then you can declaratively specify the datasource type, for example in Spring context or in your Web server JNDI. Here is an example.
Solution 3:
Putting a class in the classpath is not sufficient to have it loaded by the class loader. And the driver class must be loaded to ensure that it's registered to the JDBC API. BTW, for Class.forName
to work, the driver class must be in the classpath.
Solution 4:
In many industrial applications, we would like to abstract the data access layer from the rest of code, by pulling such information in a form of a property file/configuration file. In those cases, we might need to use something as you have done.
For e.g. we can use the JDBC interface classes to write the code to access the database, where you could configure the properties by selecting which technology you want to use as a database (e.g. ojdbc driver, or mysql jdbc driver, etc.) In those cases you can load the class using such method.