在我的 Java 应用程序中,我尝试创建用户组并根据用户所属的用户组为用户分配权限。我就是这样编程的。当用户登录到系统时,获取用户名并存储在静态变量中。当用户打开任何表单时,从静态变量中获取用户名并检查其组和权限。根据权限禁用和启用表单的某些组件。在这里,我从静态变量 stgroupName 中检索值时遇到问题。ItmMgt 类的方法 checkuserrights() 未获取当前用户的名称。有人可以帮助我了解这里出了什么问题。我想,应该有很多更好的方法来做到这一点。所以欢迎任何建议。
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;
}
}