使用Java从Mysql中获取单行结果而不在循环中迭代 [英] Fetching single row result without iterating in loop from Mysql using Java
本文介绍了使用Java从Mysql中获取单行结果而不在循环中迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个只返回行数的简单查询.
I have a simple query that returns only count of rows.
select count(*) cnt from table
我可以通过遍历结果集来读取它.
I can read it by iterating through resultset.
喜欢
while(rs.next()){
int rowCount= rs.getInt(cnt);
}
但是有没有什么办法,使用它我可以不用循环直接得到计数.
But is there any way,using which I can get count directly without looping.
推荐答案
怎么样:
int rowCount = rs.next() ? rs.getInt(cnt) : -1;
虽然它并没有为您节省多少
It doesn't save you much though
这篇关于使用Java从Mysql中获取单行结果而不在循环中迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文