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

时间:2015-10-28 12:48:39

标签: java database tomcat jdbc database-connection

以下代码:

yGrid = yGrid + np.zeros(xGrid.shape)
f3 = interpolate.interp2d(xGrid,yGrid,zGrid,kind='linear')
import matplotlib.pyplot as plt
plt.plot(np.linspace(0.001, 5, 100), [f3(y, 2) for y in np.linspace(0.001, 5, 100)])
plt.plot(xGrid[:, 1], zGrid[:, 1])
plt.plot(xGrid[:, 0], zGrid[:, 0])

是/必须打开JDBC连接。

我听说现代JDBC驱动程序不再需要它。但是我无法在我的项目中删除它,因为我得到Class.forName(dbDriver); // "org.postgres.Driver" or "com.mysql.jdbc.Driver" 异常。我正在使用No suitable driver found,Java7和tomcat7。

我什么时候可以省略postgresql-9.1-901.jdbc3.jar构造?

1 个答案:

答案 0 :(得分:10)

自JDBC 4.0以来不需要Class.forName()。

以下摘自Java Tutorials on JDBC

  

在先前版本的JDBC 中,要获得连接,首先必须这样做   通过调用Class.forName方法初始化JDBC驱动程序。这个   方法需要java.sql.Driver类型的对象。每个JDBC驱动程序   包含一个或多个实现该接口的类   java.sql.Driver中。 Java DB的驱动程序是   org.apache.derby.jdbc.EmbeddedDriver和   org.apache.derby.jdbc.ClientDriver,以及MySQL Connector / J的那个   是com.mysql.jdbc.Driver。请参阅DBMS驱动程序的文档   获取实现接口的类的名称   java.sql.Driver中。

     

在类路径中找到的任何JDBC 4.0驱动程序都是   自动加载。(但是,您必须手动加载任何驱动程序   在JDBC 4.0之前使用方法Class.forName。)