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

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

问题描述

JDBC版本4中的一个很棒的补充不需要通过调用 Class.forName 来显式加载
驱动程序。当您的应用程序第一次尝试连接数据库时, DriverManager 自动加载
中的驱动程序应用程序 CLASSPATH



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



我可以猜测,在解析连接URL时,无论驱动程序需要JDBC还是ODBC都可以但是如何能说出多个jdbc兼容的驱动程序,哪一个将被选择用于我使用的数据库? (让我说使用MySql,我需要MySql-Connector驱动程序)。

解决方案

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


当方法getConnection被调用,DriverManager将
尝试从
在初始化时加载的JDBC驱动程序中找到一个合适的驱动程序,并使用
显式地加载与当前应用程序相同的类加载器。 p>

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




  org.apache.derby.jdbc.EmbeddedDriver 

现在讨论你的问题。


如果
类路径中有多个驱动程序怎么办?


作为类加载器规则,如果已经加载,则不会由类加载器重新加载。


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.

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

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?

解决方案

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

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.

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

Now coming to your question.

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