2012-02-13 99 views
1

我想創建一個訪問存儲在外部服務器中的數據庫的Java Web服務。我已經創建了一個包含主要信息的BeepWebService類:簡單的Java Web服務問題

@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; 

} 

} 

然後我創建了出版商類,命名BeepWebServicePublisher:

public class BeepWebServicePublisher { 

public static void main(String[] args){ 

    System.out.println("Web Service initiating..."); 
    Endpoint.publish("http://xxxxxx.ipagemysql.com/", new BeepWebService()); 
} 
} 

不幸的是成功編譯兩個類和運行應用程序後,輸出消息是'失敗'(意味着連接不能與數據庫建立),然後是異常錯誤。這是完整的輸出:

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 

由於我是新手在這個領域,誰能告訴我,如果有什麼不對的代碼或問題可能與服務器?謝謝!

回答

2

打印出整個堆棧跟蹤;它會給你比你的「失敗」消息更多的信息。

你在哪裏註冊MySQL JDBC驅動程序?我沒看到它。

更新:我建議你分解問題。不要將數據庫代碼放入Web服務中。創建一個可以脫機測試的基於接口的POJO。讓它工作,然後給Web服務使用一個實例。

你有99個問題,兒子。從一開始就修復一個。

這是錯誤的:

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

您需要裝訂參數:

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

你需要一個PreparedStatement,而不是一個Statement

然後要你通過名稱綁定

你只有一個列由SELECT返回。你爲什麼在第2欄setString

錯誤的方式很多。

+0

這就是問題所在。該驅動程序未加載。 – 2012-02-13 15:28:30

+0

我編輯了問題 – Anto 2012-02-13 15:58:07

+0

太糟糕了,您沒有在堆棧跟蹤中進行編輯。 – duffymo 2012-02-13 16:02:38