java到服务器的postgresql数据库中的连接错误

时间:2017-04-05 08:59:48

标签: java postgresql

连接到我的本地Postgresql数据库时,它可以正常工作。

我使用putty将本地端口5300映射到服务器端口5432.

我填写表格的方式是两张图片(无法直接上传图片)。

putty

port mapping

java代码是:

    public static void main(String args[]) throws ClassNotFoundException, SQLException {
    Connection conn = null;
    Statement stmt = null;
    Class.forName("org.postgresql.Driver");

    conn = DriverManager.getConnection("jdbc:postgresql://localhost:5300/database", "root", "123");
    conn.setAutoCommit(false);
    System.out.println("open database successfully");

    stmt = conn.createStatement();
    String sql = "select * from table";

    stmt.execute(sql);
    stmt.close();
    conn.commit();
    conn.close();
}

错误日志是:

Exception in thread "main" org.postgresql.util.PSQLException: Connection Failed
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:275)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:194)
at org.postgresql.Driver.makeConnection(Driver.java:431)
at org.postgresql.Driver.connect(Driver.java:247)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at ConnectDB.main(FindFeature.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:170)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at org.postgresql.core.VisibleBufferedInputStream.readMore(VisibleBufferedInputStream.java:140)
at org.postgresql.core.VisibleBufferedInputStream.ensureBytes(VisibleBufferedInputStream.java:109)
at org.postgresql.core.VisibleBufferedInputStream.read(VisibleBufferedInputStream.java:67)
at org.postgresql.core.PGStream.receiveChar(PGStream.java:282)
at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:418)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:222)
... 12 more

0 个答案:

没有答案