我写了一个类是这样的,很简单的大家帮忙看看哈,主要是红字部分呀
我想问的是,红字显示部分的方法是否参数都得是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();
}
}
}
我想问的是,红字显示部分的方法是否参数都得是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();
}
}
}
要看key值能否转化为int,如果是不能转化为int值的key那就报错了。
比如hm.put("a",abc);循环到key值为a的位置,就报错了。
很明显吧,字符串“123”可以转成数字123;
而字符串“abc”如何转成数字呢?字符串“中国”如何转成数字呢?字符串“1@”如何能转成数字呢?实际上要转为整型,String是不能这么强转成Integer的。用下面这种方法
int goodsid=Integer.parseInt(it.next().toString());
hm.put(0, 1);
hm.put(2, 3);
System.out.println(hm.get(2));这些代码会输出3.说明get()方法的参数可以使int。对于第二个问题,你这样做是对的。至于2楼提到的问题我想可以不用考虑,因为你的 MAP中KEY存入全部是数字吧(goodsid),因此都可以按照你的int goodsid=(Integer)it.next(); 这个方法执行成功。it.next()返回的是Object类型数据。