如何使用Java远程连接到PostgreSQL

时间:2012-07-30 18:36:20

标签: java jdbc

我一直致力于一个需要我们连接到PostgreSQL数据库的项目。我能够使用本地IP(即192.168.1.145)将我的代码本地连接到PostgreSQL数据库,现在我用公共IP替换了本地IP,它引发了我的错误:

以下是我用来连接数据库的代码

private static final String DRIVER = "org.postgresql.Driver";
//private static final String URL = "jdbc:postgresql://192.168.1.145:5432/ResumeDatabase";
private static final String URL = "jdbc:postgresql://71.555.555.555:5432/ResumeDatabase";
private static final String USERNAME = "defaultuser";
private static final String PASSWORD = "password";

private static Connection getConnection() throws Exception {
    Class.forName(DRIVER);
    Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
    return conn;
  }

以下是我得到的错误:

  Adding a resume
    org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port    are correct and that the postmaster is accepting TCP/IP connections.
    ID ----------- 0
    Added Resume
    at
    org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:136)
        at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66)
    at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:125)
    at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:30)
    at org.postgresql.jdbc3.Jdbc3Connection.<init>(Jdbc3Connection.java:24)
    at org.postgresql.Driver.makeConnection(Driver.java:393)
    at org.postgresql.Driver.connect(Driver.java:267)
    at java.sql.DriverManager.getConnection(DriverManager.java:582)
    at java.sql.DriverManager.getConnection(DriverManager.java:185)
    at com.ssati.resumeformatter.data.dao.ResumeDAO.getConnection(ResumeDAO.java:247)
    at com.ssati.resumeformatter.data.dao.ResumeDAO.addResume(ResumeDAO.java:105)
    at com.ssati.resumeformatter.data.dao.ResumeDAO.main(ResumeDAO.java:318)
Caused by: java.net.ConnectException: Operation timed out
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:432)
    at java.net.Socket.connect(Socket.java:529)
    at java.net.Socket.connect(Socket.java:478)
    at java.net.Socket.<init>(Socket.java:375)
    at java.net.Socket.<init>(Socket.java:189)
        at org.postgresql.core.PGStream.<init>(PGStream.java:62)
    at     org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:76)
    ... 11 more

谢谢!

0 个答案:

没有答案