import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.*;
public class StudentTable extends JFrame {
private JTable table;
private DefaultTableModel tableModel;
private JTextField studentIdField;
private JTextField nameField;
private JTextField englishField;
private JTextField mathField;
private JTextField chineseField;
private JTextField pyField;
private JTextField huaField;
private JTextField swField;
private JButton saveButton;
private JButton loadButton;
private JButton statisticsButton;
private Connection connection;
private Statement statement;
public StudentTable() {
setTitle("学生信息管理");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
// 创建表格模型
tableModel = new DefaultTableModel() {
@Override
public boolean isCellEditable(int row, int column) {
// 设置序号列不可编辑
return column != 0;
}
};
tableModel.addColumn("序号");
tableModel.addColumn("学号");
tableModel.addColumn("姓名");
tableModel.addColumn("英语");
tableModel.addColumn("数学");
tableModel.addColumn("语文");
tableModel.addColumn("物理");
tableModel.addColumn("化学");
tableModel.addColumn("生物");
// 创建表格
table = new JTable(tableModel);
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
editRow();
}
}
});
// 创建插入面板
JPanel insertPanel = createInsertPanel();
// 创建按钮面板
JPanel buttonPanel = createButtonPanel();
// 创建面板并添加按钮和表格
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(insertPanel, BorderLayout.SOUTH);
panel.add(buttonPanel, BorderLayout.NORTH);
panel.add(new JScrollPane(table), BorderLayout.CENTER);
// 将面板添加到窗口中
add(panel);
// 添加窗口关闭事件监听器
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
closeConnection();
}
});
// 连接数据库
connectToDatabase();
setVisible(true);
}
// 创建插入面板
private JPanel createInsertPanel() {
JPanel insertPanel = new JPanel();
insertPanel.setLayout(new FlowLayout());
int a=3;
// 创建学号输入框
studentIdField = new JTextField(a);
insertPanel.add(new JLabel("学号:"));
insertPanel.add(studentIdField);
// 创建姓名输入框
nameField = new JTextField(a);
insertPanel.add(new JLabel("姓名:"));
insertPanel.add(nameField);
// 创建成绩输入框
englishField = new JTextField(a);
insertPanel.add(new JLabel("英语:"));
insertPanel.add(englishField);
mathField = new JTextField(a);
insertPanel.add(new JLabel("数学:"));
insertPanel.add(mathField);
chineseField= new JTextField(a);
insertPanel.add(new JLabel("语文:"));
insertPanel.add(chineseField);
pyField= new JTextField(a);
insertPanel.add(new JLabel("物理:"));
insertPanel.add(pyField);
huaField= new JTextField(a);
insertPanel.add(new JLabel("化学:"));
insertPanel.add(huaField);
swField= new JTextField(a);
insertPanel.add(new JLabel("生物:"));
insertPanel.add(swField);
// 创建插入按钮
JButton insertButton = new JButton("插入");
insertPanel.add(insertButton);
insertButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
insertRow();
}
});
// 创建删除按钮
JButton deleteButton = new JButton("删除");
insertPanel.add(deleteButton);
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
deleteRow();
}
});
return insertPanel;
}
// 创建按钮面板
private JPanel createButtonPanel() {
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
// 创建保存按钮
saveButton = new JButton("保存");
buttonPanel.add(saveButton);
saveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
saveTableData();
}
});
// 创建读出按钮
loadButton = new JButton("读出");
buttonPanel.add(loadButton);
loadButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
loadTableDataFromDatabase();
}
});
// 创建统计按钮
statisticsButton = new JButton("统计");
buttonPanel.add(statisticsButton);
statisticsButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
calculateAverageGrade();
}
});
return buttonPanel;
}
// 插入新行
private void insertRow() {
// 获取用户输入的学号、姓名和成绩
String studentId = studentIdField.getText();
String name = nameField.getText();
String eg = englishField.getText();
String ma = mathField.getText();
String ch = chineseField.getText();
String py = pyField.getText();
String hua = huaField.getText();
String sw = swField.getText();
// 插入新行
Object[] rowData = {tableModel.getRowCount() + 1, studentId, name, eg,ma,ch,py,hua,sw};
tableModel.addRow(rowData);
// 清空输入框
studentIdField.setText("");
nameField.setText("");
englishField.setText("");
mathField.setText(" ");
chineseField.setText(" ");
pyField.setText(" ");
huaField.setText(" ");
swField.setText(" ");
}
// 删除选中的行
private void deleteRow() {
int selectedRow = table.getSelectedRow();
if (selectedRow >= 0) {
tableModel.removeRow(selectedRow);
}
}
// 编辑选中的行
private void editRow() {
int selectedRow = table.getSelectedRow();
if (selectedRow >= 0) {
String studentId = tableModel.getValueAt(selectedRow, 1).toString();
String name = tableModel.getValueAt(selectedRow, 2).toString();
String eg = tableModel.getValueAt(selectedRow, 3).toString();
String ma = tableModel.getValueAt(selectedRow, 4).toString();
String ch = tableModel.getValueAt(selectedRow, 5).toString();
String py = tableModel.getValueAt(selectedRow, 6).toString();
String hua = tableModel.getValueAt(selectedRow, 7).toString();
String sw = tableModel.getValueAt(selectedRow, 8).toString();
studentIdField.setText(studentId);
nameField.setText(name);
englishField.setText(eg);
mathField.setText(ma);
chineseField.setText(ch);
pyField.setText(py);
huaField.setText(hua);
swField.setText(sw);
}
}
// 保存表格数据到数据库
private void saveTableData() {
try {
// 清空数据库表
statement.executeUpdate("TRUNCATE TABLE students");
// 逐行插入数据
for (int i = 0; i < tableModel.getRowCount(); i++) {
String studentId = tableModel.getValueAt(i, 1).toString();
String name = tableModel.getValueAt(i, 2).toString();
String English = tableModel.getValueAt(i, 3).toString();
String Math = tableModel.getValueAt(i, 4).toString();
String Chinese = tableModel.getValueAt(i, 5).toString();
String Physics = tableModel.getValueAt(i, 6).toString();
String Chemistry = tableModel.getValueAt(i, 7).toString();
String Biology = tableModel.getValueAt(i, 8).toString();
// 插入数据到数据库表
statement.executeUpdate("INSERT INTO students (studentId, name, English, Math, Chinese, Physics, Chemistry, Biology) VALUES ('" + studentId + "', '" + name + "', '" + English + "', '" + Math + "', '" + Chinese + "', '" + Physics + "', '" + Chemistry + "', '" + Biology + "')");
}
JOptionPane.showMessageDialog(this, "保存成功");
} catch (SQLException e) {
JOptionPane.showMessageDialog(this, "保存失败:" + e.getMessage());
}
}
// 从数据库读取表格数据
private void loadTableDataFromDatabase() {
try {
// 清空表格数据
tableModel.setRowCount(0);
// 从数据库查询数据
ResultSet resultSet = statement.executeQuery("SELECT * FROM students");
// 逐行读取数据并添加到表格模型
while (resultSet.next()) {
String studentId = resultSet.getString("studentId");
String name = resultSet.getString("name");
String eg = resultSet.getString("English");
String ma = resultSet.getString("Math");
String ch = resultSet.getString("Chinese");
String py = resultSet.getString("Physics");
String hu=resultSet.getString("Chemistry");
String bo = resultSet.getString("Biology");
Object[] rowData = {tableModel.getRowCount() + 1, studentId, name, eg,ma,ch,py,hu,bo};
tableModel.addRow(rowData);
}
JOptionPane.showMessageDialog(this, "读取成功");
} catch (SQLException e) {
JOptionPane.showMessageDialog(this, "读取失败:" + e.getMessage());
}
}
// 计算平均成绩
private void calculateAverage(int a,String b) {
double sum = 0;
int count = tableModel.getRowCount();
for (int i = 0; i < count; i++) {
String score = tableModel.getValueAt(i, a).toString();
sum += Double.parseDouble(score);
}
double average = sum / count;
JOptionPane.showMessageDialog(this, b+"平均成绩为:" + average);
}
private void calculateAverageGrade(){
String[] S ={"English", "Math", "Chinese", "Physics", "Chemistry", "Biology"};
for(int i=3;i<9;i++){
calculateAverage(i,S[i-3]);
}
}
// 连接数据库
private void connectToDatabase() {
try {
// 加载数据库驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 连接数据库
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/database", "root", "root");//记得修改成你的数据库
// 创建Statement对象
statement = connection.createStatement();
// 创建数据库表
statement.executeUpdate("CREATE TABLE IF NOT EXISTS students (studentId VARCHAR(20), name VARCHAR(20), score VARCHAR(10))");
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(this, "数据库驱动加载失败:" + e.getMessage());
}catch (SQLException e) {
JOptionPane.showMessageDialog(this, "数据库连接失败:" + e.getMessage());
}
}
// 关闭数据库连接
private void closeConnection() {
try {
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(this, "关闭数据库连接失败:" + e.getMessage());
}
}
public static void main(String[] args) {
new StudentTable();
}
}
这是一个Java Swing程序,用于创建一个学生信息管理系统的GUI。它包括一个表格,用于显示学生的信息(包括学号、姓名和各科成绩),以及一些按钮,用于插入、删除、编辑学生信息,保存和加载数据,以及计算平均成绩。
主要功能如下:
插入新行:用户可以在下方的文本框中输入学生的信息,然后点击"插入"按钮,将这些信息添加到表格中。
删除选中的行:用户可以选中表格中的一行,然后点击"删除"按钮,将这行数据从表格中删除。
编辑选中的行:用户可以双击表格中的一行,将这行数据的信息显示在下方的文本框中,然后用户可以编辑这些信息。
保存表格数据到数据库:用户可以点击"保存"按钮,将表格中的数据保存到数据库中。
从数据库读取表格数据:用户可以点击"读出"按钮,从数据库中读取数据,并显示在表格中。
计算平均成绩:用户可以点击"统计"按钮,计算每个科目的平均成绩,并显示在弹出的对话框中。
此外,这个程序还包括一个数据库连接功能,用于连接到MySQL数据库,并在程序启动时创建一个名为"students"的数据库表。当窗口关闭时,程序会自动关闭数据库连接。