我正在使用PostgreSQL 9.3。 PostgreSQL驱动程序是postgresql-8.3-603.jdbc4.jar
。
我需要编写jUnit测试,它建立sql-connection并聚合数据库中的数据。我写的以下@Test
方法:
@Test
public void todayStatisticTest() throws ClassNotFoundException, SQLException {
Connection conn = null;
Properties connectionProps = new Properties();
connectionProps.put("user", "postgres");
connectionProps.put("password", "password");
Class.forName("org.postgresql.Driver"); //ClassNotFound exception is thrown
conn = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/db_name", connectionProps);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM partner.partner");
}
为什么ClassNotFoundExpcetion
被抛出?
答案 0 :(得分:2)
启动junit测试时,您可能没有在类路径上提供所有必需的jar。在测试运行期间,您需要确保postgresql驱动程序jar也在类路径中。取决于您使用的IDE,或者如果您在命令行上运行它,您必须将它添加到-cp参数(这实际上是IDE将为您做的)。