驱动程序类如何位于 JDBC4 中 [英] How is driver class located in JDBC4

查看:32
本文介绍了驱动程序类如何位于 JDBC4 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JDBC 版本 4 中的一个重要新增功能您不必显式加载驱动程序不再调用 Class.forName.当您的应用程序第一次尝试连接数据库时,DriverManager 会自动加载在应用程序 CLASSPATH.

One of the great additions in version 4 of JDBC You don't have to explicitly load the driver by calling Class.forName anymore. When your application attempts to connect the database for the first time, DriverManager automatically loads the driver found in the application CLASSPATH.

我的问题是怎么样?如果类路径中有多个驱动程序怎么办?

我能猜到的一件事是,在解析连接 URL 时,可以弄清楚需要的驱动程序是 JDBC 还是 ODBC,但是如何从多个 jdbc 兼容驱动程序中说出哪一个将被选择用于我正在使用的数据库?(假设我正在使用 MySql 并且我需要 MySql-Connector 驱动程序).JVM 中是否有此类数据库驱动程序的静态映射?

One thing I can guess is that on parsing the connection URL whether driver needed is of JDBC or ODBC can be figured out but how can one say out of multiple jdbc compliant drivers which one is to be selected for the database I am using? (lets say I am using MySql and I need MySql-Connector driver). Is there any static mapping of such database drivers in JVM?

推荐答案

一些关于 JDBC4 驱动加载的信息取自:http://www.onjava.com/2006/08/02/jjdbc-4-enhancements-in-java-se-6.html

Some information about JDBC4 driver loading taken from : http://www.onjava.com/2006/08/02/jjdbc-4-enhancements-in-java-se-6.html

调用getConnection方法时,DriverManager会尝试从 JDBC 驱动程序中找到合适的驱动程序在初始化时加载和使用显式加载的与当前应用程序相同的类加载器.

When the method getConnection is called, the DriverManager will attempt to locate a suitable driver from among the JDBC drivers that were loaded at initialization and those loaded explicitly using the same class loader as the current application.

DriverManager 方法 getConnection 和 getDrivers 已经增强以支持 Java SE 服务提供者机制 (SPM).根据 SPM,服务被定义为一组众所周知的接口和抽象类,服务提供者是一个特定的服务的实施.它还指定服务提供者配置文件存储在 META-INF/services目录.JDBC 4.0 驱动程序必须包含该文件META-INF/services/java.sql.Driver.该文件包含JDBC 驱动程序对 java.sql.Driver 的实现.例如,加载连接到 Apache Derby 数据库的 JDBC 驱动程序,META-INF/services/java.sql.Driver 文件将包含以下内容条目:

The DriverManager methods getConnection and getDrivers have been enhanced to support the Java SE Service Provider mechanism (SPM). According to SPM, a service is defined as a well-known set of interfaces and abstract classes, and a service provider is a specific implementation of a service. It also specifies that the service provider configuration files are stored in the META-INF/services directory. 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. For example, to load the JDBC driver to connect to a Apache Derby database, the META-INF/services/java.sql.Driver file would contain the following entry:

org.apache.derby.jdbc.EmbeddedDriver

现在来回答你的问题.

我的问题是如何?如果有多个驱动程序怎么办类路径?

My question is how? What if there are multiple drivers in the classpath?

作为类加载器规则,首先找到的任何类都将被加载,如果它已经加载,则不会被类加载器重新加载.

As a class loader rule, any class found first will be loaded and if it is already loaded then will not be reloaded by the class loader.

这篇关于驱动程序类如何位于 JDBC4 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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