为什么我们在连接数据库时使用 Class.forName(“oracle.jdbc.driver.OracleDriver")? [英] Why we use Class.forName(“oracle.jdbc.driver.OracleDriver”) while connecting to a database?

查看:23
本文介绍了为什么我们在连接数据库时使用 Class.forName(“oracle.jdbc.driver.OracleDriver")?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

连接数据库时Class.forName("oracle.jdbc.driver.OracleDriver")的实际用途是什么?为什么我们不能只导入同一个类,而是为什么要加载它.

解决方案

使用 Class.forName() 背后的基本思想是加载 JDBC 驱动程序实现.(普通)JDBC 驱动程序必须包含一个静态初始化程序,它使用 java.sql.DriverManager 注册驱动程序实现的实例:

<块引用>

JDBC 驱动程序必须实现Driver 接口,并且该实现必须包含一个静态初始化程序,该初始化程序将在加载驱动程序时调用.这个初始化器向 DriverManager

注册了一个自己的新实例

(来自 JDBC 4.1,第 9.2 节)

从 JDBC 4.0 开始,有一种新的驱动程序注册方法:JDBC 驱动程序的 jar 文件需要包含一个文件 /META-INF/services/java.sql.Driver ,其中包含名称(s) 在那个 jar 中的 java.sql.Driver 实现.当您使用 DriverManager 创建连接时,它将使用 java.util.ServiceLoader 枚举所有 /META-INF/services/java.sql.Driver 文件并加载所有驱动程序以便注册.

<块引用>

DriverManager.getConnection 方法已得到增强,以支持 Java 标准版服务提供者机制.JDBC 4.0 驱动程序必须包含文件 META-INF/services/java.sql.Driver.此文件包含 java.sql.Driver 的 JDBC 驱动程序实现的名称.

(来自 JDBC 4.1,第 9.2.1 节)

以这种方式加载驱动程序的原因是它允许您将应用程序与其使用的驱动程序(和数据库)分离.这意味着您可以在没有任何驱动程序的情况下编写、编译甚至分发应用程序,您只需要使用java.sql(和javax.sql)中提供的接口包 - 它是 Java 的一部分 - 无需直接访问实现.

然后,应用程序的用户将有效的 JDBC 驱动程序添加到类路径(并配置诸如连接字符串之类的内容),以便应用程序实际上可以连接到数据库.在 JDBC 4.0 之前,用户必须指定驱动程序名称,以便应用程序可以使用 Class.forName 加载它,使用符合 JDBC 4.0 的驱动程序和 Java 6 或更高版本,此发现是自动的.

当你用 Class.forName("oracle.jdbc.driver.OracleDriver") 从字面上加载驱动程序时,它可能感觉有点矫枉过正,但如果你记住它也可能是一个字符串从配置文件(或用户输入)中提取,您可能会开始理解为什么它如此强大.

当然,这种驱动程序独立性不是 100%,尤其是当您的应用程序使用供应商特定的 SQL 时.但理论上您的应用程序可以独立于数据库.JDBC 还提供了一些额外的机制来解决这个问题,例如 JDBC 转义以提供驱动程序转换为特定语法的通用语法,以及 DatabaseMetaData 允许您发现特性、保留字等,允许您创建或生成兼容的查询.

What is the actual use of Class.forName("oracle.jdbc.driver.OracleDriver") while connecting to a database? Why cant we just import the same class, instead why we are loading it.

解决方案

The basic idea behind using Class.forName() is to load a JDBC driver implementation. A (normal) JDBC driver must contain a static initializer that registers an instance of the driver implementation with java.sql.DriverManager:

JDBC drivers must implement the Driver interface, and the implementation must contain a static initializer that will be called when the driver is loaded. This initializer registers a new instance of itself with the DriverManager

(from JDBC 4.1, section 9.2)

Since JDBC 4.0 however there is a new way to register drivers: the jar of a JDBC driver needs to include a file /META-INF/services/java.sql.Driver which contains the name(s) of the java.sql.Driver implementations in that jar. When you create a connection using the DriverManager, it will use java.util.ServiceLoader to enumerate all /META-INF/services/java.sql.Driver files in the classpath and load all drivers so they get registered.

The DriverManager.getConnection method has 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 driver’s implementation of java.sql.Driver.

(from JDBC 4.1, section 9.2.1)

The reasons drivers are loaded this way, is that it allows you to decouple an application from the driver (and database) it uses. This means that you can write, compile and even distribute an application without any drivers, you only need to use the interfaces provided in the java.sql (and javax.sql) package - which is part of Java - without needing to access the implementation directly.

The user of the application then adds a valid JDBC driver to the classpath (and configuring things like a connection string) so the application can actually to connect to a database. Before JDBC 4.0, the user would have to specify the driver name so that the application could load it using Class.forName, with a JDBC 4.0 compliant driver and Java 6 or higher this discovery is automatic.

When you load a driver literally with Class.forName("oracle.jdbc.driver.OracleDriver") it might feel like overkill, but if you keep in mind that it could also be a string pulled from a config file (or from user input) you might start to understand why it is so powerful.

Of course this driver independence is not 100%, especially not if your application uses vendor specific SQL. But the theory is that your application can be database independent. JDBC also provides some additional mechanisms to address this, eg JDBC escapes to provide a common syntax that the driver translates to the specific syntax, and DatabaseMetaData which allows you to discover features, reserved words etc which allow you to create or generate compatible queries.

这篇关于为什么我们在连接数据库时使用 Class.forName(“oracle.jdbc.driver.OracleDriver")?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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