swing编写学生管理系统的源代码
时间: 2023-09-02 17:10:01 浏览: 117
学生管理系统的源代码
下面是一个简单的学生管理系统的Swing源代码,包含学生表和成绩表的添加、删除、修改和查询功能:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.sql.*;
public class StudentManagementSystem extends JFrame implements ActionListener {
// 数据库连接
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
// 界面组件
JLabel nameLabel = new JLabel("姓名:");
JLabel genderLabel = new JLabel("性别:");
JLabel ageLabel = new JLabel("年龄:");
JLabel addressLabel = new JLabel("地址:");
JTextField nameField = new JTextField(10);
JComboBox<String> genderBox = new JComboBox<String>(new String[] { "男", "女" });
JSpinner ageSpinner = new JSpinner(new SpinnerNumberModel(18, 0, 100, 1));
JTextField addressField = new JTextField(20);
JButton addButton = new JButton("添加");
JButton deleteButton = new JButton("删除");
JButton modifyButton = new JButton("修改");
JButton searchButton = new JButton("查询");
DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);
public StudentManagementSystem() {
// 初始化界面
setTitle("学生管理系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// 添加组件到界面
JPanel inputPanel = new JPanel(new GridLayout(4, 2));
inputPanel.add(nameLabel);
inputPanel.add(nameField);
inputPanel.add(genderLabel);
inputPanel.add(genderBox);
inputPanel.add(ageLabel);
inputPanel.add(ageSpinner);
inputPanel.add(addressLabel);
inputPanel.add(addressField);
add(inputPanel, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(addButton);
buttonPanel.add(deleteButton);
buttonPanel.add(modifyButton);
buttonPanel.add(searchButton);
add(buttonPanel, BorderLayout.CENTER);
model.addColumn("编号");
model.addColumn("姓名");
model.addColumn("性别");
model.addColumn("年龄");
model.addColumn("地址");
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane, BorderLayout.SOUTH);
// 添加事件监听器
addButton.addActionListener(this);
deleteButton.addActionListener(this);
modifyButton.addActionListener(this);
searchButton.addActionListener(this);
// 连接数据库并初始化表格
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "password");
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM student");
while (rs.next()) {
Object[] row = { rs.getInt("id"), rs.getString("name"), rs.getString("gender"), rs.getInt("age"),
rs.getString("address") };
model.addRow(row);
}
} catch (Exception ex) {
ex.printStackTrace();
}
// 显示界面
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
// 添加学生信息
String name = nameField.getText();
String gender = (String) genderBox.getSelectedItem();
int age = (int) ageSpinner.getValue();
String address = addressField.getText();
try {
stmt.executeUpdate("INSERT INTO student (name, gender, age, address) VALUES ('" + name + "', '"
+ gender + "', " + age + ", '" + address + "')");
Object[] row = { getNewId(), name, gender, age, address };
model.addRow(row);
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (e.getSource() == deleteButton) {
// 删除学生信息
int row = table.getSelectedRow();
int id = (int) model.getValueAt(row, 0);
try {
stmt.executeUpdate("DELETE FROM student WHERE id=" + id);
model.removeRow(row);
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (e.getSource() == modifyButton) {
// 修改学生信息
int row = table.getSelectedRow();
int id = (int) model.getValueAt(row, 0);
String name = (String) model.getValueAt(row, 1);
String gender = (String) model.getValueAt(row, 2);
int age = (int) model.getValueAt(row, 3);
String address = (String) model.getValueAt(row, 4);
try {
stmt.executeUpdate("UPDATE student SET name='" + name + "', gender='" + gender + "', age=" + age
+ ", address='" + address + "' WHERE id=" + id);
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (e.getSource() == searchButton) {
// 查询学生信息
String name = nameField.getText();
String gender = (String) genderBox.getSelectedItem();
int age = (int) ageSpinner.getValue();
String address = addressField.getText();
try {
rs = stmt.executeQuery("SELECT * FROM student WHERE name LIKE '%" + name + "%' AND gender='" + gender
+ "' AND age=" + age + " AND address LIKE '%" + address + "%'");
model.setRowCount(0);
while (rs.next()) {
Object[] row = { rs.getInt("id"), rs.getString("name"), rs.getString("gender"), rs.getInt("age"),
rs.getString("address") };
model.addRow(row);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public int getNewId() {
// 获取新的学生编号
int id = 0;
try {
rs = stmt.executeQuery("SELECT MAX(id) FROM student");
if (rs.next()) {
id = rs.getInt(1);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return id + 1;
}
public static void main(String[] args) {
new StudentManagementSystem();
}
}
```
这个源代码创建了一个包含学生信息添加、删除、修改和查询功能的Swing窗口,使用了MySQL数据库来存储数据。界面组件包括文本框、下拉框、按钮和表格,事件监听器处理按钮单击事件。可以根据需要修改源代码来实现更复杂的功能。
阅读全文