Anto
Anto

Reputation: 4305

Simple Java web service issue

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!

Upvotes: 1

Views: 1403

Answers (1)

duffymo
duffymo

Reputation: 308743

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.

Upvotes: 2

Related Questions