Java图形页面代码宿舍管理员管理:能够添加管理员用户、删除管理员用户信息、查找管理员信息、修改管理员信息连接mysql数据库。
时间: 2023-08-04 20:04:00 浏览: 97
宿舍信息管理系统.zip
5星 · 资源好评率100%
好的,下面是Java图形页面代码宿舍管理员管理的实现示例:
```java
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class DormitoryManager extends JFrame implements ActionListener {
private JLabel titleLabel, nameLabel, passwordLabel, phoneLabel, emailLabel;
private JTextField nameField, phoneField, emailField;
private JPasswordField passwordField;
private JButton addButton, deleteButton, searchButton, modifyButton;
private Connection conn;
private Statement stmt;
private ResultSet rs;
public DormitoryManager() {
setTitle("宿舍管理员管理");
setSize(500, 400);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
titleLabel = new JLabel("宿舍管理员管理系统");
titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
titleLabel.setForeground(Color.BLUE);
titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
add(titleLabel, BorderLayout.NORTH);
JPanel centerPanel = new JPanel(new GridLayout(5, 2, 10, 10));
nameLabel = new JLabel("用户名:");
nameLabel.setHorizontalAlignment(SwingConstants.RIGHT);
centerPanel.add(nameLabel);
nameField = new JTextField();
centerPanel.add(nameField);
passwordLabel = new JLabel("密码:");
passwordLabel.setHorizontalAlignment(SwingConstants.RIGHT);
centerPanel.add(passwordLabel);
passwordField = new JPasswordField();
centerPanel.add(passwordField);
phoneLabel = new JLabel("电话号码:");
phoneLabel.setHorizontalAlignment(SwingConstants.RIGHT);
centerPanel.add(phoneLabel);
phoneField = new JTextField();
centerPanel.add(phoneField);
emailLabel = new JLabel("电子邮件:");
emailLabel.setHorizontalAlignment(SwingConstants.RIGHT);
centerPanel.add(emailLabel);
emailField = new JTextField();
centerPanel.add(emailField);
add(centerPanel, BorderLayout.CENTER);
JPanel southPanel = new JPanel(new GridLayout(1, 4, 10, 10));
addButton = new JButton("添加");
addButton.addActionListener(this);
southPanel.add(addButton);
deleteButton = new JButton("删除");
deleteButton.addActionListener(this);
southPanel.add(deleteButton);
searchButton = new JButton("查找");
searchButton.addActionListener(this);
southPanel.add(searchButton);
modifyButton = new JButton("修改");
modifyButton.addActionListener(this);
southPanel.add(modifyButton);
add(southPanel, BorderLayout.SOUTH);
setVisible(true);
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/dormitory", "root", "password");
stmt = conn.createStatement();
} catch(Exception e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == addButton) {
String name = nameField.getText().trim();
String password = new String(passwordField.getPassword());
String phone = phoneField.getText().trim();
String email = emailField.getText().trim();
try {
stmt.executeUpdate("INSERT INTO admin(name, password, phone, email) VALUES('" + name + "', '" + password + "', '" + phone + "', '" + email + "')");
JOptionPane.showMessageDialog(this, "添加成功!");
} catch(Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "添加失败!");
}
} else if(e.getSource() == deleteButton) {
String name = nameField.getText().trim();
try {
stmt.executeUpdate("DELETE FROM admin WHERE name = '" + name + "'");
JOptionPane.showMessageDialog(this, "删除成功!");
} catch(Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "删除失败!");
}
} else if(e.getSource() == searchButton) {
String name = nameField.getText().trim();
try {
rs = stmt.executeQuery("SELECT * FROM admin WHERE name = '" + name + "'");
if(rs.next()) {
passwordField.setText(rs.getString("password"));
phoneField.setText(rs.getString("phone"));
emailField.setText(rs.getString("email"));
} else {
JOptionPane.showMessageDialog(this, "查找失败!");
}
} catch(Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "查找失败!");
}
} else if(e.getSource() == modifyButton) {
String name = nameField.getText().trim();
String password = new String(passwordField.getPassword());
String phone = phoneField.getText().trim();
String email = emailField.getText().trim();
try {
stmt.executeUpdate("UPDATE admin SET password = '" + password + "', phone = '" + phone + "', email = '" + email + "' WHERE name = '" + name + "'");
JOptionPane.showMessageDialog(this, "修改成功!");
} catch(Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "修改失败!");
}
}
}
public static void main(String[] args) {
new DormitoryManager();
}
}
```
上述代码实现了添加、删除、查找、修改宿舍管理员信息的功能,并且连接了 MySQL 数据库。注意,因为涉及到数据库连接和操作,需要将 MySQL 驱动程序的 jar 包放到类路径下。
阅读全文