开发者

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,

Dean


id = 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜