Transform boolean from sql to boolean java?

0

I'm in a problem, I have a SELECT that asks for ususario (int) , contraseña (String) and tipo (boolean) of a database, I keep them in a Object [][] , all right there, but when I want to save those 3 values in other variables separated by their type, the int and the String keep good, but the boolean never takes the 1 that reaches it as a true , that is, it is always in false

code:

result.next();{

                datosObt[0][0] = result.getString( "usuario" );
                datosObt[0][1] = result.getString( "contraseña" );
                datosObt[0][2] = result.getString( "tipo" );//es 1
                //aquí es donde obtengo los 3 datos y los guardo en el Object[][]
                }
            result.close();
            //me salto el catch para la pregunta


            //abajo los paso a los distintos tipos en los que necesito
            userObt= Integer.parseInt(datosObt[0][0].toString());
            pwdObt= datosObt[0][1].toString();
            tipoObt= Boolean.parseBoolean(datosObt[0][2].toString());//es 1 pero el tipoObt permanece false

As I say, the first two "transformations" work well, but the third to boolean did not want to work. I hope you can give me a hand. Thanks

    
asked by Vizz3rdriX 25.10.2018 в 06:59
source

2 answers

2

You should use the methods of ResultSet , you would completely save the conversion of each value, for example to get "user" you should only use the result.getInt("usuario") method that automatically returns an integer and for a boolean:

boolean b = result.getBoolean("tipo");

I recommend reading the API-ResultSet so you can see all the methods that it offers you, greetings.

    
answered by 25.10.2018 в 07:39
0

The method of the wrapper class of Boolean converts a String to boolean, but what it expects is a "true" or "false".

As you are getting 0 or 1 you will have to do it by hand, that is to say check If what comes to you is a 1 or a 0 and set a value. You can use a ternary operator to avoid lines of code. It would be something like this:

tipoObt= datosObt[0][2] == "1" ? true : false;

You should check that dataObt [0] [2] is not null and all that kind of validations, but that will work for you.

    
answered by 25.10.2018 в 07:29