无法使用JDBC连接到PostgreSQL Server 10

问题描述 投票:0回答:1

我可以用pgAdmin但不能用JDBC连接,我做错了什么?

enter image description here

JDBC:

方法#1

String url = "jdbc:postgresql://localhost:5432/microlms360";

Properties props = new Properties();
props.setProperty("user","microlms360app");
props.setProperty("password","p0stgr@s");
props.setProperty("ssl","true");
Connection conn = DriverManager.getConnection(url, props);

方法#2

String url = "jdbc:postgresql://localhost:5432/microlms360?user=microlms360app&password=p0stgr@s&ssl=false";
try (Connection conn = DriverManager.getConnection(url)) {
    boolean res = conn.createStatement().execute("SELECT 1");
    System.out.println(res);
}

结果在两种情况下都是相同的(我已经尝试过localhost127.0.0.1作为主机名。徒劳无功。)

Connected to the target VM, address: '127.0.0.1:8648', transport: 'socket'
Exception in thread "main" org.postgresql.util.PSQLException: Connection to 127.0.0.1:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:280)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195)
    at org.postgresql.Driver.makeConnection(Driver.java:454)
    at org.postgresql.Driver.connect(Driver.java:256)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:208)
    at iex.Main.main(Main.java:16)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at org.postgresql.core.PGStream.<init>(PGStream.java:70)
    at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:91)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:192)
    ... 7 more
Disconnected from the target VM, address: '127.0.0.1:8648', transport: 'socket'

根据这个postgresql.org blog我使用最新的驱动程序

<dependency>
  <groupId>org.postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <version>42.2.5</version>
</dependency>

更新:postgres进程实际上是在监听,所以“什么都听不到端口”不是这样的情况qazxsw poi

postgresql jdbc postgresql-10
1个回答
1
投票

在Java应用程序运行的计算机上没有侦听localhost端口5432,或者它被防火墙主动阻止。错误消息“java.net.ConnectException:Connection refused:connect”来自Java的低级套接字,毫无疑问它无法连接。

PgAdmin4不会被防火墙阻止,或者PgAdmin4的屏幕截图来自不同的计算机,或者您正在连接到运行在PostgreSQL在localhost端口5432上运行的其他主机上的PgAdmin4。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.