简体   繁体   中英

resultset.next() always returning true when there is no rows returned by the sql statement

Query :

SELECT MAX(A.column2) FROM abcd A where A.column1= ? AND A.column3= 1

Code :

resultSet = statement.executeQuery();
if (resultSet.next()) {
    return resultSet.getInt(1);
}

When I execute this sql query " resultSet.next() " is always giving result "true" even if there are no rows returned and resultSet.getInt(1) is giving "0" , if there are no rows returned.. My question is why resultSet.next() is always true ? ...I want to return -1 if there are no rows returned from the table How can I do that using resultset ?

From the Documentation:

next()

Moves the cursor froward one row from its current position.

It will always move forward one row, even if there are no rows so make sure to check weather the value is not null or ""

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM