加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 驱动程序类如何位于JDBC4中

发布时间:2020-12-14 05:38:25 所属栏目:Java 来源:网络整理
导读:JDBC的第4版中的一个很好的补充您不必显式加载 驱动程序通过调用Class.forName了.当您的应用程序第一次尝试连接数据库时,DriverManager会自动加载驱动程序 应用程序CLASSPATH. 我的问题是怎么回事?如果类路径中有多个驱动程序怎么办? 有一点我可以猜到,在
JDBC的第4版中的一个很好的补充您不必显式加载
驱动程序通过调用Class.forName了.当您的应用程序第一次尝试连接数据库时,DriverManager会自动加载驱动程序
应用程序CLASSPATH.

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

有一点我可以猜到,在解析连接URL时,无论驱动程序需要是JDBC还是ODBC都可以解决,但是如何从多个符合jdbc的驱动程序中说出哪些是为我正在使用的数据库进行选择? (让我说我正在使用MySql,我需要MySql-Connector驱动程序).在JVM中有这样的数据库驱动程序的静态映射吗?

解决方法

关于JDBC4驱动程序加载的一些信息取自: 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

现在来你的问题

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读