1

我正在尝试开发一个需要通过 java servlet 连接到远程数据库 as400 并将 JSON 对象返回给用户的 android 应用程序。

这是我的 DoGet 方法代码:

protected void doGet(HttpServletRequest request,HttpServletResponse response) throws       ServletException, IOException {
    response.setContentType("application/json");
    PrintWriter out = response.getWriter();
    out.println("{");

    Connection conn = null;
    try {
    Class.forName("com.ibm.as400.access.AS400JDBCDriver").newInstance();
        conn = DriverManager.getConnection( "jdbc:as400://ipaddress;database name=dbname", "username", "password");
        System.out.println("Connected to the database");
    } catch (Exception e) {
        System.out.println("error! 1");
        e.printStackTrace();
    }


    try {
        conn.close();
        System.out.println("Disconnected from database");
    } catch (Exception e) {
        System.out.println("error! 2");
        e.printStackTrace();
    }

}

在我通过 eclipse 启动 servlet 后,它停在“Class.forName(....)”,它给了我以下错误:错误!1 java.sql.SQLException: 应用程序请求者无法建立连接。(连接超时:连接)

at com.ibm.as400.access.JDError.throwSQLException(JDError.java:565)
at com.ibm.as400.access.AS400JDBCConnection.setProperties(AS400JDBCConnection.java:3332)
at com.ibm.as400.access.AS400JDBCDriver.prepareConnection(AS400JDBCDriver.java:1393)
at com.ibm.as400.access.AS400JDBCDriver.initializeConnection(AS400JDBCDriver.java:1230)
at com.ibm.as400.access.AS400JDBCDriver.connect(AS400JDBCDriver.java:371)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at it.liuc.webtest.Servlet1.doGet(Servlet1.java:31)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.ibm.as400.access.PortMapper.getSocketConnection(PortMapper.java:273)
at com.ibm.as400.access.PortMapper.getServerSocket(PortMapper.java:161)
at com.ibm.as400.access.AS400ImplRemote.signonConnect(AS400ImplRemote.java:2334)
at com.ibm.as400.access.AS400ImplRemote.signon(AS400ImplRemote.java:2250)
at com.ibm.as400.access.AS400.sendSignonRequest(AS400.java:3042)
at com.ibm.as400.access.AS400.promptSignon(AS400.java:2606)
at com.ibm.as400.access.AS400.signon(AS400.java:3921)
at com.ibm.as400.access.AS400.connectService(AS400.java:1175)
at com.ibm.as400.access.AS400JDBCConnection.setProperties(AS400JDBCConnection.java:3324)
... 20 more
java.lang.NullPointerException
at it.liuc.webtest.Servlet1.doGet(Servlet1.java:43)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)

似乎它无法连接到服务器,但我确定用户名、密码和数据库名是正确的。我必须在 IP 地址之后插入一个端口吗?还有其他建议吗?

谢谢回答!

4

3 回答 3

3

ConnectionTimedOut堆栈跟踪中的

  • AS/400 未配置为回复 TCP 连接
  • 中间某处有防火墙

从应用服务器机器上,尝试远程登录到数据库服务器中的 AS/400 端口,并查看它是否可以连接(即使它自动断开连接)或者它是否只是挂起。您可以使用 Wireshark 更详细地查看正在发生的事情。

于 2012-05-10T10:18:18.757 回答
2

它停在“Class.forName(....)”

这是不正确的。那条线被执行了;如果失败,您会看到一个ClassNotFoundException.

您正在尝试执行以下行,即建立连接的位置。这就是失败的原因。

检查您正在使用的 URL。我还要确保您正在运行的客户端计算机可以看到数据库服务器。您可以从客户端机器 ping 数据库服务器吗?您可以使用某种非 JDBC 客户端连接到数据库吗?您是否有权使用您获得的凭据访问该数据库?拥有该数据库的 DBA 是否知道您的应用程序正在尝试连接到它?

更新:为了确保我正确理解您,您是说您在运行 Java 客户端的机器上打开了一个命令 shell 并输入了“ping ip-address”,其中“ip-address”是与您在连接 URL 中输入的值相同。

如果我在我的机器上打开一个命令 shell 并 ping Google,它看起来像这样:

C:>ping www.google.com

Pinging www.l.google.com [74.125.130.147] with 32 bytes of data:

Reply from 74.125.130.147: bytes=32 time=29ms TTL=42
Reply from 74.125.130.147: bytes=32 time=27ms TTL=42
Reply from 74.125.130.147: bytes=32 time=29ms TTL=42
Reply from 74.125.130.147: bytes=32 time=28ms TTL=42

Ping statistics for 74.125.130.147:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 27ms, Maximum = 29ms, Average = 28ms

C:>

如果你没有,你有你的答案:你的客户端机器看不到网络上的数据库服务器。

端口很重要,因为设置数据库的 DBA 可能没有使用默认端口。您需要与拥有数据库的人交谈并解决这个问题。

于 2012-05-10T10:10:33.767 回答
1

您是否为该程序提供了适当的驱动程序库?首先,您下载数据库连接驱动程序并导入您的程序。并创建与远程数据库的连接并给出类路径和连接路径。然后在你的程序中,类路径和连接路径在你的程序中给出。运行你的程序。它将解决您的问题。

于 2012-06-18T05:19:21.887 回答