关于resultset

lllindada 2010-11-26 11:35:48
有一个表onlineUser用于记录当前在线的人数,表中charRoom字段表示在线人所在的房间。
public int countUser(String chatRoom)
throws SQLException,ClassNotFoundException
{
BaseConn conn=null;
try
{
conn=new BaseConn();
String sql="select count(*) As intCount from onlineUser where chatRoom=?";
PreparedStatement ps=conn.preparedStatement(sql);
ps.setString(1,chatRoom);
ResultSet rs=conn.executeQuery();--这里rs里存的是一个数字还是一个集合?如果是集合的话包括哪个字段,只有intCount吗?如果表中没有记录,那么这个查询结果是什么?
if (rs.next())
return rs.getInt("intCount");
else
return 0;
}catch(SQLException ex)
{
ex.printStackTrace();
throw ex;
}catch(ClassNotFoundException ex)
{
ex.printStackTrace();
throw ex;
}finally
{
conn.close();
}
}
...全文
138 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jales 2010-11-26
  • 打赏
  • 举报
回复
select count(*) As intCount from onlineUser where chatRoom=?
这条语句返回的RS是一个一行一列的结果集,而且是数字类型(short,int,long)
  • 打赏
  • 举报
回复
其实这个东西自己想要知道到底是什么,自己就可以测试出来,遍历一下你的结果集不就好了!
  • 打赏
  • 举报
回复
返回的仍然是一个结果集,这个结果集里只存在一个int值,如果表中没有数据,结果集中存的是0,还有你的代码写错了,ResultSet不是通过Connection对象返回的,而是通过PreParedStatement或者Statement对象
lllindada 2010-11-26
  • 打赏
  • 举报
回复
楼上,能不能说详细些?
TsingHua2008 2010-11-26
  • 打赏
  • 举报
回复
返回一个Long
lllindada 2010-11-26
  • 打赏
  • 举报
回复
多谢楼上,这里确实写错了
再请问
如果我的onlineUser中有三个字段id,name,chatroom
有三条记录 1 ,kk,room1
2 ,kk2,room1
3 ,kk3,room2
我要执行的sql语句是select count(*) As intCount from onlineUser where chatRoom=room1
请问返回的rs是什么?
fengxuegupo 2010-11-26
  • 打赏
  • 举报
回复
你应该写成ResultSet rs=ps.executeQuery();
lllindada 2010-11-26
  • 打赏
  • 举报
回复
补充上面问题,我要执行的sql语句是select count(*) As intCount from onlineUser where chatRoom=room1
fengxuegupo 2010-11-26
  • 打赏
  • 举报
回复
ResultSet rs=conn.executeQuery();你这句代码得到的rs永远为空
lllindada 2010-11-26
  • 打赏
  • 举报
回复
如果我的onlineUser中有三个字段id,name,chatroom
有三条记录 1 ,kk,room1
2 ,kk2,room1
3 ,kk3,room2
请问返回的rs是什么?
TsingHua2008 2010-11-26
  • 打赏
  • 举报
回复
它是一个结果集,包含你查询的所有记录,包括所有字段,没有记录为null
lllindada 2010-11-26
  • 打赏
  • 举报
回复
另外请详细解释一下其中的sql语句select count(*) As intCount from onlineUser where chatRoom=?
是要把onlineUser表中的纪录数以数字的形式返回吗?

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧