微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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?

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

原文地址:https://www.jb51.cc/java/121828.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐