当前位置: 技术问答>java相关
请问:有没有方法可以在不移动ResultSet的情况判断查询的记录数是否为0,也就是说什么都没查出来!
来源: 互联网 发布时间:2015-03-25
本文导语: 谢谢 | Test for an empty ResultSet boolean found; ResultSet rs; ... // execute the query and then found = rs.next(); if (found) System.out.println("Record found"); else Sy...
谢谢
|
Test for an empty ResultSet
boolean found;
ResultSet rs;
...
// execute the query and then
found = rs.next();
if (found)
System.out.println("Record found");
else
System.out.println("Record not found");
A nicer way to do the same would be ResultSet rs;
...
if (rs.next()) {
do {
System.out.println("Record found");
} while (rs.next());
}
else {
System.out.println("Record not found");
}
boolean found;
ResultSet rs;
...
// execute the query and then
found = rs.next();
if (found)
System.out.println("Record found");
else
System.out.println("Record not found");
A nicer way to do the same would be ResultSet rs;
...
if (rs.next()) {
do {
System.out.println("Record found");
} while (rs.next());
}
else {
System.out.println("Record not found");
}
|
ResultSet result;
if (result == null) throw Exception("ResultSet is null!");
if (result == null) throw Exception("ResultSet is null!");
|
if (!result.next()) throw Exception("ResultSet is null!");
|
看看这一段,数据集为空时,这要写在else了就可以了
|
Class.forName(DBDriver);
conn = DriverManager.getConnection(DBLocation,sUser,sPassword);
Statement s= conn.createStatement();
在执行查询后
可这样判断 if(s.getResultSet()==null)
表示结果集无数据
conn = DriverManager.getConnection(DBLocation,sUser,sPassword);
Statement s= conn.createStatement();
在执行查询后
可这样判断 if(s.getResultSet()==null)
表示结果集无数据
|
好办
利用ResultSet的内置方法isBeforeFirst(),返回True和False
//这句话我已经试过拉,没问题!
if(!RSStmt.isBeforeFirst()){out.print("该记录集中没有数据");}
else
{//有纪录时的处理!
}
利用ResultSet的内置方法isBeforeFirst(),返回True和False
//这句话我已经试过拉,没问题!
if(!RSStmt.isBeforeFirst()){out.print("该记录集中没有数据");}
else
{//有纪录时的处理!
}
|
if(rs!=null)
{
.....
}
else
{
...}
也挺简单
{
.....
}
else
{
...}
也挺简单
|
(rs!=null)不对的,有时会有问题,我用过,还是路人甲的方法对。