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