0

我在使用 NetBeans 5.5.1 时遇到了一个奇怪的问题。我正在为我的一个课程创建一个测试课程。我正在测试的方法是从一个名为 getConnection 的方法中获取自己的连接,如下所示:

//This method was mainly done for an HSQLDB connection
public static Connection getConnection(String connectionString,String databaseDriver, String user, String pass) throws Exception
{
    Connection conn = null;
    try 
    {
        System.out.println("Registering database driver "+databaseDriver);
        Class.forName(databaseDriver);
        System.out.println("Successfully registered database driver "+databaseDriver);
        Properties props = new Properties();
        props.setProperty("user",user);
        props.setProperty("password",pass);

        conn = DriverManager.getConnection(connectionString, props);
    } 
       catch (ClassNotFoundException ex) 
       {
        System.out.println("Reached catch block getConnection()");
        throw new Exception ("Unable to create connection to database", ex);
    }

    catch (Exception ex) 
    {
        System.out.println("Reached catch block getConnection()");
        throw new Exception ("Unable to create connection to database", ex);
    }

        return new DatabaseConnection(conn).getConnection();

}

我使用的数据库驱动程序是 HSQLDB。运行我的测试方法后,打印到我的输出的唯一日志语句是 注册数据库驱动程序 org.hsqldb.jdbcDriver

我的预期结果是: 注册数据库驱动 org.hsqldb.jdbcDriver 成功注册数据库驱动 org.hsqldb.jdbcDriver

当我故意使用错误的数据库驱动程序时,我可以清楚地看到抛出异常,这意味着我正在使用的上述驱动程序正在工作,并且可以在我的 classPath 中找到它。

那么问题似乎是什么?

4

1 回答 1

1

正如“Ian Roberts”所建议的那样,当将异常作为 Throwable 捕获时,我意识到 Class.ForName 抱怨类号错误(当您尝试使用比您的库之一更旧的 JDK 编译时会发生这种情况) . 将 HSQLDB 从版本 2.2.9 更改为 2.2.8.jdk5 解决了这个问题。

于 2013-02-20T13:45:33.340 回答