这不是一个很简单的手术吗?但是,我看到既没有size()也没有length()方法。
当前回答
ResultSet rs = ps.executeQuery();
int rowcount = 0;
if (rs.last()) {
rowcount = rs.getRow();
rs.beforeFirst(); // not rs.first() because the rs.next() below will move on, missing the first element
}
while (rs.next()) {
// do your standard per row stuff
}
其他回答
ResultSet rs = ps.executeQuery();
int rowcount = 0;
if (rs.last()) {
rowcount = rs.getRow();
rs.beforeFirst(); // not rs.first() because the rs.next() below will move on, missing the first element
}
while (rs.next()) {
// do your standard per row stuff
}
获取ResultSet大小的方法,不需要使用数组列表等
int size =0;
if (rs != null)
{
rs.beforeFirst();
rs.last();
size = rs.getRow();
}
现在你会得到大小,如果你想打印ResultSet,在打印之前使用以下一行代码,
rs.beforeFirst();
这是一种简单的行计数方法。
ResultSet rs = job.getSearchedResult(stmt);
int rsCount = 0;
//but notice that you'll only get correct ResultSet size after end of the while loop
while(rs.next())
{
//do your other per row stuff
rsCount = rsCount + 1;
}//end while
String sql = "select count(*) from message";
ps = cn.prepareStatement(sql);
rs = ps.executeQuery();
int rowCount = 0;
while(rs.next()) {
rowCount = Integer.parseInt(rs.getString("count(*)"));
System.out.println(Integer.parseInt(rs.getString("count(*)")));
}
System.out.println("Count : " + rowCount);
我也有同样的问题。以这种方式使用ResultSet.first()在执行后解决它:
if(rs.first()){
// Do your job
} else {
// No rows take some actions
}
文档(链接):
布尔第一次() 抛出SQLException异常 将光标移动到此ResultSet对象的第一行。 返回: 如果光标位于有效的 行;如果结果集中没有行,则为False 抛出: SQLException—如果发生数据库访问错误;在封闭的结果集或结果集类型为TYPE_FORWARD_ONLY时调用此方法 SQLFeatureNotSupportedException—如果JDBC驱动程序不支持 这个方法 自: 1.2
推荐文章
- 在流中使用Java 8 foreach循环移动到下一项
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 如何在Ruby On Rails中使用NuoDB手动执行SQL命令
- 查询JSON类型内的数组元素
- 用Java计算两个日期之间的天数
- 确定记录是否存在的最快方法
- 如何配置slf4j-simple
- 在Jar文件中运行类
- 带参数的可运行?
- 获得PostgreSQL数据库中当前连接数的正确查询
- 在SQL选择语句Order By 1的目的是什么?
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 我可以在Java中设置enum起始值吗?
- Java中的回调函数
- c#和Java中的泛型有什么不同?和模板在c++ ?