Reputation: 11
I am trying to develop an android application that needs to connect to a remote database as400, through a java servlet and give back a JSON object to the user.
This is my DoGet Method code:
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();
}
}
After I launch the servlet through eclipse, it stops at "Class.forName(.... )" and it gives me the following error: error! 1 java.sql.SQLException: The application requester cannot establish the connection.(Connection timed out: connect)
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)
And seems like it cannot connect to the server, but I'm sure that username, password and dbname are correct. Do I have to insert a port, after the IPaddress? Any other suggestions?
Thanks for answering!
Upvotes: 1
Views: 6691
Reputation: 151
Have you given proper driver library to this program? First you download database connection driver and import in your program. And create connection to remote database and give class path and connection path. And then in your program that class path and connection path gives in your program. Run your program. It will fix your problem.
Upvotes: 1
Reputation: 24895
The ConnectionTimedOut
in the stack trace makes it look like as if either
From the app server machine, try to do a telnet to the AS/400 port in the DB server, and see if it gets to connect (even if it automatically disconnects) or if it is just left hanging. You can use Wireshark to see what is happenning with greater detail.
Upvotes: 3
Reputation: 308998
it stops at "Class.forName(.... )"
This is incorrect. That line was executed; if it failed, you'd see a ClassNotFoundException
.
You're trying to execute the line that follows, where the connection is made. That's what is failing.
Check the URL that you're using. I'd also make sure that the client machine you're running on can see the database server. Can you ping the database server from the client machine? Can you connect to the database using a non-JDBC client of some kind? Do you have permission to access that database with the credentials you've been given? Does the DBA who owns that database know your application is trying to connect to it?
Update: Just to be sure that I understand you correctly, you're saying that you opened a command shell on the machine you're running your Java client on and typed "ping ip-address", where "ip-address" is the same as the value you entered into your connection URL.
If I open a command shell on my machine and ping Google, it looks like this:
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:>
If yours does not, you have your answer: your client machine cannot see the database server on the network.
The port matters, because the DBA who set up the database might not have used the default port. You need to speak to the person who owns the database and sort this out.
Upvotes: 2