开发者

Check to see if ResultSet is Null if not then get int

开发者 https://www.devze.com 2023-01-02 14:06 出处:网络
I开发者_如何学运维 want to be able to get check to see if there is a result in my resultset. Todo this i would execute:

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

精彩评论

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