Doubts when applying a DAO method

Asked

Viewed 59 times

0

Well I have 2 classes the User class and the Request class

my user class is like this:

public class Usuario {
    private static Usuario instance;
    private Long id;
    private String login;
    private String senha;

    public Usuario(Long id, String login, String senha){
    this.id=id;
    this.login=login;
    this.senha=senha;
    }

    public Usuario(String login, String senha){
    this.login=login;
    this.senha=senha;
    }
    public Usuario(Long id){
    this.id = id;
    }
   public static Usuario getInstance() {
        if (instance == null)
            instance = new Usuario();
        return instance;
    }

I created a constructor to store the ID, first doubt how I could apply this to my login validation method to save the ID: my validation method:

public void validaLogin(){
    UsuarioDAO dao = new UsuarioDAO();
    List<Usuario> usuarios = dao.getList();
    Usuario usuario = Usuario.getInstance();

    for(int x = 0; x< usuarios.size(); x++){
        if(jLogin.getText().equals(usuarios.get(x).getLogin()) && jSenha.getText().equals(usuarios.get(x).getSenha())){
            Principal pr = new Principal ();
            x = usuarios.size();
            fecha();
            try {
                pr.start(new Stage());
            } catch (Exception ex) {
                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else{
            if(x == usuarios.size()-1){
                Alert al = new Alert(Alert.AlertType.ERROR);
                al.setHeaderText("Login Invalido");
                al.show();
            }
        }
    }
}

the User:

public List<Usuario> getList(){
   List<Usuario> usuarios = new ArrayList<>();
   String sql = "SELECT * FROM usuario";
    try {
        PreparedStatement stmt = con.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery();
        while(rs.next()){
            Usuario u = new Usuario();
            u.setId(rs.getLong("id_usuario"));
            u.setLogin(rs.getString("login"));
            u.setSenha(rs.getString("senha"));
            usuarios.add(u);
        }
        stmt.close();
        rs.close();
        con.close();  
    } catch (SQLException ex) {
        return null;
    }
    return usuarios;
}

I wanted you to keep the id on all the Trials that are opened as I would get it?

1 answer

1


Usually the login validation is done in the database, it is not a good practice to store in your memory a list of users with login and password not encrypted.

I’d do it this way:

String sql = "SELECT id_usuario, nome FROM usuario where login = ? and senha = ?";
try {
    PreparedStatement stmt = con.prepareStatement(sql);

    ps.setString(1, login);
    ps.setString(2, criptografar(senha)); // método qualquer de criptografia

    ResultSet rs = stmt.executeQuery();
    if (rs.next()){
        Usuario usuarioLogado = new Usuario(rs.getLong("id_usuario"));
        usuarioLogado.setNome(rs.getString("nome")); // vai exibir mesmo o login?
        return usuarioLogado;
    }
    stmt.close();
    rs.close();
    con.close();  
} catch (SQLException ex) {
    return null;
}
return null;

If this information is to be on all screens it may be the case to use a global variable in the main class. (Main.userLogado.getID())

  • vlw man, I had seen that you study in uesc, I’m from Itabuna .

  • There’s no way you can tell me if this part of the code is correct ?

  • public user validateSenha(User u){ String sql = "SELECT id_usuario, name FROM user Where login = ? and password = ?" ;&#xA; try {&#xA; PreparedStatement stmt = con.prepareStatement(sql);&#xA; ResultSet rs = stmt.executeQuery();&#xA; u.setLogin(rs.getString("login"));&#xA; u.setSenha(rs.getString("senha")); &#xA; if (rs.next()){&#xA; Username = new User(rs.getLong("id_usuario")); userLog.setName(rs.getString("name")); // will even display login? user Return; }

  • and have you give an example how I would create this global variable? It would be in the USER class is not?

  • The "global" variable would be a variable declared as Static in the same class where vc has the main method. I don’t know if the code is 100% correct, I can’t test it, but the syntax is the one I put in. I graduated from UESC myself, but I don’t live there anymore. Greetings

  • i am using javafx and I have some screens, my doubt was in which class declare this global variable, ex I have login screen/main/register/etc

Show 1 more comment

Browser other questions tagged

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