Hello Daniwebers,

I have my login form inwhich I read the value in mysql table to identify whether the password match with the role. I have on combobox with different roles(director, assistant...) which I have filled in manually. It does the work but I would like to do something else when the user login as the Director(display a different form). I know that I will have to check if cb1 = "director" but have no idea how to do that. Any help will be appreciated.

    public void loginACCT(String pass, String role) {
         ResultSet rs = null;
         Properties conProps = new Properties();
        conProps.setProperty("user", "root");
        conProps.setProperty("password", "root");


     try {
             con = DriverManager.getConnection ("jdbc:mysql://localhost:3306/stock", conProps);
              con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE );
        } catch (SQLException ex) {

        }    
     String sql = ("select * from users where Password = ? and Role = ? ");

        try {

             con.setAutoCommit(false);
            st = con.prepareStatement(sql);
            st.setString(1,pass );
             st.setString(2,role );
            st.executeQuery();
            rs = st.getResultSet();

            if(rs.next()){

                JOptionPane.showMessageDialog(null, "Welcome, You are now logged in the System!");

                Main ob = new Main(); // display main form
                    ob.setVisible(true);
                this.dispose();


            } else {
                JOptionPane.showMessageDialog(null, "You have entered an incorrect password!");
                p1.setText("");


            }

             st.close();
            con.commit();            
}
catch (Exception e) {

    System.out.println(e);
}
       //notifyListener();
}

hard to say if we don't know what type cb1 is, but if it's a String it's not:

cb1 = "director"

but

"director".equals(cb1)

  1. if you want to compare two Objects on value, use the equals method, not the == operator, which will simply compare the references.

  2. in this case, it's recommended to choose:
    "director".equals(cb1)
    over
    cb1.equals("director")
    for the simple reason that it is impossible for "director" to throw a NullPointerException.

cb1 is a combobox. I have changed my code but does not do the job

public void loginACCT(String pass, String role) {
     ResultSet rs = null;
     Properties conProps = new Properties();
    conProps.setProperty("user", "root");
    conProps.setProperty("password", "root");



 try {
         con = DriverManager.getConnection ("jdbc:mysql://localhost:3306/stock", conProps);
          con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE );
    } catch (SQLException ex) {

    }    
 String sql = ("select * from users where Password = ? and Role = ? ");

    try {

         con.setAutoCommit(false);
        st = con.prepareStatement(sql);
        st.setString(1,pass );
         st.setString(2,role );
        st.executeQuery();
        rs = st.getResultSet();
        if(rs.next()){

            if (cb1.equals("Director")) {

                JOptionPane.showMessageDialog(null, "Welcome Director, You are now logged in the System!");

            MainManage ob = new MainManage();
                ob.setVisible(true);
            this.dispose(); 
            } else{

            JOptionPane.showMessageDialog(null, "Welcome, You are now logged in the System!");

            Main ob = new Main();
                ob.setVisible(true);
            this.dispose();
            }

        } else {
            JOptionPane.showMessageDialog(null, "You have entered an incorrect password!");
            p1.setText("");


        }

         st.close();
        con.commit();            

How can a combo box be equal to a String? - they're completely different things.
You didn't post the relevant code, but at a guess you probably want to compare the text currently in the combo box. I'm sure you can guess the method that gives you the text!

Be a part of the DaniWeb community

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