日期:2014-05-19  浏览次数:20591 次

我的这个简单的算法错在哪呀?再简单不过的代码了 我真笨
我想从数据库中查找书的类型号为1(即goodstype=1)的记录数,可结果为什么总是0呢???!!我写的代码如下:
public class GoodsBeanBo {

   
Connection ct=new ConnDb().getConn();
ResultSet rs=null;
Statement st=null;
public int getrowCount(int booktype){
int rowcount=0;
try {
st=ct.createStatemen(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=st.executeQuery("select count(*) from goods where goodstype="+booktype);
if(rs.next()){
rowcount=rs.getInt(1);
}

}catch (Exception e){
e.printStackTrace();
}finally{
this.close();
}
return rowcount;
}

------解决方案--------------------
你执行了getrowCount(1)得到的返回值是0么? 看看控制台有异常输出么
------解决方案--------------------
看看控制台有出错信息吗?
没有的话,去数据库那里执行
select count(*) from goods where goodstype=1
看看,输出是几。
------解决方案--------------------
rowcount+=rs.getInt(1);

------解决方案--------------------
先定义一个变量String sql = "select count(*) from goods where goodstype="+booktype;
执行之前
System.out.println("sql=="+sql);
输出来看看,如果还是0 的话,就是你的连接链的库不对,或是其他的问题了