一、人员分工
分工 | 姓名 | 任务 |
组长 | 黄羽悦 | GUIEditDelete类,GUIEditForm类,GUIOutnum类,GUISeeknum类,StudentDataGenerator 类,GUIUserLogin类,前期调查和功能设计,博客编写 |
组员 | 庄雨宸 | JDBCUtil类,Student类,GUIInput类,编码规范设计 |
组员 | 孙思雨 | StudentDAOJDBCImpl类,StudentDAOl类,GUImain类,前期调查和功能设计 |
二、选题和前期调查
1.选题
2.前期调查
(1)使用gpt进行选题研究。
(2)参考了前辈的已完成系统:
https://blog.csdn.net/weixin_63374437/article/details/128518646
https://blog.csdn.net/qq_61827376/article/details/124470583
https://blog.csdn.net/weixin_43887148/article/details/116887706
三、个人任务
1.GUImain类
1. 程序入口和主界面展示:在main方法中使用SwingUtilities的invokeLater方法调用GUImain的构造函数,创建并展示主界面。
2. 创建主界面:loadMainGUI方法负责创建主界面的各个组件,包括一个大标题和若干按钮。
3. 创建按钮:createButtons方法负责创建主界面的按钮,并绑定按钮的ActionListener为GUImain本身。
4. 设置布局:setupLayout方法使用GridBagLayout来设置主界面的布局,将各个组件放置在合适的位置。
5. 处理按钮点击事件:actionPerformed方法捕获按钮点击事件,根据按钮的标签不同来展示不同的子界面或执行不同的操作。
6. 展示子界面:handleButtonClick方法根据按钮点击的不同,展示相应的子界面,并设置子界面的大小和可见性。
7. 添加组件:addComponent方法将组件按照指定的位置和大小加入主界面的GridBagLayout布局中,以展示在界面上。
2.StudentDAOJDBCImpl类
1. 获取所有学生信息:getAllStudents方法使用SQL查询语句从数据库中获取所有学生的信息,并将每个学生的信息封装为Student对象,然后存储在一个List中返回。
2. 添加学生信息:addStudent方法使用SQL插入语句向数据库中添加新的学生信息,将传入的Student对象的各个属性作为插入的数值。
3. 更新学生信息:updateStudent方法使用SQL更新语句根据学生的id更新数据库中对应学生的信息,将传入的Student对象的各个属性作为更新的数值。
4. 删除学生信息:deleteStudent方法使用SQL删除语句从数据库中删除指定id的学生信息。
3. StudentDAOl类
一个定义了学生信息管理的接口,包括了获取、添加、更新和删除学生信息的方法。
1. 获取所有学生信息:getAllStudents方法声明了一个无参数的方法,用于获取所有学生的信息。
2. 添加学生信息:addStudent方法声明了一个接受Student对象作为参数的方法。
3. 更新学生信息:updateStudent方法声明了一个接受Student对象作为参数的方法。
4. 删除学生信息:deleteStudent方法声明了一个接受int类型id作为参数的方法。
四、所负责三个类的关键代码
1.GUImain类
package gui;
import java.awt.Button;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Label;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.SwingUtilities;
public class GUImain extends Frame implements ActionListener {
private static final long serialVersionUID = 1L;
private Button[] buttons;
private Label a1;
private Label a2;
private GridBagLayout gb;
private GridBagConstraints gbc;
public GUImain() {
GUIUserLogin userLogin = new GUIUserLogin();
userLogin.setLocationRelativeTo((Component)null);
userLogin.setVisible(true);
userLogin.setLoginListener(new 1(this));
}
private void loadMainGUI() {
this.a1 = new Label(" 欢迎使用学生成绩管理系统");
this.a1.setFont(new Font((String)null, 1, 20));
this.a2 = new Label(" ");
this.gb = new GridBagLayout();
this.setLayout(this.gb);
this.gbc = new GridBagConstraints();
this.createButtons();
this.setupLayout();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
int windowWidth = 400;
int windowHeight = 300;
int x = (screenWidth - windowWidth) / 2;
int y = (screenHeight - windowHeight) / 2;
this.setLocation(x, y);
this.setSize(windowWidth, windowHeight);
this.setVisible(true);
}
private void createButtons() {
this.buttons = new Button[5];
this.buttons[0] = new Button("输入信息");
this.buttons[1] = new Button("查询成绩");
this.buttons[2] = new Button("输出成绩");
this.buttons[3] = new Button("编辑或删除学生信息");
this.buttons[4] = new Button("退出系统");
Button[] var4;
int var3 = (var4 = this.buttons).length;
for(int var2 = 0; var2 < var3; ++var2) {
Button button = var4[var2];
button.addActionListener(this);
}
}
private void setupLayout() {
this.gbc.fill = 2;
this.addComponent(this.a1, 0, 2, 1, 4);
this.addComponent(this.a2, 4, 2, 1, 2);
this.addComponent(this.buttons[0], 6, 2, 1, 2);
this.addComponent(this.buttons[1], 8, 2, 1, 2);
this.addComponent(this.buttons[2], 10, 2, 1, 2);
this.addComponent(this.buttons[3], 12, 2, 1, 2);
this.addComponent(this.buttons[4], 14, 2, 1, 2);
}
public void actionPerformed(ActionEvent e) {
Button[] var5;
int var4 = (var5 = this.buttons).length;
for(int var3 = 0; var3 < var4; ++var3) {
Button button = var5[var3];
if (e.getActionCommand().equals(button.getLabel())) {
this.handleButtonClick(button.getLabel());
break;
}
}
}
private void handleButtonClick(String buttonLabel) {
switch (buttonLabel) {
case "编辑或删除学生信息":
GUIEditDelete editStudentFrame = new GUIEditDelete();
this.setupFrame(editStudentFrame, 600, 400);
break;
case "查询成绩":
GUISeeknum seeknumFrame = new GUISeeknum();
this.setupFrame(seeknumFrame, 600, 400);
break;
case "输入信息":
GUIInput inputFrame = new GUIInput();
this.setupFrame(inputFrame, 600, 400);
break;
case "输出成绩":
GUIOutnum outnumFrame = new GUIOutnum();
this.setupFrame(outnumFrame, 700, 500);
break;
case "退出系统":
this.setVisible(false);
this.dispose();
System.exit(0);
}
}
private void setupFrame(Frame frame, int width, int height) {
frame.setSize(width, height);
frame.setVisible(true);
}
public void addComponent(Component c, int row, int col, int nrow, int ncol) {
this.gbc.gridx = col;
this.gbc.gridy = row;
this.gbc.gridheight = ncol;
this.gbc.gridwidth = nrow;
this.gb.setConstraints(c, this.gbc);
this.add(c);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
GUImain mainGUI = new GUImain();
mainGUI.setVisible(false);
});
}
}
2. StudentDAOJDBCImpl类
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import model.Student;
public class StudentDAOJDBCImpl implements StudentDAO {
private Connection conn;
public StudentDAOJDBCImpl(Connection conn) {
this.conn = conn;
}
public List<Student> getAllStudents() {
List<Student> students = new ArrayList();
try {
Throwable var2 = null;
Object var3 = null;
try {
PreparedStatement stmt = this.conn.prepareStatement("SELECT * FROM students");
try {
ResultSet rs = stmt.executeQuery();
try {
while(rs.next()) {
Student student = new Student(rs.getInt("id"), rs.getString("name"), rs.getString("gender").charAt(0), rs.getDate("birthdate"), rs.getDouble("math"), rs.getDouble("English"), rs.getDouble("Java"), rs.getDouble("computer"), rs.getDouble("score"), rs.getDouble("average"));
students.add(student);
}
} finally {
if (rs != null) {
rs.close();
}
}
} catch (Throwable var20) {
if (var2 == null) {
var2 = var20;
} else if (var2 != var20) {
var2.addSuppressed(var20);
}
if (stmt != null) {
stmt.close();
}
throw var2;
}
if (stmt != null) {
stmt.close();
}
} catch (Throwable var21) {
if (var2 == null) {
var2 = var21;
} else if (var2 != var21) {
var2.addSuppressed(var21);
}
throw var2;
}
} catch (SQLException var22) {
var22.printStackTrace();
}
return students;
}
public void addStudent(Student student) {
try {
Throwable var2 = null;
Object var3 = null;
try {
PreparedStatement stmt = this.conn.prepareStatement("INSERT INTO students (id, name, gender, birthdate, math, English, Java, computer, score, average) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
try {
stmt.setInt(1, student.getId());
stmt.setString(2, student.getName());
stmt.setString(3, String.valueOf(student.getGender()));
stmt.setDate(4, student.getBirthdate());
stmt.setDouble(5, student.getMath());
stmt.setDouble(6, student.getEnglish());
stmt.setDouble(7, student.getJava());
stmt.setDouble(8, student.getComputer());
stmt.setDouble(9, student.getScore());
stmt.setDouble(10, student.getAverage());
stmt.executeUpdate();
} finally {
if (stmt != null) {
stmt.close();
}
}
} catch (Throwable var12) {
if (var2 == null) {
var2 = var12;
} else if (var2 != var12) {
var2.addSuppressed(var12);
}
throw var2;
}
} catch (SQLException var13) {
var13.printStackTrace();
}
}
public void updateStudent(Student student) {
try {
Throwable var2 = null;
Object var3 = null;
try {
PreparedStatement stmt = this.conn.prepareStatement("UPDATE students SET name = ?, gender = ?, birthdate = ?, math = ?, English = ?, Java = ?, computer = ?, score = ?, average = ? WHERE id = ?");
try {
stmt.setString(1, student.getName());
stmt.setString(2, String.valueOf(student.getGender()));
stmt.setDate(3, student.getBirthdate());
stmt.setDouble(4, student.getMath());
stmt.setDouble(5, student.getEnglish());
stmt.setDouble(6, student.getJava());
stmt.setDouble(7, student.getComputer());
stmt.setDouble(8, student.getScore());
stmt.setDouble(9, student.getAverage());
stmt.setInt(10, student.getId());
stmt.executeUpdate();
} finally {
if (stmt != null) {
stmt.close();
}
}
} catch (Throwable var12) {
if (var2 == null) {
var2 = var12;
} else if (var2 != var12) {
var2.addSuppressed(var12);
}
throw var2;
}
} catch (SQLException var13) {
var13.printStackTrace();
}
}
public void deleteStudent(int id) {
try {
Throwable var2 = null;
Object var3 = null;
try {
PreparedStatement stmt = this.conn.prepareStatement("DELETE FROM students WHERE id = ?");
try {
stmt.setInt(1, id);
stmt.executeUpdate();
} finally {
if (stmt != null) {
stmt.close();
}
}
} catch (Throwable var12) {
if (var2 == null) {
var2 = var12;
} else if (var2 != var12) {
var2.addSuppressed(var12);
}
throw var2;
}
} catch (SQLException var13) {
var13.printStackTrace();
}
}
}
3. StudentDAOl类
package dao;
import java.util.List;
import model.Student;
public interface StudentDAO {
List<Student> getAllStudents();
void addStudent(Student var1);
void updateStudent(Student var1);
void deleteStudent(int var1);
}
(注:设计中有队长黄羽悦和使用chatgpt帮助解决困难)