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;
}
}
stprefix is unnecessary and make things less readable), and store things in a unique place.