学生信息管理系统之第三篇登录界面java代码

class DLFrame extends JFrame implements ActionListener, ItemListener {// 登录界面
 JPanel p1 = null;
 JPanel p2 = null;
 JPanel p3 = null;
 JLabel userName = new JLabel("帐号:");
 JTextField txtUser = new JTextField();
 JLabel password = new JLabel("密码:");
 JPasswordField txtPwd = new JPasswordField(6);
 JLabel role = new JLabel("身份:");
 JComboBox cbrole = new JComboBox();
 JButton btnLogin = new JButton("登录");
 JButton btncz = new JButton("重置");
 JButton btnCancel = new JButton("取消");
 JLabel imageLabel;
 Icon image;
 static int OK = 1;
 static int CANCEL = 0;
 int actionCode = 0;
 Connection con = null;
 Statement stmt = null;
 ResultSet rs = null;
 int qxian = 0;

 public DLFrame() {// 构造方法
  super("登录界面");
  p1 = new JPanel();
  p2 = new JPanel();
  p3 = new JPanel();
  cbrole.addItem("管理员");
  cbrole.addItem("学生");
  cbrole.addItem("教师");
  image = new ImageIcon(" Picture\\23.jpg");
  imageLabel = new JLabel(image);
  p1.add(imageLabel);
  this.setLayout(new FlowLayout());
  this.setBounds(100, 100, 246, 345);
  p2.setLayout(new GridLayout(4, 2));
  p2.add(userName);
  p2.add(txtUser);
  p2.add(password);
  p2.add(txtPwd);
  p2.add(role);
  p2.add(cbrole);
  p3.add(btnLogin);
  p3.add(btncz);
  p3.add(btnCancel);
  this.add(p1);
  this.add(p2);
  this.add(p3);
  this.setResizable(true);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.show();
  btnLogin.addActionListener(this);
  cbrole.addItemListener(this);
  btncz.addActionListener(this);
  btnCancel.addActionListener(this);
 }

 public void connDB() { // 连接数据库
  try {
   Class.forName("com.mysql.jdbc.Driver");
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  }
  try {
   con = DriverManager.getConnection(
     "jdbc:mysql://localhost:3306/student",
     "root", "1234");
   stmt = con.createStatement();
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }

 public void closeDB() // 关闭连接
 {
  try {
   stmt.close();
   con.close();
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }

 public void itemStateChanged(ItemEvent e) {
  if (e.getStateChange() == ItemEvent.SELECTED) {
   JComboBox jcb = (JComboBox) e.getSource();
   qxian = jcb.getSelectedIndex();
  }
 }

 public void actionPerformed(ActionEvent e) {
  Object source = e.getSource();
  String un = null;
  String pw = null;
  boolean success = false;// 用于判断是否登录成功
  if (source == btnLogin) {
   if (txtUser.getText().equals("") || txtPwd.getText().equals("")) {// 判断是否输入了用户名和密码
    JOptionPane.showMessageDialog(null, "登录名和密码不能为空!");
   } else {
    this.connDB();
    try {
     rs = stmt.executeQuery("select * from unpw where qx="
       + qxian);
     while (rs.next()) {
      un = rs.getString("un").trim();
      pw = rs.getString("pw").trim();
      if (txtUser.getText().equals(un)) {
       if (txtPwd.getText().equals(pw)) {
        actionCode = OK;
        this.setVisible(false);
        if (qxian == 0) {
         new ManagerFrane();// 进入管理员界面
        }
        if (qxian == 1) {
         new StudentFrame();// 进入学生界面
        }
        success = true;
        break;
       } else {
        JOptionPane.showMessageDialog(null, "密码错误!");
        txtPwd.setText("");
        success = true;
       }
      }
     }
     if (!success) {
      JOptionPane.showMessageDialog(null, "登录名错误!");
      txtUser.setText("");
      txtPwd.setText("");
     }

    } catch (SQLException e1) {
     e1.printStackTrace();
    }
   }
  } else if (source == btncz) {
   txtUser.setText("");
   txtPwd.setText("");
  } else if (source == btnCancel) {
   System.exit(0);
  }
 }
}

 

评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值