Check to see if ResultSet is Null if not then get int
I开发者_如何学运维 want to be able to get check to see if there is a result in my resultset. Todo this i would execute:
if(rs.next()){
boolean = true;
}
However i want to check to see if the value is in the database if so retrieve it:
while(rs.next())
id = rs.getInt("id);
How would i go about combining the two? I want -2 to be returned if the resultset is empty.
Thanks in Advance, Deanid = rs.next() ? rs.getInt("id") : -2;
like that?
Just use an if-else
:
if (resultSet.next()) {
return resultSet.getInt("id");
} else {
return -2;
}
Also see this answer for more hints how to handle existence, zero/one, zero/more results.
精彩评论