HỎI em tạo form đăng nhập , lấy dữ liệu từ text field rồi so sánh với dữ liệu trong database , nhưng không login đc , mọi người giúp em được không ạ ?

Quangvu0567

New Member
30/6/20
1
0
1
Hà nội
Java:
package view;

import controler.BandocDAO;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import model.book;
import model.dangnhap;

/**
*
* @author ADMIN
*/
public class login extends javax.swing.JFrame implements ActionListener{
    private login amin;
    ArrayList<dangnhap>list;
    /**
     * Creates new form login
     */
    public login() {
        initComponents();
        list= new BandocDAO().selectQuery3();
        btlogin.addActionListener(this);
        dangky.addActionListener(this);
        amin=null;
    }

    /**
     * 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() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jtxtname = new javax.swing.JTextField();
        jtxtpass = new javax.swing.JPasswordField();
        btlogin = new javax.swing.JButton();
        dangky = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setText("Đăng nhập");

        jLabel2.setText("User :");

        jLabel3.setText("Password :");

        btlogin.setText("login");

        dangky.setText("Đăng ký");

        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)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(175, 175, 175)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(58, 58, 58)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(36, 36, 36)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btlogin)
                                .addGap(52, 52, 52)
                                .addComponent(dangky))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jtxtname, javax.swing.GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE)
                                .addComponent(jtxtpass)))))
                .addContainerGap(17, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jtxtname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jtxtpass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(45, 45, 45)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btlogin)
                    .addComponent(dangky))
                .addContainerGap(105, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                      

    /**
     * @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 [URL]http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html[/URL]
         */
        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(login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(login.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 login().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                   
    private javax.swing.JButton btlogin;
    private javax.swing.JButton dangky;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jtxtname;
    private javax.swing.JPasswordField jtxtpass;
    // End of variables declaration                 

    @Override
  [B]  public void actionPerformed(ActionEvent e) {
        if(e.getSource().equals(dangky)){
            dangkyclick();
        }else{
            btloginclick();
        }
    }
    public void btloginclick(){
        try{
         for(dangnhap ammin:list){
            if(ammin.getUser1().equals(jtxtname.getText())&& ammin.getPassword().equals(jtxtpass.getText())){
                 JOptionPane.showMessageDialog(rootPane,"đăng nhập thành công");
                 break;
             }else{
                 JOptionPane.showMessageDialog(rootPane,"đăng nhập thất bại");
         }
         }
        }catch(Exception e){
            e.printStackTrace();
        }
    }[/B]
    public void dangkyclick(){
        new dangky().setVisible(true);
        this.dispose();
    }
}
///////////////////////////////////////////////////////////////////////////////////////đây là hàm lấy dữ liệu từ database ra để kiểm tra//////////////////////////////////////////////////////////////
public ArrayList<dangnhap>selectQuery3(){
        ArrayList<dangnhap>list=new ArrayList<>();
        String sql="SELECT * FROM [tvlogin] " ;
        try {
            PreparedStatement ps=conn.prepareStatement(sql);
            ResultSet rs=ps.executeQuery();
            while(rs.next()){
                dangnhap b=new dangnhap();
                b.setUser(rs.getString(1));
                b.setPassword(rs.getString(2));
                b.setRoles(rs.getString(3));
                b.setId(rs.getInt(4));
                b.setName(rs.getString(5));
                b.setAddress(rs.getString(6));
                b.setPhoneNumber(rs.getString(7));
                list.add(b);
            }
        } catch (Exception e) {
           e.printStackTrace();
        }
        return list;
}
 
Last edited by a moderator:

Joe

Thành viên VIP
21/1/13
2,935
1,304
113
You have to describe what problem you got. For example: exceptions? failed loging? etc,
nhưng không login đc
tells me NOTHING. However I can guess:
1)
selectQuery3() delivers something that doesn't match to the login-comparing values, or
2) the dangnhap object delivers wrong values
3) your DB delivers nothing so that the loop in selectQuery3() returns an empty ArrayList
Your question is cryptic and incomplete so that everyone who wants to help must guess....