DriverManager Driver Registration: Class.forName() Usage in Java 6 and Beyond
Question:
Since Java 6, is Class.forName(JDBC_DRIVER) no longer required for JDBC driver registration? Despite documentation suggesting that the jdbc.drivers system property should provide the driver path, printing this property returns null. Why does the application still function correctly?
Answer:
The elimination of Class.forName() in Java 6 (and JDBC4) is unrelated to the jdbc.drivers property. Java introduced a "service provider" concept where compliant drivers can self-register with the JVM during startup.
Service Provider Registration:
Drivers that adhere to this standard place a services directory within their JAR file in the META-INF directory. This directory contains a text file named java.sql.Driver, which specifies the implementing class.
Driver Detection:
The JVM automatically detects and registers drivers implementing this interface. This eliminates the need for explicit registration using Class.forName() for compliant drivers.
App Functionality:
In the provided example, the application may still function correctly despite the null return value for System.getProperty("jdbc.drivers") if:
The above is the detailed content of Is Class.forName() Necessary for JDBC Driver Registration in Java 6 and Later?. For more information, please follow other related articles on the PHP Chinese website!