Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
本地java程序无法连接到本地数据库_Java_Postgresql_Jdbc - Fatal编程技术网

本地java程序无法连接到本地数据库

本地java程序无法连接到本地数据库,java,postgresql,jdbc,Java,Postgresql,Jdbc,我无法连接到Pg Admin 4上的数据库;尽管能够在intellij中使用DB Browser查询数据库。下面是我不断遇到的错误 这个程序可以连接到数据库,但是在尝试下载python sql工具之后,出现了一些问题。我重新安装了postgresql并按原样创建了表,但现在无法连接到它们 com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure The last packet sent s

我无法连接到Pg Admin 4上的数据库;尽管能够在intellij中使用DB Browser查询数据库。下面是我不断遇到的错误

这个程序可以连接到数据库,但是在尝试下载python sql工具之后,出现了一些问题。我重新安装了postgresql并按原样创建了表,但现在无法连接到它们

com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
下面是试图访问数据库的connectionclass.java

public class ConnectionClass {

    public Connection connection;
    public Connection getConnection() {
        String dbName = "users";
        String userName = "postgres";
        String password = "pass";

        
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            connection = DriverManager.getConnection("jdbc:mysql://localhost:5432/" + dbName, userName, password);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }
}
最后,我有一个sql查询工具的截图

我已经检查了几十个这样的错误,但它们似乎都涉及拼写错误。我可以将数据库更改为不正确的名称、所有者或密码,但错误保持不变。当我在程序中使用mysql-connector-java-8.0.20.jar时,我认为它可能是驱动程序,但运行数据库的是postgresql。我试着用postgresql-42.2.17.jar替换jar,但也没用

在这一点上,我正在考虑放弃整件事,住在山上,所以任何帮助都是感激的


编辑:在使用解包之前,将jar压缩到lib文件夹中是不起作用的。可能需要几次尝试,但我的最终有效。非常感谢你!如果我有任何信誉点,我会投票。

下载合适的驱动程序

在您的程序中加载“正确”的驱动程序“org.postgresql.driver”,就像在mysql中一样,改为这样做

Class.forName("org.postgresql.Driver");
并使用正确的协议“jdbc:postgresql:”


执行此操作并将.jar复制到lib文件夹中,但现在出现错误:java.lang.ClassNotFoundException:org.postgresql.Driveris这是控制台应用程序还是web应用程序?控制台应用程序。
Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/" + dbName, userName, password);