简体   繁体   中英

Login which depend on user role

I am trying to create program which will display or hide button depended on role of the user, depend if user is Administrator or someone else. So in this case i am passing String from "Login" frame to "Menu" frame and if is String equal to my requirement, it show button, if is not, then hide button, on "Menu" frame. Now this is working with string. But how to do same thing but to pull Role from database? I have that field in database but i don't know how exactly to do that. - My fields in database are username, password and role.

Thanks!

Login frame

JButton btnLogin = new JButton("Login !");
    btnLogin.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            try {

                String username = textUsername.getText();
                String password = passwordField.getText();
                String S = "Administrator";

                String query = "SELECT * FROM ADMINISTRATION where username=? and password=?;";
                PreparedStatement stmt = connection.prepareStatement(query);

                stmt.setString(1, username);
                stmt.setString(2, password);


                ResultSet set=stmt.executeQuery();

                if (set.next()) {

                    Menu menu = new Menu();
                    menu.setVisible(true);
                    setVisible(false);

                    menu.Proba(S);

                    stmt.close();
                    connection.close();

                }
                else {
                    JOptionPane.showMessageDialog(contentPane, "Pogrešno korisničko ime ili lozinka !", "Greška !", JOptionPane.ERROR_MESSAGE);             

                }

            } catch (Exception e2) {
                // TODO: handle exception
            }


        }
    });

Menu frame

public void Proba(String S) {

    if (S.equals("Administrator")) {

        btnOption.setVisible(true);

    }
    else {

        btnOption.setVisible(false);
    }
}

Ok, I'm assuming this is an academic project.

So, you have a ResultSet and you are iterating over it, when you call the "next" method you move to the next (in this case the first) row.

Now you have a row, you need to call a method that retrieves a String value from a column (in this case role), check the documentation:

https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html

You have a "getString" method that needs a column name, and will return the string valuo from that column.

An example could be:

if (set.next()) {
    String role = set.getString("role");

    Menu menu = new Menu();
    menu.setVisible(true);
    setVisible(false);

    menu.Proba(role);

    stmt.close();
    connection.close();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM