Doubts about Select

3
Hello, I'm doing a user / password check in java and I need to perform a query that checks the database for a login and a password for the type in jTextFields . But I can not get anyone to help me, I'm grateful, to help get a sense of what I need here is an example:

sql= "SELECT * FROM login_sistema WHERE usuario_login =(aqui no caso ficaria a variavel do jTextField) AND senha_login =(aqui no caso ficaria a variável do jTextField) ";

But I do not know how to implement in the field marked the variable containing the value of textField tried in several ways but only resulted error.

    
asked by anonymous 05.10.2015 / 17:10

1 answer

3

Use prepared statements to define the values of your query, then send the query to the bank with executeQuery() and get the return and finally do a while to get the results.

String sql = "SELECT * FROM login_sistema WHERE usuario_login = ? AND senha_login = ?";
PreparedStatement stmt = con.prepareStatement(sql);

stmt.setString(1,   jTextField_login);
stmt.setString(2,   jTextField_senha);      

ResultSet rs = stmt.executeQuery(sql);

while (rs.next()) {
        String login = rs.getString("username");
        String senha = rs.getString("senha");
        system.out.println("Login: "+login +" Senha: "+senha);
}       
    
05.10.2015 / 17:23