Help with jdbctemplate / query / rs

Asked

Viewed 46 times

0

Well I’m trying to give a select in the database to get the data from a String:

final String queryPorLogin = "SELECT * from usuarios where login=? ";
@Autowired
private JdbcTemplate jdbcTemplate;

@Override
public boolean validar(String login) {
    return this.jdbcTemplate.queryForObject(queryPorLogin, new Usuarios(), 
    login);
}

but I have this mistake:

inserir a descrição da imagem aqui

I believe that is not the correct query to use in this case why I want to make a select in the database and if there is a result use the resultset to make a check ( of passwords with bcrpyt )someone could help me?

  • Some reason not to set up a AuthenticationManager using Spring Security?

  • @nullptr did not know of the existence of the same, but I have already managed to solve this method ( I will seek to know what server )

  • it would be interesting for the community if you answered your own question when you were able to solve the problem, your answer can contribute to solving the problem of other people in the future

  • @nullptr opa I did not know I had this possibility I will answer now.

  • @nullptr ready bro, I didn’t know there was such a possibility, if you can take a look and see where I can improve .

1 answer

0

Well after much search on the subject I ended up coming to this method:

public boolean verificar(String login, String senha) {
    try {
    Usuarios usuarios = this.jdbcTemplate.queryForObject(queryPorLogin, new Object[]{login}, 
    new RowMapper<Usuarios>() { 
    public Usuarios mapRow(ResultSet rs, int rowNum) throws SQLException { 
    Usuarios user = new Usuarios();
    if(BCrypt.checkpw(senha, rs.getString("senha"))) {
        user.setIdUsuario(rs.getLong("idUsuario"));
        user.setLogin(rs.getString("login"));
        user.setSenha(rs.getString("senha"));
    }
    return user;
    } 
    });
        if(usuarios.getIdUsuario() != null) {
        System.out.println(usuarios.toString()); 
        return true;
        }else {
        return false;
        }
    }catch (EmptyResultDataAccessException e) {
        System.out.println("Senha errada");
        return false;
    }
}

i first select in the database, and if the password encrypted with bcrypt is equal to the password entered I will set the user and then make a check if the user id is different from null ( if it returns a true, which I will use in my controller)

Catch (Emptyresultdataaccessexception e) { System.out.println("Wrong password"); Return false; }

the catch I searched a lot because before I was not using the Try catch and when there was no result generated a joptionpane error.

In case anyone can help me where to improve on the code I would be grateful!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.