You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

226 lines
9.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.WR.StudentMS.view;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import com.WR.StudentMS.dao.mysql.ZhuanyeDaoImpWR;
import com.WR.StudentMS.model.Classmqsy;
import com.WR.StudentMS.model.ZhuanyeWR;
import java.awt.*;
import java.util.List;
import java.util.Vector;
import java.util.stream.Collectors;
public class ZhuanyePanel extends JPanel {
private List<ZhuanyeWR> zhuanyeList; // 存储从数据库加载的专业信息列表
private ZhuanyeDaoImpWR zhuanyeDao; // 数据访问对象
private JTable table; // 显示专业信息的表格
private DefaultTableModel model; // 表格的数据模型
private JTextField searchField;
private JButton searchButton;
public ZhuanyePanel() {
super(new BorderLayout()); // 使用BorderLayout布局管理器
zhuanyeDao = new ZhuanyeDaoImpWR();
model = new DefaultTableModel();
initializeUI();
loadZhuanyes(); // 加载专业信息
}
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 -> addZhuanye());
deleteButton.addActionListener(e -> deleteSelectedZhuanye());
searchButton.addActionListener(e -> searchZhuanye());
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 loadZhuanyes() {
// 从数据库加载专业信息并更新表格和列表
zhuanyeList = zhuanyeDao.findAll();
model.setRowCount(0); // 清空表格
for (ZhuanyeWR zhuanye : zhuanyeList) {
Object[] rowData = {
zhuanye.getId(),
zhuanye.getName(),
zhuanye.getJieshao()
};
model.addRow(rowData);
}
}
private void addZhuanye() {
String input = (String) JOptionPane.showInputDialog(
this,
"请输入专业信息格式专业ID-专业名-介绍):",
"添加专业信息",
JOptionPane.QUESTION_MESSAGE,
null,
null,
null
);
if (input != null && !input.trim().isEmpty()) {
String[] parts = input.split("-", 3); // 修改为分割三部分
if (parts.length == 3) { // 确保分割成三部分
try {
ZhuanyeWR newZhuanye = new ZhuanyeWR();
newZhuanye.setId(Integer.parseInt(parts[0].trim())); // 确保ID是整数
newZhuanye.setName(parts[1].trim());
newZhuanye.setJieshao(parts[2].trim());
int result = zhuanyeDao.create(newZhuanye);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
if (result > 0) {
// 刷新班级列表
loadZhuanyes();
JOptionPane.showMessageDialog(ZhuanyePanel.this, "专业信息添加成功!");
} else {
// 显示添加失败的消息
JOptionPane.showMessageDialog(ZhuanyePanel.this, "专业信息添加失败!");
}
}
});
} catch (NumberFormatException e) {
// 专业ID格式错误
JOptionPane.showMessageDialog(this, "专业ID必须是整数");
} catch (Exception e) {
// 其他异常情况
JOptionPane.showMessageDialog(this, "添加专业信息失败: " + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
}
} else {
// 输入格式不正确
JOptionPane.showMessageDialog(this, "输入格式不正确需要专业ID-专业名-介绍!");
}
}
}
private void deleteSelectedZhuanye() {
int selectedRow = table.getSelectedRow();
if (selectedRow >= 0) {
int zhuanyeId = (int) model.getValueAt(selectedRow, 0);
ZhuanyeWR zhuanyeToDelete = zhuanyeDao.findById(zhuanyeId);
if (zhuanyeToDelete != null) {
int result = zhuanyeDao.remove(zhuanyeToDelete);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
if (result > 0) {
model.removeRow(selectedRow); // 从表格中移除
JOptionPane.showMessageDialog(ZhuanyePanel.this, "班级信息删除成功!");
} else {
JOptionPane.showMessageDialog(ZhuanyePanel.this, "班级信息删除失败可能班级ID不存在或删除记录未受影响");
}
}
});
} else {
JOptionPane.showMessageDialog(this, "未找到对应的班级信息!");
}
}
}
private void searchZhuanye() {
String searchQuery = searchField.getText();
if (!searchQuery.trim().isEmpty()) {
// 过滤专业信息列表仅根据专业ID进行搜索
List<ZhuanyeWR> filteredList = zhuanyeList.stream()
.filter(zhuanye -> String.valueOf(zhuanye.getId()).equals(searchQuery)) // 仅根据专业ID过滤
.collect(Collectors.toList());
// 更新表格模型以显示过滤后的专业信息
model.setRowCount(0); // 清空表格
for (ZhuanyeWR zhuanye : filteredList) {
Object[] rowData = new Object[]{
zhuanye.getId(),
zhuanye.getName(),
zhuanye.getJieshao()
};
model.addRow(rowData);
}
} else {
// 如果搜索框为空,则重新加载所有专业信息
loadZhuanyes();
}
}
// 在ZhuanyePanel类中添加saveChanges方法
private void saveChanges() {
// 获取表格中所有行的数据
for (int i = 0; i < model.getRowCount(); i++) {
// 从表格模型获取第 i 行的 Vector<Object>
Vector<Object> row = (Vector<Object>) model.getDataVector().elementAt(i);
// 从 Vector 中获取每个字段的值
int id = Integer.parseInt(row.elementAt(0).toString()); // 假设ID存储在第一列
String name = (String) row.elementAt(1); // 专业名存储在第二列
String introduction = (String) row.elementAt(2); // 介绍存储在第三列
// 创建ZhuanyeWR对象
ZhuanyeWR zhuanye = new ZhuanyeWR();
zhuanye.setId(id);
zhuanye.setName(name);
zhuanye.setJieshao(introduction);
// 检查专业ID是否已存在若存在则更新否则创建新记录
ZhuanyeWR existingZhuanye = zhuanyeDao.findById(id);
if (existingZhuanye != null) {
// 专业信息已存在,执行更新操作
int updateResult = zhuanyeDao.modify(zhuanye);
if (updateResult > 0) {
// 更新成功
} else {
// 更新失败,提示错误信息
JOptionPane.showMessageDialog(this, "更新专业信息失败。", "错误", JOptionPane.ERROR_MESSAGE);
}
} else {
// 专业信息不存在,执行创建操作
int createResult = zhuanyeDao.create(zhuanye);
if (createResult > 0) {
// 创建成功
} else {
// 创建失败,提示错误信息
JOptionPane.showMessageDialog(this, "创建专业信息失败。", "错误", JOptionPane.ERROR_MESSAGE);
}
}
}
// 保存完成后,可以刷新界面或给出提示
loadZhuanyes(); // 重新加载专业信息以刷新表格
JOptionPane.showMessageDialog(this, "所有更改已成功保存。", "成功", JOptionPane.INFORMATION_MESSAGE);
}
}