高手请帮助我一下,有高分散!

davidforever 2006-03-20 02:58:46
我用JAVABEAN编写了一个访问数据库的组件,当我用ArrayList返回记录集到页面的时候,在页面也定义了一个ArrayList变量来引用返回的ArrayList,但是访问该ArrayList的时候怎么显示不出来,也就是怎么不能读取其中的记录?其中Connector类是用来保存加载类,数据库路径等信息的Bean,ProductBean是用来保存每条产品记录的Bean
其中代码如下:

public ArrayList queryProducts(Connector connectNewcomer,String subCategory)throws SQLException,ClassNotFoundException{

// Load driver specified from class "Connector" and get a connection from database "Newcomer"

Class.forName(connectNewcomer.getDriverClass());
Connection connectProductInventory=DriverManager.getConnection(connectNewcomer.getConnectivePath(),connectNewcomer.getUser(),connectNewcomer.getPassword());

// Judge how to display results from productInventory according to parameter "subCategory"

if(subCategory==null||subCategory==""){
String queryProducts="select productNO,productName,productPrice,manufacturer,productQuantity,productPicture from productInventory where subCategory=CPU";
java.sql.Statement queryStatement=connectProductInventory.createStatement();
ResultSet productsResultSet=queryStatement.executeQuery(queryProducts);

// Create a instance of class "ArrayList" to store results queried from database

ArrayList productsList=new ArrayList();
while(productsResultSet.next()){

// Create a instance of class "ProductBean" and initialize it

ProductBean partProduct=new ProductBean();
partProduct.setProductNO(String.valueOf(productsResultSet.getLong("productNO")));
partProduct.setProductName(productsResultSet.getString("productName"));
partProduct.setProductPrice(String.valueOf(productsResultSet.getDouble("productPrice")));
partProduct.setManufacturer(productsResultSet.getString("manufacturer"));
partProduct.setProductQuantity(productsResultSet.getString("productQuantity"));
partProduct.setProductPicture(productsResultSet.getString("productPicture"));

// add it into array list

productsList.add(partProduct);
}

// close connection and return this array list

productsList.trimToSize();
productsResultSet.close();
queryStatement.close();
connectProductInventory.close();
return productsList;
} else{
String queryProducts="select productNO,productName,productPrice,manufacturer,productQuantity,productPicture from productInventory where subCategory=?";
PreparedStatement queryPrepared=connectProductInventory.prepareStatement(queryProducts);
queryPrepared.setString(1,subCategory);
ResultSet productsResultSet=queryPrepared.executeQuery();

// Create a instance of class "ArrayList" to store results queried from database

ArrayList productsList=new ArrayList();
while(productsResultSet.next()){

// Create a instance of class "ProductBean" and initialize it

ProductBean partProduct=new ProductBean();
partProduct.setProductNO(String.valueOf(productsResultSet.getLong("productNO")));
partProduct.setProductName(productsResultSet.getString("productName"));
partProduct.setProductPrice(String.valueOf(productsResultSet.getDouble("productPrice")));
partProduct.setManufacturer(productsResultSet.getString("manufacturer"));
partProduct.setProductQuantity(productsResultSet.getString("productQuantity"));
partProduct.setProductPicture(productsResultSet.getString("productPicture"));

// add it into array list

productsList.add(partProduct);
}

// close connection and return this array list

productsList.trimToSize();
productsResultSet.close();
queryPrepared.close();
connectProductInventory.close();
return productsList;
}
}
...全文
48 点赞 收藏 回复
写回复
回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2006-03-20 02:58
社区公告
暂无公告