java – 是否需要Class.forName()机制?

这个问题在这里已有答案:            >            Class.forName(JDBC_DRIVER) no longer needed?                                    2个
以下代码:

Class.forName(dbDriver); // "org.postgres.Driver" or "com.mysql.jdbc.Driver"

是/必须打开JDBC连接.

我听说现代JDBC驱动程序不再需要它.但是我无法在我的项目中删除它,因为我找不到合适的驱动程序发现异常.我使用的是postgresql-9.1-901.jdbc3.jar,Java7和tomcat7.

我什么时候可以省略Class.forName(…)构造?

从JDBC 4.0开始不需要Class.forName().

这是Java Tutorials on JDBC的摘录.

In previous versions of JDBC, to obtain a connection, you first had to
initialize your JDBC driver by calling the method Class.forName. This
methods required an object of type java.sql.Driver. Each JDBC driver
contains one or more classes that implements the interface
java.sql.Driver. The drivers for Java DB are
org.apache.derby.jdbc.EmbeddedDriver and
org.apache.derby.jdbc.ClientDriver, and the one for MySQL Connector/J
is com.mysql.jdbc.Driver. See the documentation of your DBMS driver to
obtain the name of the class that implements the interface
java.sql.Driver.

Any JDBC 4.0 drivers that are found in your class path are
automatically loaded.
(However, you must manually load any drivers
prior to JDBC 4.0 with the method Class.forName.)

https://stackoverflow.com/questions/33391496/is-class-forname-mechanism-needed

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:java – 是否需要Class.forName()机制?