关于一个mysql的语法问题,别看那么多代码,只要看与数据库有关的就行
调试的时候,网页可以正常运行,但后台出现如下错误:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
我的一个java类代码如下:
我怀疑问题出在showMyCart()方法中,因为我数据库中,goodsid是int型,而在该方法中的goodsid是String型,但是却能正常运行,我很纳闷……请指教!谢谢
public class MyCar {
HashMap hm=new HashMap();
private Connection ct=null;
private Statement st=null;
private ResultSet rs=null;
//添加货物
public void addGoods (String goodsid,int goodsnum){
hm.put(goodsid,goodsnum);
}
//删除货物
public void delGoods (String goodsid){
hm.remove(goodsid);
}
//清空货物
public void clear(){
hm.clear();
}
//修改货物
public void upGoods (String goodsid,int newnum){
hm.put(goodsid,newnum);
}
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()){
String goodsId=(String)it.next();
//int goodsid=Integer.parseInt(goodId);
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();
}
}
------解决方案--------------------能否把最后执行之前,组装完毕的SQL发出来看看?
就是:
System.out.println("SQL:\n" + sql); // 增加这句话
rs=st.executeQuery(sql);
------解决方案--------------------String sql="select * fro