本文介绍了"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?

DriverManager API文档指出

The DriverManager API doc states that

应用程序不再需要使用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驱动程序类找到.一个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(或者如果您有 not 没有设置适当的文件以使用该机制的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