alexooi17 0 Newbie Poster

I had decided to use JcomboBox but it doesn't seems to work. It doesn't seem to be able to take the string in the JComboBox as a condition to log in. Previously with correct username and password, it will be able to log in but now with correct user and pass together with Role from the JComboBox, it is saying that the user and pass is wrong.

    private void loginActionPerformed (ActionEvent loginBtn){

        if(loginBtn.getActionCommand().equals("Login"))
        {
                JFrame loginDtl = new JFrame("Login");
                loginDtl.setSize(320, 320);
                loginDtl.setLocationRelativeTo(null);

                //The ComboBox for Role Selection

                String Role[] = {"Finance Admin","Employee"};

                JPanel panel = new JPanel();
                combo = new JComboBox(Role);
                combo.setBackground(Color.gray);
                combo.setForeground(Color.white);

            panel.add(combo);

            loginDtl.add(panel);
            combo.addItemListener(new ItemListener(){
            public void itemStateChanged(ItemEvent ie){


            }
        });
        // END OF COMBOBOX

               //Start of the Login Panel details
                Panel ps = new Panel();
                Panel ps1 = new Panel();

                JUserID = new Label ("User ID");
                lUserID = new TextField(20);

                JUserPass = new Label ("Password");
                lUserPass = new JPasswordField(20); 


                ps.setLayout(new GridLayout(5,1));
                ps.add(JUserID);
                ps.add(lUserID);
                ps.add(JUserPass);
                ps.add(lUserPass);
                ps1.add(ps);
                JButton Login = new JButton("Login");
                ps.add(Login);

                Login.addActionListener(new ActionListener(){
                   public void actionPerformed(ActionEvent login2Btn){
                        login2ActionPerformed(login2Btn);
                   }
                    });

                    loginDtl.setVisible(true); 
                loginDtl.add(ps1,BorderLayout.NORTH);

            }
    }
            private void login2ActionPerformed (ActionEvent login2Btn){
        String value1 = lUserID.getText();
        String value2 = lUserPass.getText();
        if(login2Btn.getActionCommand().equals("Login"))
        {
        String user1="";
        String pass1="";


            //establish connection
            try{
                Class.forName("com.mysql.jdbc.Driver");


                Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/budget","root","");
                Statement pst = con.createStatement();
                String sqlCom = ("SELECT * FROM emp_lgn WHERE User_ID = '"+value1+"' && Password = '"+value2+"'&& Role = '"+combo+"'");
                ResultSet rs = pst.executeQuery(sqlCom);

                    while (rs.next()){
                    user1 = rs.getString("lUserID");
                    pass1 = rs.getString("lUserPass");
                }
                //if the password and user is correct
                if(value1.equals(user1)&& value2.equals(pass1)&&combo.getSelectedItem().equals("Finance Admin")){
                FinanceAdScr page=new FinanceAdScr(user1);
                page.setVisible(true);

                }
                else{
                    //error if wrong username and psd
                    JOptionPane.showMessageDialog(null,"Incorrect login or password","Error",JOptionPane.ERROR_MESSAGE);
                    lUserID.setText("");
                     lUserPass.setText("");


                }
            }
            catch(Exception ex){
                ex.printStackTrace();
            }


       }
                   }

       //for exit button
       private void exitActionPerformed (ActionEvent exitBtn){

        if (exitBtn.getActionCommand().equals("Exit Program"))
        {
            System.exit(0);
        }

       }




        public static void main(String[]args ){
        MainPage mp = new MainPage();
    }





}   
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.