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!