java 'Class.forName("MY_JDBC_DRIVER")' 的目的是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7662902/
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
What is the purpose of 'Class.forName("MY_JDBC_DRIVER")'?
提问by Kanagavelu Sugumar
I understand that class loading is useful for load the class at runtime with its class name.
我知道类加载对于在运行时使用类名加载类很有用。
However while using JDBC in our project we know which driver we are going to use and mostly driver manager string is hard coded.
然而,在我们的项目中使用 JDBC 时,我们知道我们将使用哪个驱动程序,并且大多数驱动程序管理器字符串是硬编码的。
My question is: Why are we loading driver using Class.forName("JDBC_DRIVER")
here?
Why can't we go ahead adding the driver in class path? since we know which driver jar we are going to use.
我的问题是:为什么我们Class.forName("JDBC_DRIVER")
在这里使用加载驱动程序?
为什么我们不能继续在类路径中添加驱动程序?因为我们知道要使用哪个驱动程序 jar。
I believe Class.forName(JDBC_DRIVER)
will load the Driver into DriverManager
. Is it the only reason?
我相信Class.forName(JDBC_DRIVER)
会将驱动程序加载到DriverManager
. 这是唯一的原因吗?
Edit 1:
编辑1:
The DriverManager
API docstates that
As part of its(DriverManager) initialization, the DriverManager class will attempt to load the driver classes referenced in the "jdbc.drivers" system property.
Applications no longer need to explictly load JDBC drivers using
Class.forName()
. Existing programs which currently load JDBC drivers usingClass.forName()
will continue to work without modification.
作为其(DriverManager)初始化的一部分,DriverManager 类将尝试加载“jdbc.drivers”系统属性中引用的驱动程序类。
应用程序不再需要使用
Class.forName()
. 当前使用加载 JDBC 驱动程序的现有程序Class.forName()
将继续工作而无需修改。
Then when I use other than oracle driver; do I need to change the driver name string in system property?
然后当我使用 oracle 以外的驱动程序时;我需要更改系统属性中的驱动程序名称字符串吗?
回答by Joachim Sauer
First of: with modern JDBC drivers and a current JDK (at least Java 6) the call to Class.forName()
is no longer necessary. JDBC driver classes are now locatedusing the service provider mechanism. You shouldbe able to simply remove that call and leave the rest of the code unchanged and it should continue to work.
首先:使用现代 JDBC 驱动程序和当前的 JDK(至少是 Java 6),Class.forName()
不再需要调用。JDBC 驱动程序类现在使用服务提供者机制定位。您应该能够简单地删除该调用并保持其余代码不变,它应该可以继续工作。
If you're not using a current JDK (or if you have a JDBC driver that does nothave the appropriate files set up to use that mechanism) then the driver needs to be registered with the DriverManager
using registerDriver
. That method is usuallycalled from the static initializer block of the actual driver class, which gets triggered when the class is first loaded, so issuing the Class.forName()
ensures that the driver registers itself (if it wasn't already done).
如果你不使用当前的JDK(或者如果您有没有一个JDBC驱动程序不具有设置为使用该机制将相应的文件),则驱动程序需要与注册DriverManager
使用registerDriver
。该方法通常从实际驱动程序类的静态初始化程序块调用,该类在第一次加载时触发,因此发出Class.forName()
确保驱动程序自行注册(如果尚未完成)。
And no matter if you use Class.forName()
or the new service provider mechanism, you will alwaysneed the JDBC driver on the classpath (or available via some ClassLoader
at runtime, at least).
并且无论您是否使用Class.forName()
新的服务提供者机制,您将始终需要类路径上的 JDBC 驱动程序(或ClassLoader
至少在运行时通过某些程序可用)。
tl;dr: yes, the onlyuse of that Class.forName()
call is to ensure the driver is registered. If you use a current JDK and current JDBC drivers, then this call should no longer be necesary.
tl;dr:是的,该调用的唯一用途Class.forName()
是确保驱动程序已注册。如果您使用当前的 JDK 和当前的 JDBC 驱动程序,则不再需要此调用。
回答by Eugene Kuleshov
The Class.forName(JDBC_DRIVER) call will register your JDBC driver in the DriverManager, so you can address it by url, such as "jdbc:odbc:Database" and so on...
Class.forName(JDBC_DRIVER) 调用将在DriverManager 中注册您的JDBC 驱动程序,因此您可以通过url 对其进行寻址,例如“jdbc:odbc:Database”等...
Usually the driver class has static initialization code like this, which is invoked on Class.forName():
通常驱动类有这样的静态初始化代码,它在 Class.forName() 上调用:
public class Driver implements java.sql.Driver {
static {
try {
DriverManager.registerDriver(new Driver());
} catch (SQLException E) {
throw new RuntimeException("Can't register driver!");
}
}
}
You still have to put JDBC driver jar into the classpath.
您仍然必须将 JDBC 驱动程序 jar 放入类路径。
As an alternative, you can use database specific DataSource, then you can declaratively specify the datasource type, for example in Spring context or in your Web server JNDI. Here is an example.
作为替代方案,您可以使用特定于数据库的 DataSource,然后您可以声明性地指定数据源类型,例如在 Spring 上下文中或在您的 Web 服务器 JNDI 中。这是一个例子。
回答by JB Nizet
Putting a class in the classpath is not sufficient to have it loaded by the class loader. And the driver class must be loaded to ensure that it's registered to the JDBC API. BTW, for Class.forName
to work, the driver class mustbe in the classpath.
将类放在类路径中不足以让类加载器加载它。并且必须加载驱动程序类以确保它已注册到 JDBC API。顺便说一句,为了Class.forName
工作,驱动程序类必须在类路径中。
回答by Jay Patel - PayPal
In many industrial applications, we would like to abstract the data access layer from the rest of code, by pulling such information in a form of a property file/configuration file. In those cases, we might need to use something as you have done.
在许多工业应用中,我们希望通过以属性文件/配置文件的形式提取此类信息,从其余代码中抽象出数据访问层。在这些情况下,我们可能需要像您一样使用某些东西。
For e.g. we can use the JDBC interface classes to write the code to access the database, where you could configure the properties by selecting which technology you want to use as a database (e.g. ojdbc driver, or mysql jdbc driver, etc.) In those cases you can load the class using such method.
例如,我们可以使用 JDBC 接口类编写访问数据库的代码,您可以通过选择要用作数据库的技术(例如 ojdbc 驱动程序或 mysql jdbc 驱动程序等)来配置属性。在这种情况下,您可以使用此类方法加载类。