生成一个Java数据库学生成绩管理系统课程设计代码
时间: 2023-10-05 15:09:59 浏览: 123
学生成绩管理系统数据库课程设计
5星 · 资源好评率100%
好的,请稍等,我将为您生成一个简单的Java数据库学生成绩管理系统的代码。
```java
import java.sql.*;
import javax.swing.*;
import java.awt.event.*;
public class StudentGradeManagementSystem extends JFrame implements ActionListener {
private JLabel titleLabel, nameLabel, idLabel, genderLabel, ageLabel, classLabel, subjectLabel, scoreLabel;
private JTextField nameField, idField, genderField, ageField, classField, subjectField, scoreField;
private JButton addButton, searchButton, modifyButton, deleteButton, backupButton, restoreButton;
private JTextArea resultArea;
private Connection conn;
private Statement stmt;
public StudentGradeManagementSystem() {
super("学生成绩管理系统");
initUI();
initDB();
}
private void initUI() {
titleLabel = new JLabel("学生成绩管理系统", JLabel.CENTER);
nameLabel = new JLabel("学生姓名:");
idLabel = new JLabel("学号:");
genderLabel = new JLabel("性别:");
ageLabel = new JLabel("年龄:");
classLabel = new JLabel("班级:");
subjectLabel = new JLabel("考试科目:");
scoreLabel = new JLabel("成绩:");
nameField = new JTextField(10);
idField = new JTextField(10);
genderField = new JTextField(10);
ageField = new JTextField(10);
classField = new JTextField(10);
subjectField = new JTextField(10);
scoreField = new JTextField(10);
addButton = new JButton("添加");
searchButton = new JButton("查询");
modifyButton = new JButton("修改");
deleteButton = new JButton("删除");
backupButton = new JButton("备份");
restoreButton = new JButton("恢复");
resultArea = new JTextArea(10, 30);
resultArea.setEditable(false);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(titleLabel);
panel.add(new JLabel(" "));
panel.add(new JLabel("学生信息:"));
panel.add(new JLabel(" "));
JPanel infoPanel = new JPanel();
infoPanel.add(nameLabel);
infoPanel.add(nameField);
infoPanel.add(idLabel);
infoPanel.add(idField);
infoPanel.add(genderLabel);
infoPanel.add(genderField);
infoPanel.add(ageLabel);
infoPanel.add(ageField);
infoPanel.add(classLabel);
infoPanel.add(classField);
panel.add(infoPanel);
panel.add(new JLabel(" "));
panel.add(new JLabel("学生成绩:"));
panel.add(new JLabel(" "));
JPanel scorePanel = new JPanel();
scorePanel.add(subjectLabel);
scorePanel.add(subjectField);
scorePanel.add(scoreLabel);
scorePanel.add(scoreField);
panel.add(scorePanel);
panel.add(new JLabel(" "));
JPanel buttonPanel = new JPanel();
buttonPanel.add(addButton);
buttonPanel.add(searchButton);
buttonPanel.add(modifyButton);
buttonPanel.add(deleteButton);
buttonPanel.add(backupButton);
buttonPanel.add(restoreButton);
panel.add(buttonPanel);
panel.add(new JLabel(" "));
panel.add(new JScrollPane(resultArea));
addButton.addActionListener(this);
searchButton.addActionListener(this);
modifyButton.addActionListener(this);
deleteButton.addActionListener(this);
backupButton.addActionListener(this);
restoreButton.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(panel);
pack();
setVisible(true);
}
private void initDB() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/student_grade", "root", "");
stmt = conn.createStatement();
stmt.executeUpdate("create table if not exists student(name varchar(20), id varchar(20) primary key, gender varchar(10), age int, class varchar(20))");
stmt.executeUpdate("create table if not exists grade(id varchar(20), subject varchar(20), score int, foreign key(id) references student(id))");
} catch(Exception e) {
e.printStackTrace();
}
}
private void addStudent() {
String name = nameField.getText();
String id = idField.getText();
String gender = genderField.getText();
int age = Integer.parseInt(ageField.getText());
String class = classField.getText();
try {
stmt.executeUpdate("insert into student values('" + name + "', '" + id + "', '" + gender + "', " + age + ", '" + class + "')");
resultArea.setText("添加学生成功!");
} catch(Exception e) {
e.printStackTrace();
resultArea.setText("添加学生失败!");
}
}
private void searchStudent() {
String id = idField.getText();
try {
ResultSet rs = stmt.executeQuery("select * from student where id = '" + id + "'");
if(rs.next()) {
nameField.setText(rs.getString("name"));
genderField.setText(rs.getString("gender"));
ageField.setText(rs.getInt("age") + "");
classField.setText(rs.getString("class"));
resultArea.setText("查询学生成功!");
} else {
resultArea.setText("查询学生失败!");
}
} catch(Exception e) {
e.printStackTrace();
}
}
private void modifyStudent() {
String name = nameField.getText();
String id = idField.getText();
String gender = genderField.getText();
int age = Integer.parseInt(ageField.getText());
String class = classField.getText();
try {
stmt.executeUpdate("update student set name = '" + name + "', gender = '" + gender + "', age = " + age + ", class = '" + class + "' where id = '" + id + "'");
resultArea.setText("修改学生成功!");
} catch(Exception e) {
e.printStackTrace();
resultArea.setText("修改学生失败!");
}
}
private void deleteStudent() {
String id = idField.getText();
try {
stmt.executeUpdate("delete from student where id = '" + id + "'");
stmt.executeUpdate("delete from grade where id = '" + id + "'");
resultArea.setText("删除学生成功!");
} catch(Exception e) {
e.printStackTrace();
resultArea.setText("删除学生失败!");
}
}
private void backupDB() {
try {
stmt.executeUpdate("backup to 'student_grade_backup.sql'");
resultArea.setText("备份数据库成功!");
} catch(Exception e) {
e.printStackTrace();
resultArea.setText("备份数据库失败!");
}
}
private void restoreDB() {
try {
stmt.executeUpdate("restore from 'student_grade_backup.sql'");
resultArea.setText("恢复数据库成功!");
} catch(Exception e) {
e.printStackTrace();
resultArea.setText("恢复数据库失败!");
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == addButton) {
addStudent();
} else if(e.getSource() == searchButton) {
searchStudent();
} else if(e.getSource() == modifyButton) {
modifyStudent();
} else if(e.getSource() == deleteButton) {
deleteStudent();
} else if(e.getSource() == backupButton) {
backupDB();
} else if(e.getSource() == restoreButton) {
restoreDB();
}
}
public static void main(String[] args) {
new StudentGradeManagementSystem();
}
}
```
这是一个简单的学生成绩管理系统,包括学生信息和成绩的录入、查询、修改和删除功能,以及数据库备份和恢复功能。需要注意的是,这只是一个简单的示例,还有很多需要完善的地方。
阅读全文