Đăng nhập đúng vẫn báo sai tài khoản

romworld

New Member
7/2/15
1
0
0
34
mình có đoạn code này khi đăng nhập nó check hết tài khoản rồi đăng nhập nhưng đăng nhập rồi lại báo sai tài khoản pro nào giúp mình với ạ
PHP:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package GUI;
import java.awt.Image;
import javax.swing.ImageIcon;
import ConnectDatabase.DBConnect;
import static ConnectDatabase.DBConnect.cnn;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
*
* @author Administrator
*/
public class frmLogin extends javax.swing.JFrame {

    public static String nameLogin = "";
    PreparedStatement ps = null;
    ResultSet rs = null;

    public frmLogin() {
        initComponents();
        ImageIcon icon = new ImageIcon(getClass().getResource("/Icon/G1.png"));
        Image image = icon.getImage();
        setIconImage(image);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                        
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jtfUsername = new javax.swing.JTextField();
        jbCancel = new javax.swing.JButton();
        jbLogin = new javax.swing.JButton();
        jpPassword = new javax.swing.JPasswordField();
        jLabel1 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Login");
        setBackground(new java.awt.Color(255, 255, 255));
        setResizable(false);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(0, 0, 255));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/manager-login.png"))); // NOI18N

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 15)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(102, 102, 102));
        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/acc.png"))); // NOI18N
        jLabel3.setText("Username");
        jLabel3.setToolTipText("");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 15)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(102, 102, 102));
        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/key.png"))); // NOI18N
        jLabel4.setText("Password");
        jLabel4.setToolTipText("");

        jtfUsername.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
        jtfUsername.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jtfUsername.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jtfUsernameKeyReleased(evt);
            }
        });

        jbCancel.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jbCancel.setForeground(new java.awt.Color(0, 0, 204));
        jbCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/t.png"))); // NOI18N
        jbCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelActionPerformed(evt);
            }
        });
        jbCancel.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jbCancelKeyReleased(evt);
            }
        });

        jbLogin.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jbLogin.setForeground(new java.awt.Color(0, 0, 204));
        jbLogin.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/dn.png"))); // NOI18N
        jbLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbLoginActionPerformed(evt);
            }
        });
        jbLogin.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jbLoginKeyReleased(evt);
            }
        });

        jpPassword.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
        jpPassword.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jpPassword.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jpPasswordKeyReleased(evt);
            }
        });

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/acount.png"))); // NOI18N

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/login here.png"))); // NOI18N

        jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/footer.png"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addComponent(jLabel6)))
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(227, 227, 227)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(132, 132, 132)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(jtfUsername)
                                        .addComponent(jbLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 151, Short.MAX_VALUE))
                                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jbCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jpPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addGap(5, 5, 5)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jpPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jbLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addGap(18, 18, Short.MAX_VALUE)
                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        setSize(new java.awt.Dimension(578, 463));
        setLocationRelativeTo(null);
    }// </editor-fold>                      

    public void resetForm() {
        jtfUsername.setText("");
        jpPassword.setText("");
        jtfUsername.requestFocus();
    }

    public  void send() {
        String strnull = "";
        String user = jtfUsername.getText();
        String pass =jpPassword.getText();
        if (user.equals(strnull) || pass.equals(strnull)) {
            System.out.println("Đăng nhập không thành công!");
            JOptionPane.showMessageDialog(null, "UserName And PassWord Isn't Null !");
            this.resetForm();
            //return;
        } else if (DBConnect.open()) {
            try {
                boolean blean = true;
                ps = DBConnect.cnn.prepareStatement("select fldTenDangNhap,fldMatKhau from tblDangNhap");
                rs = ps.executeQuery();
                while (rs.next()) {
                    String username = "" + rs.getString("fldTenDangNhap");
                    String password = "" + rs.getString("fldMatKhau");            
                    if (user.equals(username) && pass.equals(password)){
                        nameLogin = username ;
                       new Main().setVisible(true);
                       System.out.println("Đăng nhập thành công!");
                        dispose();
                     
                    }
                    else
                    {
                        //blean = false;
                        //this.setVisible(false);
                      
                        System.out.println("Kiểm tra lại tài khoản");
                      
                        JOptionPane.showMessageDialog(this, "User name or password is invalid \nUser not active .Please try agian", "Login Fails", JOptionPane.ERROR_MESSAGE);
                        this.resetForm();
                    }

              
               }

            } catch (SQLException ex) {
                Logger.getLogger(frmLogin.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
      
    }
    private void jbLoginActionPerformed(java.awt.event.ActionEvent evt) {                                      
        send();
    }                                     

    private void jtfUsernameKeyReleased(java.awt.event.KeyEvent evt) {                                      
        // TODO add your handling code here:
        if (evt.getKeyCode() == evt.VK_ENTER) {
            jpPassword.requestFocus();

        }
    }                                     

    private void jpPasswordKeyReleased(java.awt.event.KeyEvent evt) {                                     
        if (evt.getKeyCode() == evt.VK_ENTER) {
            send();
        }
    }                                    

    private void jbCancelActionPerformed(java.awt.event.ActionEvent evt) {                                       
        System.exit(0);
    }                                      

    private void jbLoginKeyReleased(java.awt.event.KeyEvent evt) {                                  
        if (evt.getKeyCode()==evt.VK_ENTER) {
            send();
        }
    }                                 

    private void jbCancelKeyReleased(java.awt.event.KeyEvent evt) {                                   
        if (evt.getKeyCode()==evt.VK_ENTER) {
            System.exit(0);
        }
    }                                  

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frmLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frmLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frmLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frmLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new frmLogin().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                   
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JButton jbCancel;
    private javax.swing.JButton jbLogin;
    private javax.swing.JPasswordField jpPassword;
    private javax.swing.JTextField jtfUsername;
    // End of variables declaration                 
}
 
Sửa lần cuối:

hitshootnum1

New Member
5/7/13
8
1
3
29
Câu select của bạn là lấy ra hết tất cả các bản ghi, và bạn sử dụng vòng lặp while để kiểm tra lần lượt các bản ghi có trùng với username, password hay không.
Làm cách này thì đương nhiên câu if của bạn trả về true có 1 lần và false rất nhiều lần. Nên nó hiện thông báo sai là đúng. Logic code có nhiều vấn đề.
 

Joe

Thành viên VIP
21/1/13
3,065
1,342
113
Look at this piece of codes:
PHP:
while (rs.next()) {
        String username = "" + rs.getString("fldTenDangNhap");
        String password = "" + rs.getString("fldMatKhau");        
        if (user.equals(username) && pass.equals(password)){
                nameLogin = username ;
                new Main().setVisible(true);
                System.out.println("Đăng nhập thành công!");
                dispose();
        }
        else
        {
                ...
        }

         
}
What do you see? A man-made memory-leak. Why?
1) How does the method dispose() work? It disposes the displaying GUI.
2) What then? It returns to its caller
3) Who is its caller? The while-loop

Also?
- It loops again and this time WITHOUT GUI and till the while-loop finishes.
- Consequence? a) Wrong result, b) a dangerous Memory-Leak (or OutOfMemory)

How to correct the problem?
Read the JAVA statements carefully and you'll know how to end the loop if the login is successful.

Coding and Programming are easy. Understanding the logical flow and the computing process requires more than just using a fancy IDE of Netbeans or Eclipse.
 
Sửa lần cuối:

Vjrus.HT

New Member
18/7/13
27
3
3
28
1. Bạn đã import CSDL vào SQL chưa? Đã kết nối CSDL thành công chưa?
2. Theo như mình biết thì
username: tuyentv
password: 2531989
 
Sửa lần cuối:

Joe

Thành viên VIP
21/1/13
3,065
1,342
113
Vjrus.HT,
Why you took my codes and posted them as if you wanted to address me? If you don't know then keep your mouth shut. Don't guess. It's Computer Science (CS) and NOT a Vietnamese Voodoo. My allusion is clear enough that even a layman can easily understand. It seems to me that you're unable to understand anything about CS. It's a shame that you dare to write such a nonsense like this:
1. Bạn đã import CSDL vào SQL chưa? Đã kết nối CSDL thành công chưa?
2. Theo như mình biết thì
username: tuyentv
password: 2531989
 

Vjrus.HT

New Member
18/7/13
27
3
3
28
Joe,
I'm sorry, I don't deliberately and i have edited. I just wanted to help him because I've used it.
Sorry again!