|
|
package com.WR.StudentMS.view;
|
|
|
|
|
|
import javax.swing.*;
|
|
|
import javax.swing.table.DefaultTableModel;
|
|
|
import java.awt.*;
|
|
|
import java.util.List;
|
|
|
import java.util.Vector;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import com.WR.StudentMS.model.Registrationwmm;
|
|
|
import com.WR.StudentMS.dao.mysql.Registrationdaoimplwmm;
|
|
|
|
|
|
public class RegistrationPanel extends JPanel {
|
|
|
private List<Registrationwmm> registrationList;
|
|
|
private Registrationdaoimplwmm registrationDao;
|
|
|
private JTable table;
|
|
|
private DefaultTableModel model;
|
|
|
private JTextField searchField;
|
|
|
private JButton searchButton;
|
|
|
|
|
|
|
|
|
public RegistrationPanel() {
|
|
|
super(new BorderLayout()); // 使用BorderLayout布局管理器
|
|
|
registrationDao = new Registrationdaoimplwmm();
|
|
|
model = new DefaultTableModel();
|
|
|
initializeUI();
|
|
|
loadRegistrations();
|
|
|
}
|
|
|
|
|
|
private void initializeUI() {
|
|
|
// 定义列名
|
|
|
String[] columnNames = {"学号ID", "姓名", "性别", "年龄", "班级", "入学日期", "毕业日期", "在校时间", "学院"};
|
|
|
model.setColumnIdentifiers(columnNames);
|
|
|
table = new JTable(model);
|
|
|
|
|
|
// 添加表格到滚动面板
|
|
|
JScrollPane scrollPane = new JScrollPane(table);
|
|
|
add(scrollPane, BorderLayout.CENTER);
|
|
|
|
|
|
// 创建按钮面板
|
|
|
JPanel buttonPanel = new JPanel();
|
|
|
buttonPanel.setLayout(new FlowLayout());
|
|
|
|
|
|
// 添加按钮
|
|
|
JButton addButton = new JButton("添加学籍信息");
|
|
|
JButton deleteButton = new JButton("删除选中的学籍信息");
|
|
|
JButton saveButton = new JButton("保存更改");
|
|
|
|
|
|
|
|
|
// 创建搜索框和搜索按钮
|
|
|
searchField = new JTextField();
|
|
|
searchField.setPreferredSize(new Dimension(200, searchField.getPreferredSize().height));
|
|
|
searchButton = new JButton("学号ID查询");
|
|
|
|
|
|
|
|
|
// 为按钮添加事件监听器
|
|
|
addButton.addActionListener(e -> addRegistration());
|
|
|
deleteButton.addActionListener(e -> deleteSelectedRegistration());
|
|
|
searchButton.addActionListener(e -> searchRegistration());
|
|
|
saveButton.addActionListener(e -> saveChanges());
|
|
|
|
|
|
|
|
|
// 将按钮添加到面板
|
|
|
buttonPanel.add(addButton);
|
|
|
buttonPanel.add(deleteButton);
|
|
|
buttonPanel.add(searchField);
|
|
|
buttonPanel.add(searchButton); // 添加搜索按钮到面板
|
|
|
buttonPanel.add(saveButton);
|
|
|
|
|
|
// 将按钮面板添加到底部
|
|
|
add(buttonPanel, BorderLayout.SOUTH);
|
|
|
}
|
|
|
|
|
|
private void loadRegistrations() {
|
|
|
// 从数据库加载学籍信息并更新表格
|
|
|
registrationList = registrationDao.findAll();
|
|
|
model.setRowCount(0); // 清空表格
|
|
|
for (Registrationwmm registration : registrationList) {
|
|
|
Object[] rowData = new Object[]{
|
|
|
registration.getStudentidwmm(),
|
|
|
registration.getNamewmm(),
|
|
|
registration.getSexwmm(),
|
|
|
registration.getAgewmm(),
|
|
|
registration.getClasswmm(),
|
|
|
registration.getAdmissionwmm(),
|
|
|
registration.getGraduationwmm(),
|
|
|
registration.getLengthwmm(),
|
|
|
registration.getCollegewmm()
|
|
|
};
|
|
|
model.addRow(rowData);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void addRegistration() {
|
|
|
// 弹出对话框让用户输入学籍信息
|
|
|
String input = (String) JOptionPane.showInputDialog(
|
|
|
this,
|
|
|
"请输入学籍信息(格式:学号ID/姓名/性别/年龄/班级/入学日期/毕业日期/在校时间/学院):",
|
|
|
"添加学籍信息",
|
|
|
JOptionPane.QUESTION_MESSAGE,
|
|
|
null,
|
|
|
null,
|
|
|
null
|
|
|
);
|
|
|
if (input != null && !input.trim().isEmpty()) {
|
|
|
// 使用"/"作为分隔符分割字符串
|
|
|
String[] parts = input.split("/", 9); // 最多9部分,因为最后一部分没有分隔符
|
|
|
if (parts.length >= 9) {
|
|
|
Registrationwmm newRegistration = new Registrationwmm();
|
|
|
newRegistration.setStudentidwmm(parts[0].trim());
|
|
|
newRegistration.setNamewmm(parts[1].trim());
|
|
|
newRegistration.setSexwmm(parts[2].trim());
|
|
|
newRegistration.setAgewmm(parts[3].trim());
|
|
|
newRegistration.setClasswmm(parts[4].trim());
|
|
|
newRegistration.setAdmissionwmm(parts[5].trim());
|
|
|
newRegistration.setGraduationwmm(parts[6].trim());
|
|
|
newRegistration.setLengthwmm(parts[7].trim());
|
|
|
newRegistration.setCollegewmm(parts[8].trim());
|
|
|
int result = registrationDao.create(newRegistration);
|
|
|
if (result > 0) {
|
|
|
Object[] rowData = new Object[]{
|
|
|
newRegistration.getStudentidwmm(),
|
|
|
newRegistration.getNamewmm(),
|
|
|
newRegistration.getSexwmm(),
|
|
|
newRegistration.getAgewmm(),
|
|
|
newRegistration.getClasswmm(),
|
|
|
newRegistration.getAdmissionwmm(),
|
|
|
newRegistration.getGraduationwmm(),
|
|
|
newRegistration.getLengthwmm(),
|
|
|
newRegistration.getCollegewmm()
|
|
|
};
|
|
|
model.addRow(rowData); // 添加到表格
|
|
|
JOptionPane.showMessageDialog(this, "学籍信息添加成功!");
|
|
|
}
|
|
|
else {
|
|
|
JOptionPane.showMessageDialog(this, "学籍信息添加失败!");
|
|
|
}
|
|
|
} else {
|
|
|
JOptionPane.showMessageDialog(this, "输入格式不正确,需要按照提示的格式输入信息!");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void deleteSelectedRegistration() {
|
|
|
int selectedRow = table.getSelectedRow();
|
|
|
if (selectedRow >= 0) {
|
|
|
String studentId = (String) model.getValueAt(selectedRow, 0);
|
|
|
Registrationwmm registrationToDelete = new Registrationwmm();
|
|
|
registrationToDelete.setStudentidwmm(studentId);
|
|
|
int result = registrationDao.remove(registrationToDelete);
|
|
|
if (result > 0) {
|
|
|
model.removeRow(selectedRow); // 从表格中移除
|
|
|
JOptionPane.showMessageDialog(this, "学籍信息删除成功!");
|
|
|
} else {
|
|
|
JOptionPane.showMessageDialog(this, "学籍信息删除失败!");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
private void searchRegistration() {
|
|
|
String searchQuery = searchField.getText();
|
|
|
if (!searchQuery.trim().isEmpty()) {
|
|
|
// 过滤学籍信息列表,仅根据学号ID进行搜索
|
|
|
List<Registrationwmm> filteredList = registrationList.stream()
|
|
|
.filter(registration -> registration.getStudentidwmm().equals(searchQuery)) // 仅根据学号ID过滤
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
// 更新表格模型以显示过滤后的学籍信息
|
|
|
model.setRowCount(0); // 清空表格
|
|
|
for (Registrationwmm registration : filteredList) {
|
|
|
Object[] rowData = new Object[]{
|
|
|
registration.getStudentidwmm(),
|
|
|
registration.getNamewmm(),
|
|
|
registration.getSexwmm(),
|
|
|
registration.getAgewmm(),
|
|
|
registration.getClasswmm(),
|
|
|
registration.getAdmissionwmm(),
|
|
|
registration.getGraduationwmm(),
|
|
|
registration.getLengthwmm(),
|
|
|
registration.getCollegewmm()
|
|
|
};
|
|
|
model.addRow(rowData);
|
|
|
}
|
|
|
} else {
|
|
|
// 如果搜索框为空,则重新加载所有学籍信息
|
|
|
loadRegistrations();
|
|
|
}
|
|
|
}
|
|
|
private void saveChanges() {
|
|
|
// 遍历表格中的每一行
|
|
|
for (int i = 0; i < model.getRowCount(); i++) {
|
|
|
// 获取表格行数据
|
|
|
Vector<Object> rowVector = (Vector<Object>) model.getDataVector().elementAt(i);
|
|
|
// 从 rowVector 中获取每个字段的值
|
|
|
String studentId = (String) rowVector.elementAt(0); // 学号ID存储在第一列
|
|
|
String name = (String) rowVector.elementAt(1); // 姓名存储在第二列
|
|
|
String sex = (String) rowVector.elementAt(2); // 性别存储在第三列
|
|
|
String age = (String) rowVector.elementAt(3); // 年龄存储在第四列
|
|
|
String classwmm = (String) rowVector.elementAt(4); // 班级存储在第五列
|
|
|
String admission = (String) rowVector.elementAt(5); // 入学日期存储在第六列
|
|
|
String graduation = (String) rowVector.elementAt(6); // 毕业日期存储在第七列
|
|
|
String length = (String) rowVector.elementAt(7); // 在校时间存储在第八列
|
|
|
String college = (String) rowVector.elementAt(8); // 学院存储在第九列
|
|
|
|
|
|
|
|
|
|
|
|
// 创建Registrationwmm对象
|
|
|
Registrationwmm registration = new Registrationwmm();
|
|
|
registration.setStudentidwmm(studentId);
|
|
|
registration.setNamewmm(name);
|
|
|
registration.setSexwmm(sex);
|
|
|
registration.setAgewmm(age);
|
|
|
registration.setClasswmm(classwmm);
|
|
|
registration.setAdmissionwmm(admission);
|
|
|
registration.setGraduationwmm(graduation);
|
|
|
registration.setLengthwmm(length);
|
|
|
registration.setCollegewmm(college);
|
|
|
|
|
|
// 检查学号ID是否已存在,若存在则更新,否则创建新记录
|
|
|
Registrationwmm existingRegistration = registrationDao.findById(studentId);
|
|
|
if (existingRegistration != null) {
|
|
|
// 如果学籍信息已存在,则更新
|
|
|
int updateResult = registrationDao.modify(registration);
|
|
|
if (updateResult > 0) {
|
|
|
JOptionPane.showMessageDialog(this, "更新成绩记录成功。", "正确", JOptionPane.INFORMATION_MESSAGE);
|
|
|
}else{
|
|
|
// 更新失败,显示错误消息
|
|
|
JOptionPane.showMessageDialog(this, "更新学籍信息失败。", "错误", JOptionPane.ERROR_MESSAGE);
|
|
|
}
|
|
|
} else {
|
|
|
// 如果学籍信息不存在,则创建
|
|
|
int createResult = registrationDao.create(registration);
|
|
|
if (createResult <= 0) {
|
|
|
// 创建失败,显示错误消息
|
|
|
JOptionPane.showMessageDialog(this, "创建学籍信息失败。", "错误", JOptionPane.ERROR_MESSAGE);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 保存完成后,刷新表格数据
|
|
|
loadRegistrations();
|
|
|
// 显示保存成功的提示信息
|
|
|
JOptionPane.showMessageDialog(this, "所有更改已成功保存。", "成功", JOptionPane.INFORMATION_MESSAGE);
|
|
|
}
|
|
|
} |