Skip to content
Advertisement

Class.forName(JDBC_DRIVER) no longer needed?

I’ve read here on SO that since java 6 you no longer need to register JDBC Driver using:

Class.forName(JDBC_DRIVER);

because DriverManager uses the path located in system property “jdbc.drivers” to retrieve the correct driver.

But when I do the followng:

System.out.print(System.getProperty("jdbc.drivers"));

null gets printed.

Do you have any clue why my app works correctly ?? 😉

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement