Java JDBC - 找不到合适的驱动程序

Chi*_*ase 1 java eclipse postgresql jdbc

我的代码访问本地数据库时遇到问题。我正在使用 Eclipse 和 postgresql。

我很清楚已经有几篇这样的帖子,但它们没有帮助我解决我的问题,所以这就是我问自己的原因。

驱动程序的 jar 文件已经在库中并且也被添加到构建路径中。但是我仍然从找不到驱动程序中得到错误。

无法获得连接:java.sql.SQLException:找不到适合 psql -h localhost beleg postgres 的驱动程序

我的代码如下所示:

package DB;
import java.sql.*;

public class Connections {

private Connection dbcon;
private Statement stmt;
private Statement stmt2;

Connections(String db_url, String username, String password) {
    try {
        Class.forName("org.postgresql.Driver");
        dbcon = DriverManager.getConnection(db_url, username, password);

        stmt = dbcon.createStatement();
        stmt2 = dbcon.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, 
        ResultSet.CONCUR_READ_ONLY);
    } catch (Exception e) {
        System.out.println("Can't get a connection: " + e.toString());

        try {
            if (stmt != null)
                stmt.close();
        } catch (SQLException f) {
        }

        try {
            if (stmt2 != null)
                stmt2.close();
        } catch (SQLException f) {
        }
        try {
            if (dbcon != null)
                dbcon.close();
        } catch (SQLException f) {
        }
        System.exit(0);
    }

}
Run Code Online (Sandbox Code Playgroud)

使用 Add Jar 添加了 JAR,因为它位于项目的 lib 文件夹中。

Dan*_*n W 5

您的 JDBC URL 无效。尝试将其更改为:

jdbc:postgresql://localhost/<database name>
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅PostgreSQL JDBC 文档