There are two ways to load a driver:
加载驱动程序有两种方法:
Class.forName()
DriverManager.registerDriver()
Method 1 internally also calls DriverManager.registerDriver and method 1 is the preferred way.
方法1内部也调用DriverManager.registerDriver,方法1是首选方法。
But why? Is there any small difference or is performance etc. better?
Any views are appreciated..
但为什么?是否有任何小差异或性能等更好?任何意见都赞赏..
5 个解决方案
#1
3
If you use Class.forName(), then you are not required to have any compile-time dependencies on a particular JDBC driver. This is particularly useful when you are writing code that can work with a variety of databases.
如果使用Class.forName(),则不需要对特定JDBC驱动程序具有任何编译时依赖性。当您编写可以使用各种数据库的代码时,这尤其有用。
Consider the following code:
请考虑以下代码:
// Register the PostgreSQL driver
Class.forName("org.postgresql.Driver");
Now compare it to:
现在将它与:
import org.postgresql.Driver;
// Register the PostgreSQL driver
DriverManager.registerDriver(new Driver());
And consider that in the first example, the class name could also have come from a properties file, XML file, etc., depending on what's convenient for your application.
并且考虑到在第一个示例中,类名也可能来自属性文件,XML文件等,具体取决于您的应用程序的方便程度。
#2
2
The JDBC API Tutorial and Reference is the best reference for such questions, a section of which addresses the role played by the Driver and DriverManager classes.
JDBC API教程和参考是此类问题的最佳参考,其中一部分介绍了Driver和DriverManager类所扮演的角色。
All Driver classes are expected to have a static initializer that is responsible for creating an instance of that Driver, and register it with the DriverManager, when the Driver class is loaded.
所有驱动程序类都应该有一个静态初始化程序,负责创建该驱动程序的实例,并在加载Driver类时将其注册到DriverManager。
Additionally, the DriverManager.getConnection() is probably the only user-space friendly method in the class. Most of the other methods are usually not used by most developers using the JDBC API. So the old adage still stands - use Class.forName() to load the driver, and then use DriverManager.getConnection() to get a connection to the database.
此外,DriverManager.getConnection()可能是类中唯一的用户空间友好方法。大多数使用JDBC API的开发人员通常不使用大多数其他方法。所以旧的格言仍然存在 - 使用Class.forName()来加载驱动程序,然后使用DriverManager.getConnection()来获取与数据库的连接。
#3
1
I have to say, your life will be much easier if you construct a driver instance by statically reference the driver. Once you have that, you can ignore DriverManager
which made of evil.
我不得不说,如果通过静态引用驱动程序来构造驱动程序实例,那么您的生活会更容易。一旦你有了,你可以忽略由邪恶组成的DriverManager。
#4
1
Reading the JavaDoc it looks like Class.forName was required to start with, and then things changed so that it was no longer the prefered way (or the required way).
读取JavaDoc看起来像Class.forName需要从一开始,然后事情发生了变化,以至于它不再是首选的方式(或所需的方式)。
#5
1
"is performance etc. better?"
“表演等更好吗?”
I would say that performance for this one-time operation is the least of your worries.
我想说这次一次性操作的表现是你最不担心的。
If you're using a Java EE app server the answer is "neither". You should be setting up a connection pool and let it handle loading the driver and handing out the connections.
如果您使用的是Java EE应用服务器,那么答案就是“不”。您应该设置连接池并让它处理加载驱动程序并分发连接。
#1
3
If you use Class.forName(), then you are not required to have any compile-time dependencies on a particular JDBC driver. This is particularly useful when you are writing code that can work with a variety of databases.
如果使用Class.forName(),则不需要对特定JDBC驱动程序具有任何编译时依赖性。当您编写可以使用各种数据库的代码时,这尤其有用。
Consider the following code:
请考虑以下代码:
// Register the PostgreSQL driver
Class.forName("org.postgresql.Driver");
Now compare it to:
现在将它与:
import org.postgresql.Driver;
// Register the PostgreSQL driver
DriverManager.registerDriver(new Driver());
And consider that in the first example, the class name could also have come from a properties file, XML file, etc., depending on what's convenient for your application.
并且考虑到在第一个示例中,类名也可能来自属性文件,XML文件等,具体取决于您的应用程序的方便程度。
#2
2
The JDBC API Tutorial and Reference is the best reference for such questions, a section of which addresses the role played by the Driver and DriverManager classes.
JDBC API教程和参考是此类问题的最佳参考,其中一部分介绍了Driver和DriverManager类所扮演的角色。
All Driver classes are expected to have a static initializer that is responsible for creating an instance of that Driver, and register it with the DriverManager, when the Driver class is loaded.
所有驱动程序类都应该有一个静态初始化程序,负责创建该驱动程序的实例,并在加载Driver类时将其注册到DriverManager。
Additionally, the DriverManager.getConnection() is probably the only user-space friendly method in the class. Most of the other methods are usually not used by most developers using the JDBC API. So the old adage still stands - use Class.forName() to load the driver, and then use DriverManager.getConnection() to get a connection to the database.
此外,DriverManager.getConnection()可能是类中唯一的用户空间友好方法。大多数使用JDBC API的开发人员通常不使用大多数其他方法。所以旧的格言仍然存在 - 使用Class.forName()来加载驱动程序,然后使用DriverManager.getConnection()来获取与数据库的连接。
#3
1
I have to say, your life will be much easier if you construct a driver instance by statically reference the driver. Once you have that, you can ignore DriverManager
which made of evil.
我不得不说,如果通过静态引用驱动程序来构造驱动程序实例,那么您的生活会更容易。一旦你有了,你可以忽略由邪恶组成的DriverManager。
#4
1
Reading the JavaDoc it looks like Class.forName was required to start with, and then things changed so that it was no longer the prefered way (or the required way).
读取JavaDoc看起来像Class.forName需要从一开始,然后事情发生了变化,以至于它不再是首选的方式(或所需的方式)。
#5
1
"is performance etc. better?"
“表演等更好吗?”
I would say that performance for this one-time operation is the least of your worries.
我想说这次一次性操作的表现是你最不担心的。
If you're using a Java EE app server the answer is "neither". You should be setting up a connection pool and let it handle loading the driver and handing out the connections.
如果您使用的是Java EE应用服务器,那么答案就是“不”。您应该设置连接池并让它处理加载驱动程序并分发连接。