将ResultSet变量存储为服务

时间:2012-11-19 14:54:14

标签: mysql web-services netbeans resultset

我使用NetBeans编写Web服务的代码,这里是操作的代码:

/**
 * Get the products data 
 */
@WebMethod(operationName = "getProducts")
public String[] getProducts() throws Exception {

    Class.forName("com.mysql.jdbc.Driver");

    Connection con;
    con = DriverManager.getConnection("jdbc:mysql://localhost/commerce","root","");
    PreparedStatement stat;
    stat = con.prepareStatement("select * from products");

    ResultSet result = stat.executeQuery();
    String tab[] = new String[5];
    while(result.next())
    {
        System.out.println(tab[0]= result.getString(1));
        System.out.println(tab[1]= result.getString(2));
        System.out.println(tab[2]= result.getString(3));
        System.out.println(tab[3]= result.getString(4));
        System.out.println(tab[4]= result.getString(5));  
    } 
    return tab;
}

所以我只想存储变量“result”的结果并返回表的数据,我试着让操作返回一个ResultSet变量,但是我得到了一个错误,上面的代码我只是获取数据库的最后一个条目,这是逻辑,因为只存储了循环的最后一个值,所以我想知道是否有一个方法可以在结构中返回数据,所以我可以在客户端项目中使用它(Java EE)

0 个答案:

没有答案