JDBC与PostgreSQL连接

时间:2014-11-27 17:09:19

标签: java postgresql jdbc

我正在尝试将我的PC上运行的ProgreSQL DB与JDBC连接。

package postgres;
import java.sql.*;
import java.util.Properties;


public class SQLConnector {
String url = "jdbc:postgresql://localhost:5432/postgres";
Properties props = new Properties();
Connection con;

public SQLConnector() throws SQLException {
    props.setProperty("user", "postgres");
    props.setProperty("password", "admin");

    this.con = DriverManager.getConnection(url, props);
}
public boolean isOpen() throws SQLException {

    return con.isValid(5);

}
public static void main(String[] args)throws SQLException {
    SQLConnector sqldb = new SQLConnector();
    if (sqldb.isOpen()) {
        System.out.println("Connection successfully established.");
    }
}
}

我总是得到以下例外:

Exception in thread "main" java.lang.AbstractMethodError: org.postgresql.jdbc3g.Jdbc3gConnection.isValid(I)Z
at postgres.SQLConnector.isOpen(SQLConnector.java:21)
at postgres.SQLConnector.main(SQLConnector.java:27)

驱动程序位于项目的引用库中。

对帮助感到高兴。

蒂莫

1 个答案:

答案 0 :(得分:1)

欢迎Timon。

不幸的是,“jdbc3”方法“isValid”未实现。

我建议你使用“jdbc4”。