我写了一个类是这样的,很简单的大家帮忙看看哈,主要是红字部分呀
我想问的是,红字显示部分的方法是否参数都得是String型呀,hm.get()方法的参数都必须是String型吗?另外,在public ArrayList showMyCart()方法中,it.next()的返回值是什么类型呀,能否把它转化为int类型,即int goodsid=(Integer)it.next(); 这句代码是否正确,谢谢哈 高分悬赏 所有的分全都给你们了 谢谢了
package mycar.wl.com;
import goods.wl.com.GoodsBean;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import conndb.wl.com.*;
public class MyCar {
HashMap hm=new HashMap();
private Connection ct=null;
private Statement st=null;
private ResultSet rs=null;
//添加货物
public void addGoods (int goodsid,int goodsnum){
hm.put(goodsid,goodsnum);
}
//删除货物
public void delGoods (int goodsid){
hm.remove(goodsid);
}
//清空货物
public void clear(){
hm.clear();
}
//修改货物
public void upGoods (int goodsid,int newnum){
hm.put(goodsid,newnum);
}
//根据goodsid得到数量
public int getGoodsnumById(int goodsid){
return (Integer)hm.get(goodsid);
}
public ArrayList showMyCart(){
ArrayList al=new ArrayList();
try{
//组织sql
String sql="select * from goods where goodsid in ";
//使用迭代器
Iterator it=hm.keySet().iterator();
String sub="(";
while(it.hasNext()){
int goodsid=(Integer)it.next(); // int goodsid=Integer.parseInt(goodsId);
if(it.hasNext()){
sub+=goodsid+",";
}else{
sub+=goodsid+")";
}
}
sql+=sub;
ct=new ConnDb().getConn();
st=ct.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs=st.executeQuery(sql);
//ps=ct.prepareStatement(sql);
// rs=ps.executeQuery();
while(rs.next()){
GoodsBean gb=new GoodsBean();
gb.setGoodsid(rs.getInt("goodsid"));
gb.setGoodsname(rs.getString("goodsname"));
gb.setGoodsinfo(rs.getString("goodsinfo"));
gb.setGoodsprice(rs.getString("goodsprice"));
gb.setGoodsnum(rs.getInt("goodsnum"));
gb.setPublisher(rs.getString("publisher"));
gb.setGoodstype(rs.getInt("goodstype"));
gb.setGoodsphoto(rs.getString("goodsphoto"));
gb.setGoodsauthor(rs.getString("goodsauthor"));
al.add(gb);
}
}catch(Exception e){
e.printStackTrace();
}finally{
this.close();
}
return al;
}
public void close(){
try{
if(rs!=null){
rs.close();
rs=null;
}
if(st!=null){
st.close();
st=null;
}
if(ct!=null){
ct.close();
ct=null;
}
}catch(Exception e){
e.printStackTrace();
}
}
}
------解决方案--------------------
1、hm.get()方法的参数都必须是String型吗?