我正在尝试开发一个需要通过java servlet连接到远程数据库as400的android应用程序,并将JSON对象返回给用户。
这是我的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)
似乎它无法连接到服务器,但我确信用户名,密码和dbname是正确的。我必须在IP地址之后插入一个端口吗?还有其他建议吗?
感谢您的回答!
答案 0 :(得分:3)
堆栈跟踪中的ConnectionTimedOut
使其看起来像是
从应用服务器计算机上,尝试telnet到数据库服务器中的AS / 400端口,看看它是否连接(即使它自动断开连接),或者它是否只是挂起。您可以使用Wireshark查看更详细的内容。
答案 1 :(得分:2)
它停在“Class.forName(....)”
这是不正确的。那条线被执行了;如果失败,您会看到ClassNotFoundException
。
您正在尝试执行连接所在的行。这就是失败的原因。
检查您正在使用的网址。我还要确保您运行的客户端计算机可以看到数据库服务器。你能从客户端机器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可能没有使用默认端口。您需要与拥有数据库的人员交谈并对其进行排序。
答案 2 :(得分:1)
您是否为此计划提供了正确的驱动程序库?首先,下载数据库连接驱动程序并导入程序。并创建与远程数据库的连接,并提供类路径和连接路径。然后在您的程序中,类路径和连接路径在您的程序中提供。运行你的程序。它会解决你的问题。