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

查看:189
本文介绍了JDBC Class.forName vs 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方法 getConnection getDrivers 已得到增强,可支持Java Standard Edition Service Provider机制。 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 vs DriverManager.registerDriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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