Doubt about Select

Asked

Viewed 46 times

3

Hello, I am doing a user/password verification in java and I need to perform a query in which check in the database there is a login and a password related to the typed in jTextFields. But I’m not getting if anyone can help me I’m grateful, to help get a sense of what I need here’s 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) ";

However, I don’t know how to implement in the demarcated field the variable containing the value of textField tried several ways but only resulted error.

  • See if you haven’t forgotten the "'". Your code should look like this: "SELECT * FROM login_sistema WHERE usuario_login ='(here in case would be the variable of jTextField)' AND login ='(here in case would be the variable of jTextField)'";

  • But for example, I added the jTextFields to a variable in this way [ String password_conf = jTextField2.gettext(); ] as I could include the variable password_conf in sql code ?

  • Are you using jdbc? if you have the code that processes sql put as well

  • Yes, I am. 
 PreparedStatement ps;
 ps = connection.prepareStatement(sql);
 ps = (PreparedStatement) ps.executeQuery(sql); 
 ps.setString(1, login_login); 
 ps.setString(2, senha_senha);

  • this is what I am putting after sql request

  • I think we just missed the questions and post your code in question :P, part of it seems correct.

Show 1 more comment

1 answer

3


Use Prepared statements to define the values of your query, then send the query to the bank with executeQuery() and take the return and finally do a while to catch 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);
}       

Browser other questions tagged

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