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

前端之家收集整理的这篇文章主要介绍了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.f2er.com/java/121828.html

猜你在找的Java相关文章