2012-03-19 53 views
1

我试图创建一个jax-ws web服务,它将查询mysql数据库并从@WebMethod返回用户定义的数据类型。我第一次尝试返回由sql导入提供的sql.ResultSet。但是,JAXB无法绑定它。所以我试着返回一个String [] [],但是在客户端读取结果时,它显示了所有的空字段。JAX-WS @WebMethod返回用户定义的数据类型

然后我尝试创建自己的数据类型,我明白我创建的类应该有一个默认的构造函数和公共集合并获取该类中定义的所有变量。但是我想返回一个结果集。我的Web服务的代码如下:

@WebMethod 
public String[][] getSummary(int month, int year){ 
    // register jdbc driver 
    try 
    { 
     Class.forName("com.mysql.jdbc.Driver").newInstance(); 
    } 
    catch(Exception ex) 
    { 
     System.out.print(ex.getMessage()); 
    } 

    // declare connection, statement and result set 
    Connection connection = null; 
    Statement statement = null; 
    ResultSet resultset = null; 

    String[][] result = new String[30][8]; 
    int counter = 0; 

    try 
    { 
     connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/Livestock?user=root"); 
     statement = connection.createStatement(); 
     resultset = statement.executeQuery("SELECT Category, Particulars, Nos, OriginalCost, AccuDep, WDV, NRV, CapLoss FROM TB_SUMMARY WHERE Month=" + month + " AND Year=" + year); 

     // loop over result set 
     while(resultset.next()) 
     { 
      result[counter][0] = resultset.getString("Category"); 
      result[counter][1] = resultset.getString("Particulars"); 
      result[counter][2] = resultset.getString("Nos"); 
      result[counter][3] = resultset.getString("OriginalCost"); 
      result[counter][4] = resultset.getString("AccuDep"); 
      result[counter][5] = resultset.getString("WDV"); 
      result[counter][6] = resultset.getString("NRV"); 
      result[counter][7] = resultset.getString("CapLoss"); 

      counter++; 
     }   
    } 
    catch(SQLException ex) 
    { 
     System.out.println("SQLException: " + ex.getMessage()); 
     System.out.println("SQLState: " + ex.getSQLState()); 
     System.out.println("VendorError: " + ex.getErrorCode()); 
    } 
    finally 
    { 
     // it is a good idea to release 
     // resources in a finally{} block 
     // in reverse-order of their creation 
     // if they are no-longer needed 
     if(resultset != null) 
      try 
      { 
       resultset.close(); 
      } 
      catch (SQLException e) 
      { 
       e.printStackTrace(); 
      } 

     resultset = null; 

     if(statement != null) 
      try 
      { 
       statement.close(); 
      } 
      catch (SQLException e) 
      { 
       e.printStackTrace(); 
      } 

     statement = null; 
    } 

    return result; 
} 

我不知道为什么所有的细胞在String [] []是空的,因为代码甚至不到达的executeQuery() ;或者也许它的jaxb绑定不起作用。

我还不明白jaxb是如何工作的。如果有人有一个如何与jaxb一起使用jaxws的好例子,我会非常感激。我在互联网上发现的文件是压倒性的和复杂的。

回答

3

在你的情况下,您将无法返回ResultSet因为JAX-WS(JAX-B)只能够结合简单的数据类型。看看this

你的方法返回数组是最常见的解决这个问题,并且在你的情况下足够。

您应该尝试调试您的应用程序以确保您的查询被触发并且结果被填充。

我的最好的办法是,你在下面的线路之一出现错误:

的Class.forName( “com.mysql.jdbc.Driver”)的newInstance();
connection = DriverManager.getConnection(“jdbc:mysql:// localhost:3306/Livestock?user = root”);

但你的代码看起来很好,所以我只是在猜测。确保您的数据库在线并尝试调试您的代码。

玩得开心!

+0

你是绝对正确的。我的错误是在Class.forName(“com.mysql.jdbc.Driver”)。newInstance(); – moeabdol 2012-03-19 09:17:55

+0

很高兴听到!我希望这也解决了你的其他问题!欢呼声 – SimonSez 2012-03-19 09:40:45

+0

感谢您的帮助。它确实指出了错误的位置,但我仍然需要找出如何解决这个问题。我尝试了与其他一些数据库Java应用程序相同的语句,并据此工作。仍然不知道为什么它不在这里工作。无论如何,感谢您指出:) – moeabdol 2012-03-19 09:50:07