JDBC Class.forName 与 DriverManager.registerDriver [英] JDBC Class.forName vs DriverManager.registerDriver

查看:34
本文介绍了JDBC Class.forName 与 DriverManager.registerDriver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

forName 方法与 registerDriver 加载和注册 JDBC 驱动程序的区别是什么?

Which is the difference from forName method vs registerDriver to load and register a JDBC driver?

推荐答案

Class.forName() 与 JDBC 完全没有直接关系.它只是加载一个类.

Class.forName() is not directly related to JDBC at all. It simply loads a class.

大多数 JDBC 驱动程序类通过调用 registerDriver().

Most JDBC Driver classes register themselves in their static initializers by calling registerDriver().

registerDriver() 是您几乎不需要自己调用的真正调用(除非您编写自己的 JDBC 驱动程序).

registerDriver() is the real call that you hardly ever need to call yourself (unless you write your own JDBC driver).

请注意,在 JDBC 4 中,如果您的 JDBC 驱动程序是最新的,则您不应该需要任何一个,因为可以使用服务定位机制来找到驱动程序(即,只需省略呼叫并照常打开您的连接).请参阅文档DriverManager 详情:

Note that in JDBC 4 you should not need either of those if your JDBC driver is up-to-date, as drivers can be found using the service location mechanisms instead (i.e. simply leave out that call and open your connection as usual). See the documentaton of DriverManager for details:

DriverManager 方法 getConnectiongetDrivers 已得到增强,以支持 Java 标准版服务提供者机制.JDBC 4.0 驱动程序必须包含文件 META-INF/services/java.sql.Driver.此文件包含 java.sql.Driver 的 JDBC 驱动程序实现的名称.例如,要加载 my.sql.Driver 类,META-INF/services/java.sql.Driver 文件将包含条目:

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. For example, to load the my.sql.Driver class, the META-INF/services/java.sql.Driver file would contain the entry:

my.sql.Driver

应用程序不再需要使用 Class.forName() 显式加载 JDBC 驱动程序.当前使用 Class.forName() 加载 JDBC 驱动程序的现有程序将无需修改即可继续运行.

Applications no longer need to explictly load JDBC drivers using Class.forName(). Existing programs which currently load JDBC drivers using Class.forName() will continue to work without modification.

这篇关于JDBC Class.forName 与 DriverManager.registerDriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆