我试图从我的derby数据库中的特定表中获取计数,但是当我运行代码时,在当前游标位置仍然得到无效的操作。
public int getNumUsers(){
ResultSet rs = null;
int size = -1;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery("select count(*) from USERS");
while(rs.next()){
size = rs.getInt("COUNT");
}
stmt.close();
} catch (SQLException sqe) {
sqe.printStackTrace();
}
return size;
}发布于 2012-12-10 04:13:41
在您的select语句中提供一个计数别名(*)。在mysql中,我们使用as来提供别名,但我不知道是不是这样,但我认为它应该是类似的。
rs = stmt.executeQuery("select count(*) as count from USERS");
while(rs.next()){
size = rs.getInt("count");
}发布于 2012-12-10 04:13:49
将查询更改为
select count(*) As COUNT from USERS或者将函数调用更改为
rs.getInt(1);https://stackoverflow.com/questions/13791330
复制相似问题