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

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

问题描述

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

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.

推荐答案

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

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驱动程序必须实现Driver接口,并且该实现必须包含一个静态初始化程序,该静态初始化程序将在驱动程序加载时被调用.此初始化程序在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

(来自JDBC 4.1,第9.2节)

(from JDBC 4.1, section 9.2)

但是,从JDBC 4.0开始,有一种注册驱动程序的新方法:JDBC驱动程序的jar需要包含文件/META-INF/services/java.sql.Driver,该文件包含该jar中java.sql.Driver实现的名称.使用DriverManager创建连接时,它将使用java.util.ServiceLoader枚举类路径中的所有/META-INF/services/java.sql.Driver文件并加载所有驱动程序,以便它们被注册.

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.

DriverManager.getConnection方法已得到增强,以支持Java Standard Edition Service Provider机制. JDBC 4.0驱动程序必须包含文件META-INF/services/java.sql.Driver.该文件包含JDBC驱动程序的java.sql.Driver实现的名称.

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.

(来自JDBC 4.1,第9.2.1节)

(from JDBC 4.1, section 9.2.1)

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

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.

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

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.

使用Class.forName("oracle.jdbc.driver.OracleDriver")从字面意义上加载驱动程序时,可能会感觉有些过时,但是如果您记住它也可能是从配置文件(或从用户输入)中提取的字符串,您可能会开始理解为什么非常强大.

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.

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

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天全站免登陆