我正在尝试开发一个需要通过 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 地址之后插入一个端口吗?还有其他建议吗?
谢谢回答!