|
|
package 学生成绩查询管理系统.功能;
|
|
|
|
|
|
|
|
|
|
|
|
import 学生成绩查询管理系统.界面.Login;
|
|
|
import 学生成绩查询管理系统.界面.StudentSystem;
|
|
|
|
|
|
import javax.swing.*;
|
|
|
import javax.swing.table.DefaultTableModel;
|
|
|
import java.awt.*;
|
|
|
import java.awt.event.ActionEvent;
|
|
|
import java.awt.event.ActionListener;
|
|
|
import java.sql.*;
|
|
|
|
|
|
public class ClassroomManager extends JFrame implements ActionListener {
|
|
|
private JMenuItem reLogin;
|
|
|
private JMenuItem menu;
|
|
|
private JMenuItem exit;
|
|
|
private JMenuItem aboutMe;
|
|
|
String path = "C:\\Users\\23513\\Desktop\\学生成绩管理系统\\";
|
|
|
private static final String URL = "jdbc:mysql://localhost:3306/studentmanager";
|
|
|
private static final String USERNAME = "root";
|
|
|
private static final String PASSWORD = "123456";
|
|
|
|
|
|
private static Connection connection;
|
|
|
private DefaultTableModel tableModel;
|
|
|
private JTable dataTable;
|
|
|
|
|
|
public ClassroomManager() {
|
|
|
initializeDatabase();
|
|
|
initializeMainFrame();
|
|
|
queryRecords(); // Auto-load data from the database upon initializing the main frame
|
|
|
initMenu();
|
|
|
}
|
|
|
|
|
|
// 初始化数据库连接
|
|
|
private void initializeDatabase() {
|
|
|
try {
|
|
|
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
|
|
|
} catch (SQLException e) {
|
|
|
e.printStackTrace();
|
|
|
JOptionPane.showMessageDialog(this, "无法连接到数据库", "错误", JOptionPane.ERROR_MESSAGE);
|
|
|
System.exit(1);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 初始化主界面
|
|
|
private void initializeMainFrame() {
|
|
|
setTitle("学生信息管理界面");
|
|
|
setSize(600, 450);
|
|
|
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
|
|
setLocationRelativeTo(null);
|
|
|
ImageIcon imageIcon = new ImageIcon( "imge\\Login1.2.png");
|
|
|
JLabel label = new JLabel(imageIcon);
|
|
|
this.getContentPane().add(label, BorderLayout.CENTER);
|
|
|
// 修改图标
|
|
|
Toolkit tk = Toolkit.getDefaultToolkit();
|
|
|
// 获取图片 三种图片格式都可以
|
|
|
java.awt.Image img = tk.getImage( "imge\\成绩录入2.png");
|
|
|
this.setIconImage(img);//详细分析
|
|
|
|
|
|
// 创建表格模型和表格
|
|
|
tableModel = new DefaultTableModel();
|
|
|
tableModel.addColumn("ID");
|
|
|
tableModel.addColumn("姓名");
|
|
|
tableModel.addColumn("年龄");
|
|
|
tableModel.addColumn("性别");
|
|
|
tableModel.addColumn("年级");
|
|
|
tableModel.addColumn("班级");
|
|
|
|
|
|
dataTable = new JTable(tableModel);
|
|
|
|
|
|
// 创建按钮面板
|
|
|
JPanel buttonPanel = new JPanel();
|
|
|
JButton addButton = new JButton("添加数据");
|
|
|
JButton displayButton = new JButton("显示数据");
|
|
|
JButton deleteButton = new JButton("删除数据");
|
|
|
|
|
|
// 将按钮添加到按钮面板
|
|
|
buttonPanel.add(addButton);
|
|
|
buttonPanel.add(displayButton);
|
|
|
buttonPanel.add(deleteButton);
|
|
|
|
|
|
// 添加按钮点击事件监听器
|
|
|
addButton.addActionListener(e -> showAddDataDialog());
|
|
|
displayButton.addActionListener(e -> showDisplayDataDialog());
|
|
|
deleteButton.addActionListener(e -> deleteSelectedRecord());
|
|
|
|
|
|
// 将按钮面板和表格添加到主界面
|
|
|
add(buttonPanel, BorderLayout.NORTH);
|
|
|
add(new JScrollPane(dataTable), BorderLayout.CENTER);
|
|
|
}
|
|
|
|
|
|
// 显示添加数据的对话框
|
|
|
private void showAddDataDialog() {
|
|
|
AddDataDialog addDataDialog = new AddDataDialog(this, "添加数据", true);
|
|
|
addDataDialog.setLocationRelativeTo(this);
|
|
|
addDataDialog.setVisible(true);
|
|
|
}
|
|
|
|
|
|
// 显示显示数据的对话框
|
|
|
private void showDisplayDataDialog() {
|
|
|
queryRecords();
|
|
|
}
|
|
|
|
|
|
// 查询数据库中的记录
|
|
|
private void queryRecords() {
|
|
|
try {
|
|
|
Statement statement = connection.createStatement();
|
|
|
ResultSet resultSet = statement.executeQuery("SELECT * FROM student");
|
|
|
|
|
|
tableModel.setRowCount(0);
|
|
|
|
|
|
|
|
|
while (resultSet.next()) {
|
|
|
Object[] rowData = {
|
|
|
resultSet.getInt("id"),
|
|
|
resultSet.getString("name"),
|
|
|
resultSet.getInt("age"),
|
|
|
resultSet.getString("sex"),
|
|
|
resultSet.getString("grade"),
|
|
|
resultSet.getString("classroom")
|
|
|
};
|
|
|
tableModel.addRow(rowData);
|
|
|
}
|
|
|
|
|
|
// Close resources
|
|
|
resultSet.close();
|
|
|
statement.close();
|
|
|
} catch (SQLException e) {
|
|
|
e.printStackTrace();
|
|
|
JOptionPane.showMessageDialog(this, "查询数据失败", "错误", JOptionPane.ERROR_MESSAGE);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 删除选定的记录
|
|
|
private void deleteSelectedRecord() {
|
|
|
int selectedRow = dataTable.getSelectedRow();
|
|
|
|
|
|
if (selectedRow != -1) {
|
|
|
int idToDelete = (int) dataTable.getValueAt(selectedRow, 0);
|
|
|
|
|
|
try {
|
|
|
String deleteQuery = "DELETE FROM student WHERE id=?";
|
|
|
try (PreparedStatement preparedStatement = connection.prepareStatement(deleteQuery)) {
|
|
|
preparedStatement.setInt(1, idToDelete);
|
|
|
preparedStatement.executeUpdate();
|
|
|
}
|
|
|
|
|
|
|
|
|
queryRecords();
|
|
|
|
|
|
JOptionPane.showMessageDialog(this, "记录删除成功", "成功", JOptionPane.INFORMATION_MESSAGE);
|
|
|
} catch (SQLException e) {
|
|
|
e.printStackTrace();
|
|
|
JOptionPane.showMessageDialog(this, "删除记录失败", "错误", JOptionPane.ERROR_MESSAGE);
|
|
|
}
|
|
|
} else {
|
|
|
JOptionPane.showMessageDialog(this, "请选择要删除的记录", "警告", JOptionPane.WARNING_MESSAGE);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 添加数据的对话框
|
|
|
private static class AddDataDialog extends JDialog {
|
|
|
private JTextField idTextField;
|
|
|
private JTextField nameTextField;
|
|
|
private JTextField ageTextField;
|
|
|
private JTextField sexTextField;
|
|
|
private JTextField gradeTextField;
|
|
|
private JTextField classroomTextField;
|
|
|
|
|
|
public AddDataDialog(JFrame parent, String title, boolean modal) {
|
|
|
super(parent, title, modal);
|
|
|
setSize(300, 200);
|
|
|
|
|
|
// 初始化文本框和按钮
|
|
|
idTextField = new JTextField(15);
|
|
|
nameTextField = new JTextField(15);
|
|
|
ageTextField = new JTextField(15);
|
|
|
sexTextField = new JTextField(15);
|
|
|
gradeTextField = new JTextField(15);
|
|
|
classroomTextField = new JTextField(15);
|
|
|
|
|
|
JButton addButton = new JButton("添加");
|
|
|
addButton.addActionListener(e -> addRecord());
|
|
|
|
|
|
// 创建面板,并添加标签、文本框和按钮
|
|
|
JPanel panel = new JPanel(new GridLayout(7, 2, 10, 10));
|
|
|
panel.add(new JLabel("ID:"));
|
|
|
panel.add(idTextField);
|
|
|
panel.add(new JLabel("姓名:"));
|
|
|
panel.add(nameTextField);
|
|
|
panel.add(new JLabel("年龄:"));
|
|
|
panel.add(ageTextField);
|
|
|
panel.add(new JLabel("性别:"));
|
|
|
panel.add(sexTextField);
|
|
|
panel.add(new JLabel("年级:"));
|
|
|
panel.add(gradeTextField);
|
|
|
panel.add(new JLabel("班级:"));
|
|
|
panel.add(classroomTextField);
|
|
|
panel.add(new JLabel()); // 空标签用于调整布局
|
|
|
panel.add(addButton);
|
|
|
|
|
|
// 将面板添加到对话框
|
|
|
add(panel);
|
|
|
}
|
|
|
|
|
|
// 添加记录到数据库
|
|
|
private void addRecord() {
|
|
|
try {
|
|
|
// Get values from text fields
|
|
|
int id = Integer.parseInt(idTextField.getText());
|
|
|
String name = nameTextField.getText();
|
|
|
int age = Integer.parseInt(ageTextField.getText());
|
|
|
String sex = sexTextField.getText();
|
|
|
String grade = gradeTextField.getText(); // Update to getString for non-numeric grade
|
|
|
String classroom = classroomTextField.getText();
|
|
|
|
|
|
// Insert the record into the database
|
|
|
String insertQuery = "INSERT INTO student (id, name, age, sex, grade, classroom) VALUES (?, ?, ?, ?, ?, ?)";
|
|
|
try (PreparedStatement preparedStatement = connection.prepareStatement(insertQuery)) {
|
|
|
preparedStatement.setInt(1, id);
|
|
|
preparedStatement.setString(2, name);
|
|
|
preparedStatement.setInt(3, age);
|
|
|
preparedStatement.setString(4, sex);
|
|
|
preparedStatement.setString(5, grade); // Update to setString for non-numeric grade
|
|
|
preparedStatement.setString(6, classroom);
|
|
|
|
|
|
preparedStatement.executeUpdate();
|
|
|
}
|
|
|
|
|
|
// Update the table model with the new data
|
|
|
((ClassroomManager) getParent()).queryRecords();
|
|
|
|
|
|
// Close the dialog
|
|
|
JOptionPane.showMessageDialog(this, "记录添加成功", "成功", JOptionPane.INFORMATION_MESSAGE);
|
|
|
dispose();
|
|
|
} catch (NumberFormatException | SQLException e) {
|
|
|
e.printStackTrace();
|
|
|
JOptionPane.showMessageDialog(this, "添加记录失败", "错误", JOptionPane.ERROR_MESSAGE);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
private void initMenu() {
|
|
|
JMenuBar jMenuBar = new JMenuBar();
|
|
|
JMenu about = new JMenu("关于我");
|
|
|
JMenu function = new JMenu("功能");
|
|
|
aboutMe = new JMenuItem("QQ二维码");
|
|
|
reLogin = new JMenuItem("重新登录");
|
|
|
menu = new JMenuItem("主页");
|
|
|
exit = new JMenuItem("退出");
|
|
|
|
|
|
about.add(aboutMe);
|
|
|
function.add(reLogin);
|
|
|
function.add(menu);
|
|
|
function.add(exit);
|
|
|
|
|
|
aboutMe.addActionListener(this);
|
|
|
reLogin.addActionListener(this);
|
|
|
menu.addActionListener(this);
|
|
|
exit.addActionListener(this);
|
|
|
|
|
|
jMenuBar.add(about);
|
|
|
jMenuBar.add(function);
|
|
|
this.setJMenuBar(jMenuBar);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
// 获取当前被点击的条目对象
|
|
|
Object obj = e.getSource();
|
|
|
if (obj == reLogin) {
|
|
|
dispose();
|
|
|
new Login();
|
|
|
} else if (obj == menu) {
|
|
|
dispose();
|
|
|
new StudentSystem();
|
|
|
} else if (obj == exit) {
|
|
|
System.exit(0);
|
|
|
} else if (obj == aboutMe) {
|
|
|
// 创建一个弹框对象
|
|
|
JDialog jDialog = new JDialog();
|
|
|
Toolkit tk = Toolkit.getDefaultToolkit();
|
|
|
Image img = tk.getImage("imge\\成绩录入2.png");
|
|
|
jDialog.setIconImage(img);
|
|
|
// 创建一个管理图片的容器对象JLabel
|
|
|
JLabel jLabel = new JLabel(new ImageIcon("imge\\about.png"));
|
|
|
// 设置位置和宽高
|
|
|
jLabel.setBounds(0, 0, 300, 380);
|
|
|
// 把图片添加到弹框当中
|
|
|
jDialog.getContentPane().add(jLabel);
|
|
|
// 给弹框设置大小
|
|
|
jDialog.setSize(400, 400);
|
|
|
// 让弹框置顶
|
|
|
jDialog.setAlwaysOnTop(true);
|
|
|
// 让弹框居中
|
|
|
jDialog.setLocationRelativeTo(null);
|
|
|
// 弹框不关闭则无法操作下面的界面
|
|
|
jDialog.setModal(true);
|
|
|
// 让弹框显示出来
|
|
|
jDialog.setVisible(true);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
// 主方法,启动Swing应用程序
|
|
|
public static void main(String[] args) {
|
|
|
SwingUtilities.invokeLater(() -> {
|
|
|
ClassroomManager scoreManager = new ClassroomManager();
|
|
|
scoreManager.setVisible(true);
|
|
|
});
|
|
|
}
|
|
|
}
|