How to modify this query in SQL

0

I have this query in SQL:

SELECT datname FROM pg_catalog.pg_database WHERE LOWER(datname) = LOWER(BaseDatos);

But I am running it from java with jdbc, and I need to pass the result of the query to a ResultSet, but the problem is that when the database does not exist the program gives me a NullPointerException. My question is: How can I modify my query so that I always return something? For example: true if the database exists and false if not. Thanks and regards.

    
asked by Luis Ramiro 06.06.2018 в 04:37
source

2 answers

0

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.

    
answered by 06.06.2018 / 14:55
source
0

I think you should use try / catch for exception handling. I do not know how you will have implemented the connection with BBDD exactly, but here is a small template about how it would be:

public ResultSet execSQL() {
    ResultSet rs = null;

    try {
        //Estableces la conexion
        throw new SQLException();
    } catch (SQLException | NullPointerException e) {}
    //Esta parte se ejecutara en caso de que salte una excepción del tipo SQLException o NullPointerException.
    //Te encargas en esta parte de liberar lo que tengas iniciado de la base de datos       

    //En caso de que haya ido bien, estaras devolviendo el ResultSet relleno, si ha ido mal, estaras devolviendo null
    return rs;
}

There are more "correct" ways to do it, which may come in handy if you need to have better error control, although I think that will be worth it.

    
answered by 06.06.2018 в 10:33