Results of MySQL query from java

0

Good morning,

By making a database query from java, I am unable to know how to interpret the results. I have the following code, which should find me in a database if the game is included and if the column also belongs to "-".

Statement st = comprueba.createStatement();
boolean isInsert;
    try (PreparedStatement ps = comprueba.prepareStatement("SELECT * FROM info_XboxOne WHERE Juego = ? AND Pertenece = ?")) {
     ps.setString(1, game);
     ps.setString(2, "-");              

     try (ResultSet rs = ps.executeQuery()) {
         isInsert = !rs.next();
     }
}

if (!isInsert) { //si se cumple esta condicción significa que se cumplen las condiciones de juego base y que no pertenezca a ninguno
     resultado = "Si";
}

The fact is that it is not working for me. If it gives me some result it is because both conditions are met, and then the variable resultado will become "Yes". But something I have to be doing wrong. Is there any similar way to see the result that I get from the query as var_dump in PHP?

    
asked by JetLagFox 13.05.2017 в 03:18
source

1 answer

-1

In JAVA to identify if a query (SQL query) returns results you must use the method execute (), which returns a boolean value true: if the query is a select and false otherwise:

boolean isSelect = comprueba.prepareCall("select * from tu_tabla").execute();

Here is an example of what your code should look like:

boolean isSelect = comprueba.prepareCall("select * from tu_tabla").execute();
if(isSelect)
{
    //de ser verdadero isSelect debemos obtener los datos del Select con la clase ResultSet
    ResultSet resultados = comprueba.prepareCall("select * from tu_tabla").executeQuery();
    //debemos iterar sobre resultados hasta que la condicion pase a false(ya no habran mas filas por recorrer)
    while(resultados.next())
    {
        //obtenemos el String de la columna uno
        System.out.println(resultados.getString(1));
    }

}
else
{
    //en caso de que isSelect sea false, indica que la consulta SQL no es un Select , por lo tanto sera un INSERT,UPDATE O DELETE por lo que debemos validar que se hayan modificado las filas
    //el metodo executeUpdate() nos retornara un long indicando la cantidad de filas afectadas en caso de ser 0 ninguna fila fue modificada, de caso contrario nos indicara la cantidad de filas afectadas
    int filasModificadas = comprueba.prepareCall("select * from tu_tabla").executeUpdate();
}

I hope it serves you Greetings

    
answered by 13.05.2017 в 04:14