Java Class.forName,JDBC连接加载驱动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18058714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Class.forName, JDBC connection loading driver
提问by Selvaraj
While doing a simple JDBC connection, all the resources gives the same code that
在做一个简单的 JDBC 连接时,所有的资源都给出了相同的代码
String driver = "com.mysql.jdbc.Driver";
Statement statement = null;
Class.forName(driver);
Connection conn = DriverManager.getConnection(url + dbName,userName, password);
But we actually nothing do with "Class.forName(driver)". We didn't stored it anywhere. What is the use of that as we nothing do with Class.forName(driver)'s return.
但我们实际上与“Class.forName(driver)”无关。我们没有把它存放在任何地方。这有什么用,因为我们与 Class.forName(driver) 的返回无关。
采纳答案by Bohemian
Class.forName()
attempts to load the named class. In early versions of JDBC, this was necessary as the Driver
class required the class to be loaded in this way. This hasn't been required for ages.
Class.forName()
尝试加载指定的类。在 JDBC 的早期版本中,这是必要的,因为Driver
类要求以这种方式加载类。多年来一直不需要这样做。
Leave out the call and nothing bad will happen.
不接电话,不会有什么不好的事情发生。
For some reason, tutorials and examples persist with the old way.
出于某种原因,教程和示例仍然采用旧方式。
The only tiny benefit of loading the class manually is that it tells you exactly what the problem is in case you haven't got the right class in the classpath.
手动加载类的唯一微小好处是,它可以准确地告诉您问题是什么,以防您在类路径中没有正确的类。
回答by Ravi Thapliyal
Class.forName("driver.class");
loads the specified JDBC driver. When the driver loads, it also registers itself with the DriverManager
. Hence, when you call DriverManager#getConnection()
you're able to establish the Connection
through the driver loaded before.
Class.forName("driver.class");
加载指定的 JDBC 驱动程序。当驱动程序加载时,它也会向DriverManager
. 因此,当您调用时,DriverManager#getConnection()
您可以Connection
通过之前加载的驱动程序来建立。
When the method getConnection is called, the DriverManager will attempt to locate a suitable driver from amongst those loaded at initialization and those loaded explicitly using the same classloader as the current applet or application.
当调用 getConnection 方法时,DriverManager 将尝试从初始化时加载的驱动程序和使用与当前小程序或应用程序相同的类加载器显式加载的驱动程序中找到合适的驱动程序。
回答by Mark Rotteveel
Using Class.forName(..)
loads the class. Most java.sql.Driver
implementations (using a static initializer) register themselves with the java.sql.DriverManager
implementation when the class is loaded. See section 9.2 in the JDBC 4.1 specification for details:
使用Class.forName(..)
加载类。大多数java.sql.Driver
实现(使用静态初始值设定项)java.sql.DriverManager
在类加载时向实现注册自己。有关详细信息,请参阅 JDBC 4.1 规范中的第 9.2 节:
JDBC drivers must implement the
Driver
interface, and the implementation must contain a static initializer that will be called when the driver is loaded. This initializer registers a new instance of itself with theDriverManager
,
JDBC 驱动程序必须实现该
Driver
接口,并且该实现必须包含一个静态初始化程序,该初始化程序将在加载驱动程序时调用。这个初始值设定项用DriverManager
,
After registration you can create a connection using that driver.
注册后,您可以使用该驱动程序创建连接。
However starting with JDBC 4.0 (Java 6), drivers compliant with the JDBC 4.0 specification no longer need to be loaded this way, as the DriverManager
itself will take care of locating and loading JDBC drivers using the ServiceLoader
mechanism. See section 9.2.1 of the JDBC 4.1 specification:
然而,从 JDBC 4.0 (Java 6) 开始,符合 JDBC 4.0 规范的驱动程序不再需要以这种方式加载,因为它DriverManager
本身将使用该ServiceLoader
机制负责定位和加载 JDBC 驱动程序。请参阅 JDBC 4.1 规范的第 9.2.1 节:
The
DriverManager.getConnection
method has been enhanced to support the Java Standard Edition Service Provider mechanism. JDBC 4.0 Drivers must include the fileMETA-INF/services/java.sql.Driver
. This file contains the name of the JDBC driver's implementation ofjava.sql.Driver
.
该
DriverManager.getConnection
方法已得到增强以支持 Java Standard Edition Service Provider 机制。JDBC 4.0 驱动程序必须包含文件META-INF/services/java.sql.Driver
. 此文件包含 JDBC 驱动程序实现的名称java.sql.Driver
。
回答by Mark Bramnik
Well, it does have one side effect - it loads the driver specified by a string name into the memory.
嗯,它确实有一个副作用——它将由字符串名称指定的驱动程序加载到内存中。
In Java the classes are loaded only when they're actually needed to be used.
在 Java 中,类仅在实际需要使用时才加载。
So Class.forName()
will cause the class loader to "read" the bytecode and load up the class definition into the memory of the JVM.
所以Class.forName()
会导致类加载器“读取”字节码并将类定义加载到JVM的内存中。
Now when this happens, the static initialization block of this class (and Drivers should have one) is executed (it should be static because we don't really create objects of this class).
现在当这种情况发生时,这个类的静态初始化块(驱动程序应该有一个)被执行(它应该是静态的,因为我们并没有真正创建这个类的对象)。
This static initialization block written so that it registers the driver in the DriverManager.
编写此静态初始化块以便它在 DriverManager 中注册驱动程序。
This is a 'by the book' explanation. Of course this API is not that clear and not obvious. Its possible to do this explicitly:
这是“按书本”的解释。当然,这个 API 不是那么清楚,也不是很明显。可以明确地做到这一点:
Driver driver = (Driver)Class.forName("com.mysql.jdbc.Drivercom.mysql.jdbc.Driver").newInstance();
DriverManager.registerDriver(driver);
Since Java 6 this mechanism should not be used anymore. Read hereabout the new way to load up the driver.
从 Java 6 开始,不应再使用此机制。在此处阅读有关加载驱动程序的新方法。
Hope this helps
希望这可以帮助
回答by PSR
The class.forName() causes the ClassLoader to load the class into memory. JDBC driver classes contain a static initializer block that registers the driver with DriverManager for later reference. When you connect DriverManager uses the database parameter to look up the right driver
class.forName() 使 ClassLoader 将类加载到内存中。JDBC 驱动程序类包含一个静态初始化程序块,该块将驱动程序注册到 DriverManager 以供以后参考。连接时 DriverManager 使用数据库参数查找正确的驱动程序
回答by Evgeniy Dorofeev
This is the part of com.mysql.jdbc.Driver which registers itself with DriverManager
这是 com.mysql.jdbc.Driver 的一部分,它向 DriverManager 注册自己
//
// Register ourselves with the DriverManager
//
static {
try {
java.sql.DriverManager.registerDriver(new Driver());
} catch (SQLException E) {
throw new RuntimeException("Can't register driver!");
}
}
It should be noted that since ver 1.6 there is no need to explictly load JDBC drivers using Class.forName(), DriverManager can detect JDBC 4.0 Drivers automatically using Service Provider mechanism. JDBC drivers class name is writen in META-INF/services/java.sql.Driver file
需要注意的是,从1.6版开始,不需要使用Class.forName()显式加载JDBC驱动,DriverManager可以使用Service Provider机制自动检测JDBC 4.0驱动。JDBC 驱动类名写在 META-INF/services/java.sql.Driver 文件中