How do I validate login when the user changes fields?

Asked

Viewed 14 times

0

How can I check if a login has already been registered? Below is an excerpt from an attempt I made. If you can help me thank you.

This is an excerpt from the DAO layer.

public boolean validarLogin () throws SQLException, ClassNotFoundException {
    String login = null;
    this.conn=new ConnectionFactory().getConnectionFactory();
    boolean testa;
    String LOGIN_SQL ="SELECT login FROM pessoa WHERE login = ?";
    PreparedStatement st = conn.prepareStatement(LOGIN_SQL);
    st.setString(1, login);
    ResultSet rs = st.executeQuery();
    if (rs.next()){
        testa = rs.getBoolean(LOGIN_SQL);
    }
    else {
        testa = false;
    }
    return testa;
}

Here is the code that appears in the VIEW event.

 PessoaDAO pDAO = new PessoaDAO();
   boolean login = false;
    try {
        login = pDAO.validarLogin();
    } catch (SQLException ex) {
        Logger.getLogger(cadastrar_pessoa.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(cadastrar_pessoa.class.getName()).log(Level.SEVERE, null, ex);
    }

    System.out.println(login);

I am studying on my own and am having some beginner problems, as you can see. (rs)

  • https://answall.com/questions/276585/howto checkout%C3%A1rio-j%C3%A1-was-registered-in-database

  • 1

    It is necessary to pass a parameter to validarLogin. Ex: public boolean(String login); Then remove String login = null and finally pDAO.validarLogin("seu-login");

  • It worked perfectly. I appreciate the help.

No answers

Browser other questions tagged

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