I can not get 1 object from my database, problem with SQLite and PreparedStatement

0

I'm doing a system with a full CRUD, the only problem I have is that I can not recover 1 object from my database, I can list all the objects but I do not get only 1, I'm using MVC and java web.

Here is the servlet that runs the search

@Override
    public Usuario_DTO_bean mostrarUsuario(String usu) {
        Usuario_DTO_bean usuarioMostrar= new Usuario_DTO_bean(1, "falla", "falla", "falla", 1, "falla", "falla", "falla");
        try {
            String sql="select * from usuario where usuario =?";
            con=Conector.connect();
            PreparedStatement statement;
            statement=con.prepareStatement(sql);
            statement.setString(1,usu);
            ResultSet resultSet = statement.executeQuery(sql);
            if (resultSet.next()) {
                int id = resultSet.getInt("id");
                String usuario = resultSet.getString("usuario");
                String clave = resultSet.getString("clave");
                String permisos=resultSet.getString("permisos");
                int estado=resultSet.getInt("estado");
                String nombre=resultSet.getString("nombre");
                String apellido_p=resultSet.getString("apellido_p");
                String apellido_m=resultSet.getString("apellido_m");
                usuarioMostrar=new Usuario_DTO_bean(id, usuario, clave, permisos, estado, nombre, apellido_p,apellido_m);
            }
            resultSet.close();
            statement.close();
            con.close();

        } catch (Exception e) {
            // TODO: handle exception
        }
        return usuarioMostrar;



    }

After trying, I think the problem is in this line of code

ResultSet resultSet = statement.executeQuery(sql);

Since after this line does not execute any code, nor a simple println in console and jumps in front of the return, as if the try detected an error and left the block

With this code in a main I execute the function

String registro="SI-84";
    Usuario_DTO_bean usuario;
    Usuario_DTO_bo bo=new Usuario_DTO_bo();
    usuario=bo.mostrarUsuario(registro);
    System.out.println(usuario.getId());
    System.out.println(usuario.getUsuario());

They should know that the user with registration SI-84 exists, but instead of returning me their values, I will return the following

    
asked by angelo1793 24.07.2018 в 17:10
source

2 answers

1

In addition to the error of not putting an exception in the catch as I was told, the problem was that maybe using SQLite does not allow me to use PrepareStatement, since the console error indicated me an SQL error saying that it did not support the PrapareStatement with which I wanted to use the typical "?" to replace the data.

Now instead of using PrepareStatement, just use Statement I made a small change in the sql statement, this works great: D

//Metodo para mostrar 1 solo usuario
    @Override
    public Usuario_DTO_bean mostrarUsuario(String usu) {
        Usuario_DTO_bean usuarioMostrar= new Usuario_DTO_bean(1, "falla", "falla", "falla", 1, "falla", "falla", "falla");
        try {
            String sql="select * from usuario where usuario ='"+usu+"'";
            con=Conector.connect();
            Statement statement;
            statement=con.createStatement();
            //statement.setString(1,usu);
            ResultSet resultSet = statement.executeQuery(sql);
            if (resultSet.next()) {
                int id = resultSet.getInt("id");
                String usuario = resultSet.getString("usuario");
                String clave = resultSet.getString("clave");
                String permisos=resultSet.getString("permisos");
                int estado=resultSet.getInt("estado");
                String nombre=resultSet.getString("nombre");
                String apellido_p=resultSet.getString("apellido_p");
                String apellido_m=resultSet.getString("apellido_m");
                usuarioMostrar=new Usuario_DTO_bean(id, usuario, clave, permisos, estado, nombre, apellido_p,apellido_m);
            }
            resultSet.close();
            statement.close();
            con.close();

        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return usuarioMostrar;
    
answered by 24.07.2018 / 21:04
source
0

First of all you return those values because you are instantiating them at first

Usuario_DTO_bean usuarioMostrar= new Usuario_DTO_bean(1, "falla", "falla", "falla", 1, "falla", "falla", "falla");

From what I see, so far it seems to me that the method is fine. As you say you have to print the exception something like this:

try{
//La lógica
}catch(SQLException ex){
      System.out.println("No se pudo encontrar el usuario " + ex.getMessage());
}

If there is an error, know exactly what error is coming out to help you. Greetings

    
answered by 24.07.2018 в 17:40