簡體   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