1

I want to create a webservice in Java that accesses a database stored in an external server. I have created a BeepWebService class containing the main information:

@WebService public class BeepWebService { private Connection conn = null; private String url = "jdbc:mysql://xxxxxx.ipagemysql.com/"; private String dbName = "beep"; private String userName = "beep_user_name"; private String password = "pswrd"; private String db_str = " select Name beep.SW where Name = "; public BeepWebService(){ try { Class.forName("com.mysql.jdbc.Driver").newInstance(); this.conn = DriverManager.getConnection(url+dbName,userName,password); System.out.println("Connected to the database"); }catch (Exception e) { System.out.print("failed"); } } @WebMethod public String returnFormat(@WebParam(name="input_value") String input){ String str = null; String query = db_str+input; try { Statement statement = conn.createStatement(); ResultSet rs = statement.executeQuery(query); while (rs.next()) { str = rs.getString(2); System.out.println(str); } rs.close(); statement.close(); } catch (SQLException e) { e.printStackTrace(); } return str; } } 

I have then created the publisher class, named BeepWebServicePublisher:

public class BeepWebServicePublisher { public static void main(String[] args){ System.out.println("Web Service initiating..."); Endpoint.publish("http://xxxxxx.ipagemysql.com/", new BeepWebService()); } } 

Unfortunately after compiling successfully the two classes and run the application, the output message is 'failed' (meaning that the connection couldn't be estabilished with the database) followed by an exception error. This is the complete output:

Web Service starting.. failedException in thread "main" com.sun.xml.internal.ws.server.ServerRtException: Server Runtime Error: java.net.BindException: Address already in use: bind at com.sun.xml.internal.ws.transport.http.server.ServerMgr.createContext(Unknown Source) at com.sun.xml.internal.ws.transport.http.server.HttpEndpoint.publish(Unknown Source) at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.publish(Unknown Source) at com.sun.xml.internal.ws.spi.ProviderImpl.createAndPublishEndpoint(Unknown Source) at javax.xml.ws.Endpoint.publish(Unknown Source) at com.BeepServicePackage.server.BeepWebServicePublisher.main(BeepWebServicePublisher.java:17) Caused by: java.net.BindException: Address already in use: bind at sun.nio.ch.Net.bind(Native Method) at sun.nio.ch.ServerSocketChannelImpl.bind(Unknown Source) at sun.nio.ch.ServerSocketAdaptor.bind(Unknown Source) at sun.net.httpserver.ServerImpl.<init>(Unknown Source) at sun.net.httpserver.HttpServerImpl.<init>(Unknown Source) at sun.net.httpserver.DefaultHttpServerProvider.createHttpServer(Unknown Source) at com.sun.net.httpserver.HttpServer.create(Unknown Source) ... 6 more 

As I am a novice in this field, can someone tell me if there is something wrong in the code or the problem may be with the server? Thanks!

0

1 Answer 1

2

Print out the entire stack trace; it'll give you more information than your "failed" message.

Where did you register the MySQL JDBC driver? I don't see it.

UPDATE: I'd recommend that you decompose the problem. Don't put the database code in the web service. Create an interface-based POJO that you can test off line. Get it working, then give an instance to the web service to use.

You have 99 problems, son. Start by fixing one at a time.

This is wrong:

private String db_str = " select Name beep.SW where Name = "; 

You need a binding parameter:

private String db_str = " select Name beep.SW where Name = ?"; 

You need a PreparedStatement, not a Statement.

Then you want to bind the name you pass in.

You only have one column returned by the SELECT; why do you setString on column 2?

Wrong in so many ways.

Sign up to request clarification or add additional context in comments.

6 Comments

That's the problem. The driver isn't loaded.
The code still prints the message; I realized after I wrote it that your update includes it.
@Anto - you're explicitly ignoring the exception that getConnection() is throwing, instead printing "failed". Perhaps you should print the stacktrace instead - it tells you what the problem is. Your second problem is caused by trying to start your webserver twice; it's already running. Ever try and start your car when it's already running? That :)
ok got this error: Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)....
I tried with Ping and it works, a connection can be estabilished
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.