System using Java and Mysql Database does not recognize user and login even while in the database

Asked

Viewed 33 times

0

I am making a Java Services Order System in Netbeans based in this course. I created the database in Mysql Workworkbench, created the user table, connected to the server in XAMP and made the connection code:

public class TelaLogin extends javax.swing.JFrame {

    Connection conexao = null; //conection da biblioteca sql e conexao variavel do modulo conexao
    PreparedStatement pst = null; // framework(conjunto de lib) para manipular instruções sql

    ResultSet rs = null; //exibe o resultado das intruções sql executadas

    public void logar() {
        String sql = "select * from tbusuarios where login=? and senha=?";
        try {
            // preparar a consulta ao banco em funcoa do que foi digitado
            // nas caixas de texto. ? é substituído pelo conteúdo das 
            // variáveis
            pst = conexao.prepareStatement(sql); //prepara a string 
            pst.setString(1, txtUsuario.getName()); //subritui o primeiro ?
            pst.setString(2, txtSenha.getText()); //subtitui o segundo ?
            rs = pst.executeQuery(); //executa a query

            if (rs.next()) {
                // se existir usuário e senha correspondente
                //vai abrir a tela principal
                TelaPrincipal principal = new TelaPrincipal();
                principal.setVisible(true);
            } else {
                JOptionPane.showMessageDialog(null, "usuário e/ou senha inválido(s)s");
            }
        } catch (HeadlessException | SQLException e) {
            JOptionPane.showMessageDialog(null, e);
        }

    }

But when trying to log in always says that the user and password are invalid even if I put the same that is in the database

I believe the problem is related to the course being based on older versions of the programs, as this Workbench notification warns:

Notificação ao abrir o MySQL WorkBench

  • You are able to connect to the database "Connection conexao = null;"? This line "pst.setString(1, txtUsuario.getName());" shouldn’t be "pst.setString(1, txtUsuario.getText());"?

No answers

Browser other questions tagged

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