2015-06-24 49 views
1

我正在试图从我的查询中获取ResultSet中的数字,如下面的代码中所示。查询检索数字5.我怎样才能从ResultSet中获得这个数字?获取结果集中的行数

代码:

String sql_count_stop = "select count(*) FROM behaviour where mac = ? "; 
PreparedStatement preparedCount = con.prepareStatement(sql_count_stop); 
preparedCount.setString(1, macD); 
ResultSet rsCount = preparedCount.executeQuery(); 
while(rsCount.next()){ 

} 
+0

ResultSet :: getInt(String)方法有什么问题? –

回答

3

您可以修改您的查询

"SELECT count(*) AS totalCount FROM behaviour WHERE mac = ? "; 

再使用,

macId= rsCount.getInt("totalCount"); 
0

您可以修改你的SQL语句:(加入AS 'countMacs'

select count(*) as 'countMacs' FROM behaviour where mac = ? 

然后得到一个值

while(rsCount.next()){ 
    int count = rsCount.getInt("countMacs"); 
} 
1

或使用位置rsCount.getInt(1),你并不需要一个列别名。

此外,由于只有一行,if(rsCount.next())while一样好,在我看来更清楚地表明这个逻辑只会执行一次。