0

In my Java Application Im trying to create user groups and assign permission to users depending on the user group they belongs to. This is how I programmed. When User Login to the system, grab the user name and store in a static variable. When user opens any Form, get the user name from static variable and check its group and permissions. Depending on the permissions disable and enable some components of the form. Here Im having problem retrieving the value from static variable stgroupName. Method checkuserrights() at ItmMgt class not getting the current user's name. Can someone please assist me to understand whats the wrong here. I guess, there should be many better ways to do this. So any suggestions welcome.

  public class Login extends javax.swing.JInternalFrame {

        public static String USERNAME;
        USERNAME  = txtUserName.getText(); // get the user name to static variable
  }

  public class ItemMgt extends javax.swing.JInternalFrame {

        public ItemMgt() {

              initComponents();
              checkuserrights();
              genarateID();
        }

  private void checkuserrights() {
        try {

              Usergroup usergroup = UserGroupController.getUserRights(Login.USERNAME);// check the user's rights passing user name from static variable.
              if (usergroup != null) {
                    btnDelete.setEnabled(usergroup.isDeleteItem());
                    btnAdd.setEnabled(usergroup.isAdditem());
                    btnUpdate.setEnabled(usergroup.isUpdateitem());

              }
        } catch (ClassNotFoundException ex) {
              Logger.getLogger(ItemMgt.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
              Logger.getLogger(ItemMgt.class.getName()).log(Level.SEVERE, null, ex);
        }

  }

  public class UserGroupController {

        public static Usergroup getUserRights(String username) throws ClassNotFoundException, SQLException {
              Connection conn = DBConnection.conn();
              String sql = "select * from UserGroup where uGroupName = ?";

              Object[] values = {username};
              ResultSet res = DBHandller.getData(sql, conn, values);
              while (res.next()) {
                    String grpName = res.getString("uGroupName");
                    boolean additem = res.getBoolean("addItem");
                    boolean delitem = res.getBoolean("delteItem");
                    boolean upitem = res.getBoolean("editItem");
                    Usergroup ugroup = new Usergroup(grpName, additem, delitem, upitem);
                    return ugroup;
              }
              return null;
        }
  }
4
  • 1
    Your code is unclear, and doesn't compile. It sems you're storing the user name in a static variable named stgroupName of the Login class, and then try to get back the group name from a static variable also named stgroupName, but located in the UserGroup class. Fix your naming conventions (this st prefix is unnecessary and make things less readable), and store things in a unique place. Commented Apr 30, 2013 at 13:07
  • is it a maltyvuser application? Commented Apr 30, 2013 at 13:10
  • @JB Nizet Sorry. It should be Usergroup usergroup = UserGroupController.getUserRights(Login.stgroupName); Updated the code Commented Apr 30, 2013 at 13:12
  • @ MaVRoSCy yes. This is Multi User Application Commented Apr 30, 2013 at 13:16

1 Answer 1

3

A static variable is by definition a gloabl object. When you change the value in one instance, then the value of all other instances of that object will have the same value. That's the point of using a static.

It sounds to me that your object design is wrong and you should use an object having information about a user which would include the groupname. This object you can pass around in your code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.