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

huangapple go评论75阅读模式
英文:

Local java program unable to connect to local database

问题

以下是翻译好的内容:

我无法连接到Pg Admin 4上的数据库,尽管我能够使用Intellij中的DB Browser查询数据库。以下是我一直收到的错误。

这个相同的程序在尝试下载Python SQL工具后仍然可以连接到数据库,但出现了问题。我重新安装了postgresql,并按照原样创建了表,但现在无法连接到它们。

com.mysql.cj.jdbc.exceptions.CommunicationsException: 通信链接失败

最后一个成功发送到服务器的数据包距离现在0毫秒。驱动程序未从服务器收到任何数据包。

这里是尝试访问数据库的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查询工具的截图。

DB Browser

我已经检查了很多类似的错误,但它们似乎都涉及拼写错误。我可以将数据库更改为不正确的名称、所有者或密码,但错误仍然相同。我认为可能是驱动程序的问题,因为在程序中我使用了mysql-connector-java-8.0.20.jar,但数据库是由postgresql运行的。我尝试将jar替换为postgresql-42.2.17.jar,但也不起作用。

在这一点上,我正在考虑放弃这一切,去山上生活,所以任何帮助都将不胜感激。

编辑:将jar文件复制到lib文件夹中,在使用此方法来解压缩它之前是不起作用的。可能需要尝试几次,但我的最终起作用了。非常感谢!如果我有声望点数,我会给你点赞。

英文:

I am unable to connect to a database on Pg Admin 4; despite being able to query the database using DB Browser in intellij. Below is the error I keep getting.

This same program could connect to the database however after trying to download a python sql tool something went wrong. I re-installed postgresql and created the tables as they were but I now can't connect to them.

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.

Here is the connectionclass.java which tries to access the database.

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;
    }
}

Finally I have a screengrab of the sql query tool.

DB Browser

I've checked dozens of errors like this but they all seem to involve misspellings. I can change the database to an incorrect name, owner, or password but the error remains the same. I thought it might be the driver as I use mysql-connector-java-8.0.20.jar in the program but postgresql is what runs the database. I tried replacing the jar with postgresql-42.2.17.jar but that dosn't work either.

At this point I'm considering scrapping the whole thing and going to live on a mountain so any help is appreciated.

EDIT: Coping the jar into lib folder does not work until you use the method outlined here to unpack it. Might take a few tries but mine finally works. Thank you so much! If I had any reputation points I'd upvote.

答案1

得分: 1

下载适当的驱动程序

https://jdbc.postgresql.org/download.html

在您的程序中加载“正确”的驱动程序“org.postgresql.Driver”,就像您为mysql做的那样,但这次请这样做:

Class.forName("org.postgresql.Driver");

然后使用正确的协议“jdbc:postgresql:”进行连接:

Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/" + dbName, userName, password);
英文:

Download the appropriate driver

https://jdbc.postgresql.org/download.html

load the "correct" driver ""org.postgresql.Driver" in your program as you do for mysql, do this instead

Class.forName("org.postgresql.Driver");

and connect with the correct protocol "jdbc:postgresql:"

Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/" + dbName, userName, password);

huangapple
  • 本文由 发表于 2020年10月16日 04:05:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64378953.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定