日期:2014-05-20  浏览次数:20648 次

java webservice 与数据库交互问题。。急急急。。。
首先我 写了一个操作数据库(我用的是mysql)的java类,里面有一些方法是直接返回一个结果集vector(比如:select * from goods_list),接着我使用axis 的java2wsdl将这个java类生成wsdl文件,然后用一个测试类根据wsdl文件来引用websevice,调用其中的返回结果集的方法,这时候问题就来了,数据库可以连接,但是就是不能返回结果集,取不到数据??我不知道为什么,请各位高手支支招!!!最好能以实例说明

------解决方案--------------------
用wsdl生成axis的客户端,然后再调用webservice服务,看一下能不能返回结果。
如果不行,尝试将Vector修改为数组。
------解决方案--------------------
因为你结果集是个List或者数组或者其他容器,axis2里面返回容器类或者数组的时候要将其封装成OMElement(XML格式)才能传递,你参考下下面的传递list的例子:

package aixs2listtest;

import java.util.*;
public class HelloBean {
private String name;
private int id;
private List<HelloBeanClass> helloBeanClasses;
public HelloBean(){}
public HelloBean(String name, int id ,List<HelloBeanClass> helloBeanClass){
this.name=name;
this.id=id;
this.helloBeanClasses=helloBeanClass;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void setId(int id){
this.id=id;
}
public int getId(){
return this.id;
}
public List<HelloBeanClass> getHelloBeanClass(){
return this.helloBeanClasses;
}
public void setHelloBeanClass(List<HelloBeanClass> helloBeanClass){
this.helloBeanClasses=helloBeanClass;
}

}





package aixs2listtest;

public class HelloBeanClass {
private int id;
private String className;

public HelloBeanClass(){}
public HelloBeanClass(int id, String className){
this.id=id;
this.className=className;
}
public void setId(int id){
this.id=id;
}
public int getId(){
return this.id;
}

public void setClassName(String className){
this.className=className;
}
public String getClassName(){
return this.className;
}

}






Java code
package aixs2listtest;
import java.util.*;

import org.apache.axiom.om.OMNode;
import org.apache.axiom.om.OMElement;
import org.apache.axis2.util.StreamWrapper;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.axiom.om.impl.llom.factory.OMXMLBuilderFactory;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axis2.databinding.utils.BeanUtil;
import javax.xml.namespace.QName;
public class HelloSource {
public OMElement echo(){
HelloBeanClass hbc1=new HelloBeanClass(11,"moonlight1");
HelloBeanClass hbc2=new HelloBeanClass(22,"moonlight2");
HelloBeanClass hbc3=new HelloBeanClass(33,"moonlight3");
List<HelloBeanClass> clss=new ArrayList<HelloBeanClass>();
clss.add(hbc1);clss.add(hbc2);clss.add(hbc3);
HelloBean hbs=new HelloBean("moon",1,clss);
HelloBean hbs2=new HelloBean("mo"+"\non2",2,clss);
HelloBean hbs3=new HelloBean("moon3",3,clss);
List<HelloBean> list = new ArrayList<HelloBean>();
list.add(hbs);
list.add(hbs2);
list.add(hbs3);
OMElement omElement = BeanUtil.getOMElement(new QName("HelloBeans"), list.toArray(), new QName("HelloBean"), false, null);

return omElement;
}
public static void main(String [] args){
HelloSource h=new HelloSource();
System.out.println("asd"+"\n"+"sdf");
h.echo();
}

}