繁体   English   中英

Java应用:连接到PostgreSQL数据库

[英]Java app: Connect to PostgreSQL database

我正在尝试连接到Postgres SQL数据库。 通常,我可以通过以下链接进行连接:

test.ischool.testU.edu

通过输入凭据连接到phpPgAdmin的位置,然后从侧面选择数据库即可连接到数据库testDB。

这是我相关的Java代码:

    try {
        Class.forName("org.postgresql.Driver");
    } catch (ClassNotFoundException e1) {
        e1.printStackTrace();
    }

    String url = "jdbc:postgresql://test.ischool.testU.edu:5432/testDB";
    String user = "testuser";
    String pass = "testpassword";
    Connection db = null;
    try {
        db = DriverManager.getConnection(url,user,pass);
    } catch (SQLException e1) {
        e1.printStackTrace();
    }

我收到以下错误:

org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port             are correct and that the postmaster is accepting TCP/IP connections.

我敢肯定我做错了什么。 假设我的代码正确(我确定不是这种情况),要连接到数据库,是否需要将.jar文件放在远程目录中,还是可以在本地运行它? 这有什么不同吗?

我已使用以下python代码成功连接到我的数据库(此代码直接插入到远程服务器中并从那里运行):

    conn = psycopg2.connect("dbname=testDB user=testuser password=testpassword")

您使用的JDBC URL适用于MySQL:

jdbc:mysql://test.ischool.testU.edu:5432/testDB

对PostgreSQL使用一个:

jdbc:postgresql://test.ischool.testU.edu:5432/testDB

如果更改此方法无济于事,请发布您遇到的确切错误。

尝试这段代码:

String url = "jdbc:postgresql://localhost/YOURSCHEMA?user=YOURUSER&password=YOURPWD";

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

Connection con = DriverManager.getConnection(url);`

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM