1. 程式人生 > >學生資訊管理系統之第三篇登入介面java程式碼

學生資訊管理系統之第三篇登入介面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);
  }
 }
}