用Java写一个代码 教育管理系统 该软件用于管理班级和学生信息 功能要求: 1. 登录注册 2.学科信息管理(增加、删除、修改、查询) 3. 专业信息管理(增加需要选择拥有学科多选、删除、修改、查询 4. 班级信息管理(增加需要选择所属专业、删除、修改、查询 5.学生信息管理(增加需要选择所属班级、删除、修改、查询) 6.数据信息分页展示(加分项) 7. 3次密码错误冻结1分钟(加分项
时间: 2023-07-10 16:38:16 浏览: 89
基于Java的学生管理系统,旨在提供学生信息的管理和查询功能
好的,下面是一个简单的教育管理系统的Java代码实现,包括登录注册、学科、专业、班级和学生信息管理,数据信息分页展示和密码错误冻结功能。
首先,我们需要创建一个名为EducationManagementSystem的Java类,这个类包含main方法和其他方法,其中main方法用于启动程序。我们可以使用Java的Swing库来实现用户界面,使用JDBC连接数据库来存储、查询和更新数据。
```java
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class EducationManagementSystem {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/education";
static final String USER = "root";
static final String PASS = "password";
static Connection conn = null;
static Statement stmt = null;
static int loginAttempts = 0;
public static void main(String[] args) {
// 创建登录面板
createLoginPanel();
}
// 创建登录面板
public static void createLoginPanel() {
JFrame frame = new JFrame("教育管理系统");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JLabel userLabel = new JLabel("用户名:");
c.gridx = 0;
c.gridy = 0;
panel.add(userLabel, c);
JTextField userText = new JTextField(20);
c.gridx = 1;
c.gridy = 0;
panel.add(userText, c);
JLabel passwordLabel = new JLabel("密码:");
c.gridx = 0;
c.gridy = 1;
panel.add(passwordLabel, c);
JPasswordField passwordText = new JPasswordField(20);
c.gridx = 1;
c.gridy = 1;
panel.add(passwordText, c);
JButton loginButton = new JButton("登录");
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 2;
panel.add(loginButton, c);
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String username = userText.getText();
String password = new String(passwordText.getPassword());
if (validateLogin(username, password)) {
frame.dispose();
createMainPanel();
} else {
loginAttempts++;
if (loginAttempts >= 3) {
JOptionPane.showMessageDialog(null, "登录失败次数超过限制,账号被锁定1分钟。");
loginAttempts = 0;
Timer timer = new Timer(60000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
((Timer) e.getSource()).stop();
}
});
timer.start();
} else {
JOptionPane.showMessageDialog(null, "登录失败,请检查用户名和密码。");
}
}
}
});
JButton registerButton = new JButton("注册");
c.gridx = 0;
c.gridy = 3;
c.gridwidth = 2;
panel.add(registerButton, c);
registerButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
createRegisterPanel();
}
});
frame.add(panel);
frame.setVisible(true);
}
// 创建注册面板
public static void createRegisterPanel() {
JFrame frame = new JFrame("注册");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(400, 200);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JLabel userLabel = new JLabel("用户名:");
c.gridx = 0;
c.gridy = 0;
panel.add(userLabel, c);
JTextField userText = new JTextField(20);
c.gridx = 1;
c.gridy = 0;
panel.add(userText, c);
JLabel passwordLabel = new JLabel("密码:");
c.gridx = 0;
c.gridy = 1;
panel.add(passwordLabel, c);
JPasswordField passwordText = new JPasswordField(20);
c.gridx = 1;
c.gridy = 1;
panel.add(passwordText, c);
JLabel confirmPasswordLabel = new JLabel("确认密码:");
c.gridx = 0;
c.gridy = 2;
panel.add(confirmPasswordLabel, c);
JPasswordField confirmPasswordText = new JPasswordField(20);
c.gridx = 1;
c.gridy = 2;
panel.add(confirmPasswordText, c);
JButton registerButton = new JButton("注册");
c.gridx = 0;
c.gridy = 3;
c.gridwidth = 2;
panel.add(registerButton, c);
registerButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String username = userText.getText();
String password = new String(passwordText.getPassword());
String confirmPassword = new String(confirmPasswordText.getPassword());
if (!password.equals(confirmPassword)) {
JOptionPane.showMessageDialog(null, "两次输入的密码不一致,请重新输入。");
} else {
try {
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql = "INSERT INTO users (username, password) VALUES ('" + username + "', '" + password
+ "')";
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "注册成功。");
frame.dispose();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "用户名已存在,请重新输入。");
} finally {
try {
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
}
}
});
frame.add(panel);
frame.setVisible(true);
}
// 验证登录信息
public static boolean validateLogin(String username, String password) {
try {
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql = "SELECT * FROM users WHERE username='" + username + "' AND password='" + password + "'";
ResultSet rs = stmt.executeQuery(sql);
return rs.next();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
return false;
} finally {
try {
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
}
// 创建主面板
public static void createMainPanel() {
JFrame frame = new JFrame("教育管理系统");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
JPanel panel = new JPanel(new BorderLayout());
JTabbedPane tabbedPane = new JTabbedPane();
JPanel subjectPanel = createSubjectPanel();
tabbedPane.addTab("学科信息", subjectPanel);
JPanel majorPanel = createMajorPanel();
tabbedPane.addTab("专业信息", majorPanel);
JPanel classPanel = createClassPanel();
tabbedPane.addTab("班级信息", classPanel);
JPanel studentPanel = createStudentPanel();
tabbedPane.addTab("学生信息", studentPanel);
panel.add(tabbedPane, BorderLayout.CENTER);
frame.add(panel);
frame.setVisible(true);
}
// 创建学科信息面板
public static JPanel createSubjectPanel() {
JPanel panel = new JPanel(new BorderLayout());
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JButton addButton = new JButton("增加");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
createSubjectDialog();
}
});
buttonPanel.add(addButton);
JButton deleteButton = new JButton("删除");
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int row = subjectTable.getSelectedRow();
if (row >= 0) {
int id = (int) subjectTable.getValueAt(row, 0);
int confirm = JOptionPane.showConfirmDialog(null, "确定要删除该学科吗?", "确认删除",
JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION) {
try {
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql = "DELETE FROM subjects WHERE id=" + id;
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "删除成功。");
refreshSubjectTable();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
} finally {
try {
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
}
} else {
JOptionPane.showMessageDialog(null, "请选择要删除的学科。");
}
}
});
buttonPanel.add(deleteButton);
JButton editButton = new JButton("修改");
editButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int row = subjectTable.getSelectedRow();
if (row >= 0) {
int id = (int) subjectTable.getValueAt(row, 0);
String name = (String) subjectTable.getValueAt(row, 1);
createSubjectDialog(id, name);
} else {
JOptionPane.showMessageDialog(null, "请选择要修改的学科。");
}
}
});
buttonPanel.add(editButton);
JButton refreshButton = new JButton("刷新");
refreshButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
refreshSubjectTable();
}
});
buttonPanel.add(refreshButton);
panel.add(buttonPanel, BorderLayout.NORTH);
String[] columnNames = { "编号", "名称" };
Object[][] data = {};
subjectTableModel = new DefaultTableModel(data, columnNames);
subjectTable = new JTable(subjectTableModel);
subjectTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scrollPane = new JScrollPane(subjectTable);
panel.add(scrollPane, BorderLayout.CENTER);
refreshSubjectTable();
return panel;
}
// 刷新学科信息表格
public static void refreshSubjectTable() {
try {
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql = "SELECT * FROM subjects";
ResultSet rs = stmt.executeQuery(sql);
subjectTableModel.setRowCount(0);
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
Object[] row = { id, name };
subjectTableModel.addRow(row);
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
} finally {
try {
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
}
// 创建学科信息对话框
public static void createSubjectDialog() {
createSubjectDialog(0, "");
}
// 创建学科信息对话框
public static void createSubjectDialog(int id, String name) {
JFrame frame = new JFrame("学科信息");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(400, 200);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JLabel nameLabel = new JLabel("名称:");
c.gridx = 0;
c.gridy = 0;
panel.add(nameLabel, c);
JTextField nameText
阅读全文