📄 login.java
字号:
package file1;
/*功能描述:该模块用来处理用户的登陆,登陆成功后根据管理员的类型显示相应权限的可供操作的模块,这些
* 模块是通过登陆的管理员的类型的ID查询数据库中的管理员类型-模块对应表获得的.
* @Author:黄顺武
* Create Time:2007-11-28
* @Version:1.4
*/
import java.awt.*;
import sun.jdbc.rowset.*;
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.StringTokenizer;
public class Login extends JPanel implements ActionListener, KeyListener {
private int[] adminstratorID = new int[50];// 登陆用户的管理员类型(可能有多种),-1表示非管理员
private int isLoading = -1;// 用户登录的状态,-1表示为进行登录,1表示已进行登录过
private String[] partnames = new String[500];// 存储所有显示模块名称的数组
private String[] partsID = new String[500];// 存储所有显示模块id的数组
private DBConnection con = null;// 声明数据库连接类
// 声明登陆界面的控件
private JLabel titleLB = new JLabel("管理员登陆");
private JLabel usernameLB = new JLabel("用户名:");
private JLabel passwordLB = new JLabel("密 码:");
private JTextField usernameTF = new JTextField(10);
private TextField passwordTF = new TextField(10);
// 声明界面中触发事件的按钮
private JButton loadButton = new JButton("登陆");
private JButton cancelButton = new JButton("取消");
// 声明装载控件的面版
private JPanel titlePanel = new JPanel();
private JPanel nameAndPasswordPanel = new JPanel();
public Login() {
titleLB.setFont(new Font(Font.DIALOG, Font.ITALIC, 15));
usernameLB.setFont(new Font(Font.DIALOG, Font.ITALIC, 12));
passwordLB.setFont(new Font(Font.DIALOG, Font.ITALIC, 12));
usernameTF.setBorder(null);
usernameTF.setBackground(Color.LIGHT_GRAY);
passwordTF.setBackground(Color.LIGHT_GRAY);
loadButton.setBorder(null);
loadButton.setBackground(Color.yellow);
loadButton.setFont(new Font(Font.DIALOG, Font.ITALIC, 14));
loadButton.setContentAreaFilled(false);
cancelButton.setBorder(null);
cancelButton.setBackground(Color.yellow);
cancelButton.setFont(new Font(Font.DIALOG, Font.ITALIC, 14));
cancelButton.setContentAreaFilled(false);
// 声明titlePanel面版的布局及其加载控件
titlePanel.setLayout(new FlowLayout(FlowLayout.CENTER));
titlePanel.add(titleLB);
// 声明nameAndPanel的布局及其加载控件
nameAndPasswordPanel.setLayout(new GridLayout(3, 2, 10, 10));
nameAndPasswordPanel.add(usernameLB);
nameAndPasswordPanel.add(usernameTF);
nameAndPasswordPanel.add(passwordLB);
passwordTF.setEchoChar('*');// 声明输入的密码的显示格式
nameAndPasswordPanel.add(passwordTF);
nameAndPasswordPanel.add(loadButton);
nameAndPasswordPanel.add(cancelButton);
this.setLayout(new BorderLayout(0, 20));
this.add(titlePanel, BorderLayout.NORTH);
this.add(nameAndPasswordPanel, BorderLayout.CENTER);
// 注册按钮事件
loadButton.addActionListener(this);
cancelButton.addActionListener(this);
usernameTF.addKeyListener(this);
passwordTF.addKeyListener(this);
}
public int getAdminstratorID() {
return adminstratorID[0];
}
public String[] getPartnames() {
return partnames;
}
public int getIsLoading() {
return isLoading;
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == loadButton) {
doLoad();
}
if (ae.getSource() == cancelButton) {
isLoading = 1;// 表示已进入过登陆
}
}
public void keyTyped(KeyEvent ke) {
}
public void keyPressed(KeyEvent ke) {
if (ke.getSource() == usernameTF) {// 当在用户名输入框键入回车键时把光标移到密码输入框
if (ke.getKeyCode() == 10) {// 键入回车键
passwordTF.requestFocus();
}
}
if (ke.getSource() == passwordTF) {// 当在密码框键入回车键时进行登录处理
if (ke.getKeyCode() == 10) {// 键入回车键
doLoad();
}
}
}
public void keyReleased(KeyEvent ke) {
}
public void doLoad() {// 进行登录处理时调用的方法
isLoading = 1;// 表示已进行过登陆
String name = usernameTF.getText().trim();
if (name.equals("")) {
JOptionPane.showMessageDialog(null, "用户名不能为空!", "",
JOptionPane.ERROR_MESSAGE);
return;
}
String password = passwordTF.getText().trim();
if (password.equals("")) {
JOptionPane.showMessageDialog(null, "密码不能为空!", "",
JOptionPane.ERROR_MESSAGE);
return;
}
try {
con = new DBConnection();
/*
* 以下代码块的功能描述:进行sql语句的拼接并根据拼接的结果字符串进行数据库查询并处理查询结果,
* 具体的处理内容有:如果查询结果不为空,则把查得的管理员的类型ID赋给adminstratorID,当
* 程序跳出到主窗口时可根据调用该类的getAdminstratorID()方法获得该登陆管理员的类型ID,
* 然后根据该ID显示主界面中的相应权限的模块。
*/
String sqlStr = "select typeID from Admins where username='" + name
+ "' and password='" + password + "'";
CachedRowSet crs = con.getResultSet(sqlStr);
if (crs.next()) {
if (crs.getString(1).indexOf(",") == -1) {
adminstratorID[0] = Integer
.valueOf(crs.getString(1).trim());
} else {// 该用户有多种角色
StringTokenizer st = new StringTokenizer(crs.getString(1)
.trim(), ",");
int countBase = 0;// 计数变量
while (st.hasMoreTokens()) {
adminstratorID[countBase++] = Integer.valueOf(st
.nextToken());
}
}
} else {
JOptionPane.showMessageDialog(null,
"this adminstrator is not exist!", "",
JOptionPane.ERROR_MESSAGE);
return;
}
if (adminstratorID[0] != 0) {// 数组非空
crs = null;
int recordCount = 0;// 记录该用户所拥有的操作权限的记录总数
int temp2 = 0;// 存储所有显示模块的名称和id的数组的下标
int isShow = 0;// 是否要显示库存不足货品记录,只有超级管理员和仓库管理员才需显示
for (int i = 0; i < adminstratorID.length; i++) {
if (adminstratorID[i] == 0) {// 到达数组实际存储的元素的末尾跳出循环
break;
} else {
if (adminstratorID[i] == 1 || adminstratorID[i] == 13) {// 只有超级管理员和仓库管理员才需显示
isShow = 1;
}
sqlStr = "select partsID from Admin_Partname where Admin_Partname.typeID="
+ adminstratorID[i];
crs = con.getResultSet(sqlStr);
if (crs.next()) {// 该管理员类型有可操作模块
recordCount++;// 权限-模块对应记录累加
String idStr = crs.getString(1).trim();// 模块id字符串
if (idStr.indexOf(",") == -1) {// 该类型管理员有单个可操作模块
int haveIn = 0;// 表示该id的模块还不存在于数组中
for (int j = 0; j < temp2; j++) {// temp2为显示模块id数组中实际元素的个数
if (partsID[j].equals(idStr)) {
haveIn = 1;// 表示该id的模块已经过存在于数组中
}
}
if (haveIn == 0) {// 不存在
partsID[temp2++] = idStr;
}
} else {// 该类型管理员有多个可操作模块
StringTokenizer st = new StringTokenizer(idStr,
",");// 解析该用户对应的显示模块的id字符串
while (st.hasMoreTokens()) {// 开始解析id字符串中所有的id到数组中
int haveIn = 0;// 表示该id的模块还不存在于数组中
String id = st.nextToken().trim();
for (int j = 0; j < temp2; j++) {// temp2为显示模块id数组中实际元素的个数
if (partsID[j].equals(id)) {
haveIn = 1;// 表示该id的模块已经过存在于数组中
}
}
if (haveIn == 0) {// 不存在
partsID[temp2++] = id;
}
}
}
}
}
}
for (int temp = 0; temp < temp2; temp++) {// 根据解析得到的所有id查询对应的模块的名称
String singleQueryStr = "select pName_CN from Parts where ID="
+ partsID[temp];
CachedRowSet crsTemp = con.getResultSet(singleQueryStr);
if (crsTemp.next()) {// 把得到的模块的名称储存到数组当中,以便根据模块名称进行显示
partnames[temp] = crsTemp.getString(1).trim();
}
}
if (isShow == 1) {// 需要显示库存不足模块
GoodNotEnoughWarningJPanel GoodNotEnoughWarningJPanel = new GoodNotEnoughWarningJPanel();
if (GoodNotEnoughWarningJPanel.getTotalCount() > 0) {
JFrame frameToShow = new JFrame();
JDialog dialog = new JDialog(frameToShow, true);
dialog.setSize(460, 300);
dialog.setTitle("库存不足的货品列表");
dialog.setLocation(100, 50);
dialog.getContentPane().setLayout(new BorderLayout());
dialog.getContentPane().add(GoodNotEnoughWarningJPanel,
BorderLayout.CENTER);
dialog.setVisible(true);
}
}
if (recordCount == 0) {// 该用户没有任何操作权限
JOptionPane.showMessageDialog(null, "您没有任何操作权限", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
}
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -