我是个初学者。我做了一个简单的例子,遇到点问题,希望大家可以帮忙看看
首先这里是个pojo类,名字为Product.java
[color=red] public class Product {
private String pid;
private String pname;
private double price;
private int cnt;
private String ext;
public int getCnt() {
return cnt;
}
public void setCnt(int cnt) {
this.cnt = cnt;
}
public String getExt() {
return ext;
}
public void setExt(String ext) {
this.ext = ext;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}[/color]
[b]接着我做了一个数据库连接的文件,,名字为:DBAccess.java
[/b][color=red]import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBAccess {
private static Connection _conn = null;
public static Connection getConn() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
_conn = DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:ICSS", "PRODUCT", "icss");
} catch (Exception e) {
e.printStackTrace();
}
return _conn;
}
public static void closeConn(Statement stmt, ResultSet rs, Connection conn) {
try {
if (rs != null) {
rs.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void closeConn() {
try {
if (_conn == null || _conn.isClosed()) {
return;
} else {
_conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void execUpdate() {
}
public ResultSet exceQuery(String sql) {
ResultSet rs = null;
try {
if (_conn.isClosed()) {
System.out.println("Conn is closed!!");
}
Statement stmt = _conn.createStatement();
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
}[/color]
[b]接着我又做了一个接口:Productoption.java[/b]
[color=red]import java.util.ArrayList;
public interface Productoption {
public ArrayList getAllProducts(); } [/color]//[b]里面有一个返回为ArrayList的方法[/b]
[color=darkred]将这几个文件通过MyEclipse+Tomcat发布之后,进入发布后classes所在的文件夹:我的是C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\SimpleExample\WEB-INF\classes[/color]
在cmd中输入命令:[color=red]C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\SimpleExample\WEB-INF\classes>[/color] [b]java org.apache.axis.wsdl.Java2WSDL -o mb.wsdl -l "http://localhost:80/axis/services/Productoption" -n "urn:Productoption" -p"com.icss.oa.opt" "urn:Produtoption" com.icss.oa.opt.Productoption[/b]
生成了mb.wsdl文件,接着在输入命令:[b] java org.apache.axis.wsdl.WSDL2Java -o . -d Session -s -S true -Nurn:Productoption com.icss.oa.opt mb.wsdl[/b]
生成了许多的.java文件,
最后就是在[b]ProductoptionSoapBindingImpl.java[/b]中添加业务代码了,也就是说具体实现上面接口中的getAllProducts()方法了,[color=red]可是在这里就有问题了[/color]
这里是[b]ProductoptionSoapBindingImpl.java[/b]的代码
public class ProductoptionSoapBindingImpl implements com.icss.oa.opt.Productoption{
public [color=red]java.lang.Object[] getAllProducts()[/color] throws java.rmi.RemoteException {
return null;
}
}
这里返回类型怎么成了[color=red]java.lang.Object[][/color]而我的业务代码应该是
import ....[color=red]这里也要倒入一些包[/color]
public class ProductoptionSoapBindingImpl implements com.icss.oa.opt.Productoption{
public [color=red]java.lang.Object[] getAllProducts()[/color] throws java.rmi.RemoteException {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
conn = DBAccess.getConn();
[color=red] //上面主要负责数据库的连接[/color]
String sql ="select * from product";
[color=red]ArrayList<Product> proList = new ArrayList<Product>(); [/color]
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
Product product;
while(rs.next()){
product = new Product();
product.setPid(rs.getString(1));
product.setPname(rs.getString(2));
product.setPrice(rs.getDouble(3));
product.setCnt(rs.getInt(4));
product.setExt(rs.getString(5));
proList.add(product);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBAccess.closeConn(stmt,rs,conn);
}
[color=red]return proList;[/color]//[color=red]这里肯定是错误的,可是我确实想让他返回一个ArrayList类型 啊,要不然在调用的时候怎么办,如下面我写的test.java来测试这个web 服务[/color]
}
}
}
[b]test.java文件[/b]
import .....//[color=red]这里要倒入一些包[/color]
public class test {
public static void main(String args[]) throws ServiceException, MalformedURLException, RemoteException{
ProductoptionService s = new ProductoptionServiceLocator();
Productoption opt = s.getProductoption();
//[color=red]这里我想用ArrayList因为只有这样才能取出数据库的东西啊[/color]
ArrayList list = opt.getAllProducts();
for (int i = 0; i < list.size(); i++){
Product product = (Product) list.get(i);
System.out.println("pid is" +product.getPid());
System.out.println("cnt is" +product.getCnt());
System.out.println("ext is" +product.getExt());
System.out.println("pname is" +product.getPname());
System.out.println("price is" +product.getPrice());
}
}
}
[b]请问各位大侠,这里怎么修改才可以解决这个问题啊;[/b]
[b]问题补充:[/b]
谢谢您,不过我也试了toArray了,只是以前没用过toArray,可能有点不对,这里是我重新写的关于上面[color=red]ProductoptionSoapBindingImpl.java[/color]
[b]import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import com.icss.oa.entity.Product;
public class ProductoptionSoapBindingImpl implements com.icss.oa.opt.Productoption{
public java.lang.Object[] getAllProducts() throws java.rmi.RemoteException {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
conn = DBAccess.getConn();
String sql ="select * from product";
ArrayList<Product> proList = new ArrayList<Product>();
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
Product product;
while(rs.next()){
product = new Product();
product.setPid(rs.getString(1));
product.setPname(rs.getString(2));
product.setPrice(rs.getDouble(3));
product.setCnt(rs.getInt(4));
product.setExt(rs.getString(5));
proList.add(product);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBAccess.closeConn(stmt,rs,conn);
}
[color=red] java.lang.Object[] pro = null;
pro = (proList).toArray(pro);
return pro;[/color] //这里是使用了toArray
}
}[/b]
接着我又修改了[color=red]test.java文件[/color]
具体如下:
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import javax.xml.rpc.ServiceException;
import com.icss.oa.entity.*;
import com.icss.oa.opt.*;
public class test {
public static void main(String args[]) throws ServiceException, MalformedURLException, RemoteException{
ProductoptionService s = new ProductoptionServiceLocator();
Productoption opt = s.getProductoption();
[color=red]java.lang.Object[] l = opt.getAllProducts();
ArrayList myList = new ArrayList(l.length);[/color] //[color=red]这里我认为要再转换回来,要不然下面的代码无法用 [/color]
for (int i = 0; i < myList.size(); i++) {
Product product = (Product) myList.get(i);
System.out.println("pid is" +product.getPid());
System.out.println("cnt is" +product.getCnt());
System.out.println("ext is" +product.getExt());
System.out.println("pname is" +product.getPname());
System.out.println("price is" +product.getPrice());
}
}
}