1. Introduction
1.绪论
JDBC is a set of specifications defining the API and SPI parts of the contract for Java Database Connectivity. The standard defines the JDBC driver abstraction as the primary entry point to interact with a database.
JDBC是一套规范,定义了Java数据库连接合同的API和SPI部分。该标准将JDBC驱动程序抽象定义为与数据库交互的主要入口点。
In this tutorial, we’ll take a look at some of the basic steps needed to load JDBC drivers.
在本教程中,我们将看一下加载JDBC驱动程序所需的一些基本步骤。
2. JDBC Drivers
2.JDBC驱动程序
To connect to a database, we must get an instance of a JDBC driver.
为了连接到数据库,我们必须得到一个JDBC驱动程序的实例。
We can obtain it through the DriverManager by specifying the JDBC URL connection string. Such a URL contains the type of database engine, database name, hostname, and port, as well as other connection parameters that are specific to the database vendor.
我们可以通过DriverManager指定JDBC URL连接字符串来获得它。这样的URL包含了数据库引擎的类型、数据库名称、主机名和端口,以及其他针对数据库供应商的连接参数。
Using the connection string, we can obtain a database connection object, which is the foundational unit of communication with the database in JDBC:
使用连接字符串,我们可以获得一个数据库连接对象,它是JDBC中与数据库通信的基本单位。
Connection con = DriverManager.getConnection(
"jdbc:postgresql://localhost:21500/test?user=fred&password=secret&ssl=true");
How does the driver manager know which driver to use if the only indication is the specified URL?
如果唯一的指示是指定的URL,驱动管理器如何知道使用哪个驱动?
There may be many JDBC drivers on the classpath, so there must be a way to distinguish each driver uniquely.
在classpath上可能有许多JDBC驱动,所以必须有一种方法来独特地区分每个驱动。
3. Legacy Approach
3.遗留方法
Before JDBC version 4 and Java SE 1.6, there was no generic mechanism in the JVM that would enable services to be discovered and registered automatically. Because of that, a manual step was needed to load the JDBC driver class by name:
在 JDBC 第 4 版和 Java SE 1.6 之前,JVM 中没有任何通用机制可以使服务被自动发现和注册。正因为如此,所以需要一个手动步骤来加载JDBC驱动程序类的名称。
Class.forName("oracle.jdbc.driver.OracleDriver");
The class loading process triggers a static initialization routine that registers the driver instance with the DriverManager and associates this class with the database engine identifier, such as oracle or postgres.
类的加载过程触发了一个静态初始化例程,该例程向DriverManager注册驱动实例,并将该类与数据库引擎标识符相关联,例如oracle或postgres。
After the registration is complete, we can use this identifier inside the JDBC URL as jdbc:oracle.
注册完成后,我们可以在JDBC的URL里面使用这个标识符作为jdbc:oracle。
A typical driver registration routine will instantiate the driver instance and pass it over to the DriverManager.registerDriver method:
一个典型的驱动注册程序将实例化驱动实例,并将其传递给DriverManager.registerDriver方法。
public static void register() throws SQLException {
if (isRegistered()) {
throw new IllegalStateException("Driver is already registered. It can only be registered once.");
} else {
Driver registeredDriver = new Driver();
DriverManager.registerDriver(registeredDriver);
Driver.registeredDriver = registeredDriver;
}
}
The example above shows the Postgres JDBC driver registration with the DriverManager. It is triggered by the JVM as part of the static initializer.
上面的例子显示了Postgres JDBC驱动在DriverManager的注册。它是由JVM触发的,是静态初始化器的一部分。
It is possible to partially automate this step even with the legacy approach by setting the jdbc.drivers system property:
通过设置jdbc.drivers系统属性,即使使用传统方法,也可以部分地自动完成这一步骤。
java -Djdbc.drivers=oracle.jdbc.driver.OracleDriver
When this property is specified, the driver manager will automatically attempt to load the specified JDBC driver.
当此属性被指定时,驱动程序管理器将自动尝试加载指定的JDBC驱动程序。
4. JDBC 4 Approach
4.JDBC 4的方法
The problem of automatic service discovery was solved with Java 1.6 and the service provider mechanism. It enables service providers to declare their services by placing them under META-INF/services inside the JAR file containing the services.
自动服务发现的问题在Java 1.6和服务提供者机制中得到了解决。它使服务提供者能够通过将其服务放在包含服务的JAR文件内的META-INF/services下来声明其服务。
This mechanism registers the driver automatically so that the manual step to load the class is no longer necessary. However, even with the service provider in place, manual class loading will not cause a failure. It is perfectly legal to invoke driver loading explicitly with recent JVMs and JDBC 4 drivers.
这个机制自动注册了驱动程序,这样就不再需要手动加载类的步骤了。然而,即使有服务提供者的存在,手动加载类也不会导致失败。用最近的JVM和JDBC 4驱动显式调用驱动加载是完全合法的。
The service provider specification simply replaces manual class loading with a declarative approach. For example, the PostgreSQL JDBC driver has a single file under META-INF/services/. The file name is java.sql.Driver (which is a well-established convention for JDBC drivers). It contains the fully qualified class name of the JDBC driver, which, in this case, is org.postgresql.Driver.
服务提供者的规范只是用声明性的方法取代了手动加载类。例如,PostgreSQL JDBC驱动在META-INF/services/下有一个文件。文件名是java.sql.Driver(这是一个公认的JDBC驱动程序的惯例)。它包含了JDBC驱动程序的完全合格的类名,在这个例子中,它是org.postgresql.Driver。
5. Conclusion
5.总结
In this article, we’ve reviewed basic concepts around JDBC, as well as various methods to load JDBC drivers, with an explanation of each approach.
在这篇文章中,我们回顾了围绕JDBC的基本概念,以及加载JDBC驱动程序的各种方法,并对每种方法进行了解释。
As usual, the complete source code for the article is available over on GitHub.
像往常一样,该文章的完整源代码可在GitHub上获得。