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?

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

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...