本文介绍了“Class.forName(“MY_JDBC_DRIVER")"的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道类加载对于在运行时加载类及其类名很有用.

I understand that class loading is useful for load the class at runtime with its class name.

然而,在我们的项目中使用 JDBC 时,我们知道我们将使用哪个驱动程序,并且大部分驱动程序管理器字符串是硬编码的.

However while using JDBC in our project we know which driver we are going to use and mostly driver manager string is hard coded.

我的问题是:为什么我们在这里使用 Class.forName("JDBC_DRIVER") 加载驱动程序?
为什么我们不能继续在类路径中添加驱动程序?因为我们知道要使用哪个驱动程序 jar.

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) 会将驱动程序加载到 DriverManager 中.这是唯一的原因吗?

I believe Class.forName(JDBC_DRIVER) will load the Driver into DriverManager. Is it the only reason?

编辑 1:

DriverManager API 文档指出

The DriverManager API doc states that

作为其(DriverManager)初始化的一部分,DriverManager 类将尝试加载jdbc.drivers"中引用的驱动程序类.系统属性.

应用程序不再需要使用 Class.forName() 显式加载 JDBC 驱动程序.当前使用 Class.forName() 加载 JDBC 驱动程序的现有程序将无需修改即可继续运行.

Applications no longer need to explictly load JDBC drivers using Class.forName(). Existing programs which currently load JDBC drivers using Class.forName() will continue to work without modification.

然后当我使用oracle以外的驱动程序时;我需要更改系统属性中的驱动程序名称字符串吗?

Then when I use other than oracle driver; do I need to change the driver name string in system property?

推荐答案

首先:使用现代 JDBC 驱动程序和当前的 JDK(至少是 Java 6),对 Class.forName() 的调用是不再需要.现在使用 定位 JDBC 驱动程序类a href="http://download.oracle.com/javase/7/docs/technotes/guides/jar/jar.html#Service%20Provider" rel="noreferrer">服务提供者机制.您应该能够简单地删除该调用并保持其余代码不变,它应该可以继续工作.

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 located using the service provider mechanism. You should be able to simply remove that call and leave the rest of the code unchanged and it should continue to work.

如果您没有使用当前的 JDK(或者如果您的 JDBC 驱动程序没有设置了适当的文件以使用该机制),则该驱动程序需要在DriverManager 使用 registerDriver.该方法通常从实际驱动程序类的静态初始化块调用,该类在第一次加载时被触发,因此发出 Class.forName() 确保驱动程序自行注册(如果尚未完成).

If you're not using a current JDK (or if you have a JDBC driver that does not have the appropriate files set up to use that mechanism) then the driver needs to be registered with the DriverManager using registerDriver. That method is usually called 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).

而且无论您使用 Class.forName() 还是新的服务提供者机制,您都总是需要类路径上的 JDBC 驱动程序(或通过某些ClassLoader 至少在运行时).

And no matter if you use Class.forName() or the new service provider mechanism, you will always need the JDBC driver on the classpath (or available via some ClassLoader at runtime, at least).

tl;dr:是的,Class.forName() 调用的唯一用途是确保驱动程序已注册.如果您使用当前的 JDK 和当前的 JDBC 驱动程序,则不再需要此调用.

tl;dr: yes, the only use 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.

这篇关于“Class.forName(“MY_JDBC_DRIVER")"的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 13:19