There are several reasons to avoid using this method:
ResultSet.TYPE_FORWARD_ONLY
. Database drivers will throw an exception if
not supported. ResultSet
is unclear. Database drivers may return true
or
false
in this case . ResultSet.next()
is a good alternative to ResultSet.isLast()
as it does not have the mentioned issues. It is always
supported and, as per specification, returns false
for empty result sets.
Refactor your code to use ResultSet.next()
instead of ResultSet.isLast()
. Be cautious of its different semantics and side
effects on cursor positioning in the result set. Verify that your program logic is still valid under these side effects and otherwise adjust it.
ResultSet results = stmt.executeQuery("SELECT name, address FROM PERSON"); StringBuilder sb = new StringBuilder(); while (results.next() && !results.isLast()) { // Noncompliant sb.append(results.getString("name") + ", "); } sb.append(results.getString("name")); String formattedNames = sb.toString();
ResultSet results = stmt.executeQuery("SELECT name, address FROM PERSON"); List<String> names = new ArrayList<>(); while (results.next()) { // Compliant, and program logic refactored names.add(results.getString("name")); } String formattedNames = names.stream().collect(Collectors.joining(", "));