I recommend that all the control of the answer by persistence be resolved in logic. That is, you should not modify your SQL query.
Why should I not change my SQL query? .
It is not convenient for you to modify your SQL query because it is written in a general way, that is, with standard SQL. The advantage that you have when writing it in such a way is that your persistence layer will be as cohesive as possible, that is, you can change the storage engine and your queries will continue to work in each of them. On the other hand, if you decide to use native functions of a certain database engine to corroborate the result of a query, these functions may not work in the other engines, and therefore, you have to make big changes in your persistence layer in case of changing the database engine.
Solution
I recommend that you learn to use the TRY / CATCH blocks to capture the exceptions.
Let's see how it would look like in an example:
public void ejemploPersistencia(){
//Utilizamos el bloque try/catch mencionado mas arriba
try{
//Realizamos la consulta SQL y la capturamos en un objeto de tipo CachedRowSet
CachedRowSet respuestaSQL = tuConsultaSQL();
//Recorres tu respuesta
while(respuestaSQL.next()){
/*
Haces todo lo que tengas que hacer.
*/
}
}catch(NullPointerException ex){
/*
Aquí solucionas el problema si quieres
*/
}
}
Recommendation
Try not to use the ResultSet object. Why?. Because the ResultSet object must be manipulated while the connection is active (that is, open), which is not recommended. Whenever you have captured the values that you need from persistence, you must release the resources of the engine by closing the connection to the database independently if you have already begun to manipulate the data logically. The solution for this is to use the CachedRowSet object.