热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

驱动程序类如何位于JDBC4中-HowisdriverclasslocatedinJDBC4

Oneofthegreatadditionsinversion4ofJDBCYoudonthavetoexplicitlyloadthedriverbycall

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.

在JDBC的第4版中,您不必通过调用类显式地加载驱动程序。forName了。当应用程序第一次尝试连接数据库时,DriverManager会自动加载在应用程序类路径中找到的驱动程序。

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?

我能猜到的一件事是,在解析连接URL时,是否需要JDBC驱动程序或ODBC驱动程序,是可以算出来的,但是如何从多个兼容JDBC的驱动程序中选择一个驱动程序,用于我正在使用的数据库呢?(假设我正在使用MySql,我需要sql - connector driver)。JVM中有这种数据库驱动程序的静态映射吗?

3 个解决方案

#1


10  

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

关于JDBC4驱动程序加载的一些信息来自:http://www.onjava.com/2006/08/02/jdbc -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.

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

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:

驱动管理器方法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。驱动程序文件将包含以下条目:

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.

作为类装入器规则,首先找到的任何类都将被装入,如果它已经装入,那么类装入器将不会重新装入。

#2


10  

Every JDBC 4 compliant driver has a file in its jar called META-INF/services/java.sql.Driver, in that file it will list its implementation(s) of java.sql.Driver. When you request a connection, DriverManager will use the ServiceLoader to find all(!) copies of META-INF/services/java.sql.Driver in the classpath and will then load all classes listed. When a java.sql.Driver class is loaded, it has to register itself with the DriverManager, so the DriverManager loads all classes using the service loader, and each Driver implementation registers itself.

每个兼容JDBC 4的驱动程序在其jar中都有一个名为META-INF/services/java.sql的文件。在该文件中,它将列出java.sql.Driver的实现。当您请求一个连接时,DriverManager将使用ServiceLoader来找到META-INF/services/java.sql的所有(!)副本。驱动程序在类路径中,然后将加载列出的所有类。当一个java.sql。驱动程序类被加载,它必须向驱动程序管理器注册自己,因此驱动程序管理器使用服务加载器加载所有类,每个驱动程序实现注册自己。

When you request a connection from DriverManager, the DriverManager will iterate over all registered drivers asking them for a Connection. The driver will use the JDBC url to check if its a protocol it supports (eg Jaybird/Firebird JDBC checks if the url starts with "jdbc:firebirdsql:" or "jdbc:firebird:"). If the driver does not support the protocol, it will return null, if it does support the protocol it will either return an established connection, or it will throw an SQLException (eg if you made an error in the URL, or it couldn't connect). If all drivers return null (none support the protocol), then DriverManager will throw an SQLException with error "No suitable driver found for "

当您从驱动管理器请求连接时,驱动管理器将遍历所有请求连接的已注册驱动程序。驱动程序将使用JDBC url检查它是否支持它的协议(例如Jaybird/Firebird JDBC检查url是否以“JDBC:firebirdsql:”或“JDBC: Firebird:”开头)。如果驱动程序不支持协议,它将返回null,如果它支持协议,它将返回一个已建立的连接,或者它将抛出一个SQLException(例如,如果URL出错,或者它无法连接)。如果所有的驱动程序都返回null(不支持该协议),那么DriverManager将会抛出一个带有错误的SQLException,“不适合在 中找到合适的驱动程序”。

So having multiple drivers on the classpath does not matter as long as they support different protocols, however if there are multiple drivers for the same database (or at least: same protocol prefixes), it will use the first in the list of drivers, and if that driver fails with an SQLException, it will not try the other one.

所以有多个驱动程序类路径无关紧要,只要他们支持不同协议,但是如果有多个驱动程序相同的数据库(或者至少:协议前缀相同),它将使用列表中的第一个司机,如果司机没有抛出SQLException,它不会尝试另一个。

#3


0  

In JDBC 4, the drivers are registered automatically. How this happens? When we write

在JDBC 4中,驱动程序是自动注册的。这是如何发生的?当我们写

Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sonoo","root","root");  

this will call the static blocks inside the DriverManager class. If you open the DriverManager source file, you can see the static block

这将调用DriverManager类中的静态块。如果打开DriverManager源文件,您可以看到静态块

 static {
        loadInitialDrivers();
        println("JDBC DriverManager initialized");
    }

this will call the function loadInitialDrivers(); function and load all the drivers and registered automatically. So we don't need to explicitly register or load the drivers.

这将调用函数loadInitialDrivers();运行并加载所有驱动程序并自动注册。所以我们不需要显式地注册或加载驱动程序。


推荐阅读
  • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
  • 基于PgpoolII的PostgreSQL集群安装与配置教程
    本文介绍了基于PgpoolII的PostgreSQL集群的安装与配置教程。Pgpool-II是一个位于PostgreSQL服务器和PostgreSQL数据库客户端之间的中间件,提供了连接池、复制、负载均衡、缓存、看门狗、限制链接等功能,可以用于搭建高可用的PostgreSQL集群。文章详细介绍了通过yum安装Pgpool-II的步骤,并提供了相关的官方参考地址。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 关于我们EMQ是一家全球领先的开源物联网基础设施软件供应商,服务新产业周期的IoT&5G、边缘计算与云计算市场,交付全球领先的开源物联网消息服务器和流处理数据 ... [详细]
  • 本文介绍了在Mac上搭建php环境后无法使用localhost连接mysql的问题,并通过将localhost替换为127.0.0.1或本机IP解决了该问题。文章解释了localhost和127.0.0.1的区别,指出了使用socket方式连接导致连接失败的原因。此外,还提供了相关链接供读者深入了解。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • 本文介绍了Java工具类库Hutool,该工具包封装了对文件、流、加密解密、转码、正则、线程、XML等JDK方法的封装,并提供了各种Util工具类。同时,还介绍了Hutool的组件,包括动态代理、布隆过滤、缓存、定时任务等功能。该工具包可以简化Java代码,提高开发效率。 ... [详细]
  • Android Studio Bumblebee | 2021.1.1(大黄蜂版本使用介绍)
    本文介绍了Android Studio Bumblebee | 2021.1.1(大黄蜂版本)的使用方法和相关知识,包括Gradle的介绍、设备管理器的配置、无线调试、新版本问题等内容。同时还提供了更新版本的下载地址和启动页面截图。 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 本文讨论了在数据库打开和关闭状态下,重新命名或移动数据文件和日志文件的情况。针对性能和维护原因,需要将数据库文件移动到不同的磁盘上或重新分配到新的磁盘上的情况,以及在操作系统级别移动或重命名数据文件但未在数据库层进行重命名导致报错的情况。通过三个方面进行讨论。 ... [详细]
  • CentOS 6.5安装VMware Tools及共享文件夹显示问题解决方法
    本文介绍了在CentOS 6.5上安装VMware Tools及解决共享文件夹显示问题的方法。包括清空CD/DVD使用的ISO镜像文件、创建挂载目录、改变光驱设备的读写权限等步骤。最后给出了拷贝解压VMware Tools的操作。 ... [详细]
  • 本文讨论了在openwrt-17.01版本中,mt7628设备上初始化启动时eth0的mac地址总是随机生成的问题。每次随机生成的eth0的mac地址都会写到/sys/class/net/eth0/address目录下,而openwrt-17.01原版的SDK会根据随机生成的eth0的mac地址再生成eth0.1、eth0.2等,生成后的mac地址会保存在/etc/config/network下。 ... [详细]
  • 安装mysqlclient失败解决办法
    本文介绍了在MAC系统中,使用django使用mysql数据库报错的解决办法。通过源码安装mysqlclient或将mysql_config添加到系统环境变量中,可以解决安装mysqlclient失败的问题。同时,还介绍了查看mysql安装路径和使配置文件生效的方法。 ... [详细]
author-avatar
踏山321
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有