不再需要Class.forName(JDBC_DRIVER)? [英] Class.forName(JDBC_DRIVER) no longer needed?
问题描述
我在这里读过,因为java 6你不再需要使用以下命令注册JDBC驱动程序:
Class。的forName(JDBC_DRIVER);
因为DriverManager使用系统属性中的路径jdbc.drivers
来检索正确的驱动程序。
但是当我执行以下操作时:
是System.out.print(System.getProperty( jdbc.drivers中));
null
被打印。
你有什么线索为什么我的应用程序正常工作? ;)
这与该系统属性无关。 Java6(和JDBC4)引入了一个名为服务提供商,JVM在启动期间可以检测到已知接口的实现。符合该标准的驱动程序将由DriverManager自动注册。这就是 Class.forName()
不再需要的原因 - 但仅限于驱动程序支持的情况。
服务如果在META-INF目录内的驱动程序的jar文件中有 services
目录,则启动注册。该目录需要包含一个文本文件,其中包含在包含实现类的 java.sql.Driver
的JDBC驱动程序的情况下实现的接口的名称。 / p>
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 ?? ;)
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.
这篇关于不再需要Class.forName(JDBC_DRIVER)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!