Class.forName(JDBC_DRIVER) no longer needed?

后端 未结 2 1807
遥遥无期
遥遥无期 2020-12-02 00:42

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

Class.forName(JDBC_DRIVER);

because DriverManag

2条回答
  •  独厮守ぢ
    2020-12-02 00:54

    From the Javadocs of DriverManager:

    As part of its initialization, the DriverManager class will attempt to load the driver classes referenced in the "jdbc.drivers" system property. This allows a user to customize the JDBC Drivers used by their applications. For example in your ~/.hotjava/properties file you might specify:

    jdbc.drivers=foo.bah.Driver:wombat.sql.Driver:bad.taste.ourDriver
    

    This means that the system property need not be specified (as it says DriverManager will attempt). There is another mechanism through which drivers are automatically loaded, which relies on service loading since Java 6:

    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.

    Almost all JDBC drivers now conform to this requirement. Note that DriverManager does not internally fill the jdbc.drivers property, so it's still null.

提交回复
热议问题