无法在java中加载驱动程序类

时间:2018-02-28 16:45:42

标签: java postgresql

我正在尝试连接到我的Postgresql数据库,但在尝试连接到驱动程序时,我收到一条错误消息,说“无法加载驱动程序类!”#39;。我不明白为什么会这样。在我编写的代码中是否有任何我错过的内容?

public class getDatabase {

private Connection conn;
private PreparedStatement getEmployees;


public getDatabase() {

    try {
        Class.forName("org.postgresql.Driver"); 
        conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres", "anna123");
    }
    catch(ClassNotFoundException ex) {
            System.out.println("Error: unable to load driver class!");
            System.exit(1);
    } catch (SQLException e1) {
        e1.printStackTrace();
    } catch (Exception e2) {
        e2.printStackTrace();
    }
    //Setting up the prepared SQL-queries we will need.
    try {
        getEmployees = conn.prepareStatement("SELECT * FROM employee");

    } catch (SQLException e) {
        System.out.println("Error: unable to prepare SQL statements.");
        e.printStackTrace();
    }   

}

0 个答案:

没有答案