一、项目简介
功能描述:
基于Java Swing+MySQL的学生基本信息管理系统,支持对学院、班级、学生信息的增删改查。
参考git地址或博客地址:
https://www.bilibili.com/video/BV1bQ4y1f7At
项目亮点:
在原始功能基础上,增加了学院和班级功能,在Student表中添加外键关联到Secondary表及Class表。
个人负责任务:
负责此项目的所有工作,包括GUI界面的构建,MySQL数据库的创建、登录功能的实现、增删改查相关功能的实现。
源码地址:
https://github.com/KillerNex/StudentSystem
二、功能架构图
三、个人任务简述
1. 完成的任务与功能:
序号 | 完成功能与任务 | 描述 |
1 | 登录功能 | 在UserDao中使用select语句对User表进行检索,查询用户名、密码是否匹配 |
2 | 添加功能 | 使用insert into语句,向表中添加数据 |
3 | 查询功能 | 使用select语句,对表中数据模糊查询 |
4 | 修改功能 | 使用update语句,对表中数据进行更新 |
5 | 删除功能 | 使用delete语句,对表中数据进行删除 |
6 | 数据库读写编码部分 | 使用了DAO模式。 |
7 | 数据库设计 | 采用MySQL数据库。表设计符合第三范式。 |
2. Git提交记录截图:
四、本人负责功能详解
1.*面向对象设计
2. *学院/班级添加功能:
监听器调用添加事件处理方法,添加事件处理方法调用Dao类中的add方法向数据库写入学院/班级数据。
//添加事件处理
private void secondaryAddActionPerformed() {
String depart_name = this.secondary_nameTxt.getText();
String depart_desc = this.secondary_descTxt.getText();
if (StringUtil.isEmpty(depart_name)) {
JOptionPane.showMessageDialog(null, "部门名称不能为空!");
return;
}
if (StringUtil.isEmpty(depart_desc)) {
JOptionPane.showMessageDialog(null, "部门描述不能为空!");
return;
}
Secondary secondary = new Secondary(depart_name, depart_desc);
Connection con = null;
try {
con = dbUtil.getCon();
int addnum = secondaryDao.add(con, secondary);
if (addnum == 1) {
JOptionPane.showMessageDialog(null, "添加成功!");
resetNull();
} else {
JOptionPane.showMessageDialog(null, "添加失败!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
dbUtil.closeCon(con);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
3.*学生信息修改
读取表单中的数据并且调用StudentDao中的更新数据方法,对学生信息进行修改。
//修改事件处理
private void studentUpdateActionPerformed(ActionEvent e) {
String name = this.nameTxt.getText();
String studentId = this.studentIdTxt.getText();
String phone_num = this.phone_numTxt.getText();
String dormitory_num = this.dormitory_numTxt.getText();
String address = this.addressTxt.getText();
int id = 0;
//获取数据库中ID
Student selectedStu = new Student(studentId, name);
Connection con = null;
try {
con = dbUtil.getCon();
ResultSet rs = studentDao.list(con, selectedStu);
while (rs.next()) {
id = rs.getInt("id");
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
dbUtil.closeCon(con);
} catch (SQLException ex) {
ex.printStackTrace();
}
}
String politics_status = "";
if (this.politics_statusJCB.getSelectedIndex() == 0) {
politics_status = "群众";
} else if (this.politics_statusJCB.getSelectedIndex() == 1) {
politics_status = "团员";
} else {
politics_status = "党员";
}
String sex = "";
if (this.maleJRB.isSelected()) {
sex = "男";
} else {
sex = "女";
}
String year = String.valueOf(this.yearJCB.getSelectedIndex() + 2000);
String month = String.valueOf(this.monthJCB.getSelectedIndex() + 1);
String day = String.valueOf(this.dayJCB.getSelectedIndex() + 1);
Secondary secondary = (Secondary) this.secondaryJCB.getSelectedItem();
int secondaryId = secondary.getId();
StudentClass studentClass = (StudentClass) this.studentClassJCB.getSelectedItem();
int classId = studentClass.getId();
if (StringUtil.isEmpty(name)) {
JOptionPane.showMessageDialog(null, "姓名不能为空!");
return;
}
if (StringUtil.isEmpty(studentId)) {
JOptionPane.showMessageDialog(null, "学号不能为空!");
return;
}
if (StringUtil.isEmpty(phone_num)) {
JOptionPane.showMessageDialog(null, "联系电话不能为空!");
return;
}
if (StringUtil.isEmpty(dormitory_num)) {
JOptionPane.showMessageDialog(null, "宿舍号不能为空!");
return;
}
if (StringUtil.isEmpty(address)) {
JOptionPane.showMessageDialog(null, "家庭住址不能为空!");
return;
}
Student student = new Student(id, studentId, name, sex, year, month, day, politics_status, address, phone_num, dormitory_num, secondaryId, classId);
con = null;
try {
con = dbUtil.getCon();
int addnum = studentDao.update(con, student);
if (addnum == 1) {
JOptionPane.showMessageDialog(null, "学生信息保存成功!");
this.fillTable(new Student());
this.s_secondaryJCB.setSelectedIndex(0);
this.s_classJCB.setSelectedIndex(0);
restNull();
} else {
JOptionPane.showMessageDialog(null, "学生信息保存失败!");
}
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "学生信息保存失败!");
} finally {
try {
dbUtil.closeCon(con);
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
4.*数据库读写部分
SecondaryDao部分
public class SecondaryDao {
//学院添加
public int add(Connection con, Secondary secondary) throws Exception {
String sql = "insert into secondary values(null,?,?)";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, secondary.getSecondary_name());
pstmt.setString(2, secondary.getSecondary_desc());
return pstmt.executeUpdate();
}
//查询学院
public ResultSet list(Connection con, Secondary secondary) throws Exception {
StringBuilder strb = new StringBuilder("select*from secondary");
if (StringUtil.isNotEmpty(secondary.getSecondary_name())) {
strb.append(" and secondary_name like '%" + secondary.getSecondary_name() + "%'");
}
PreparedStatement pstmt = con.prepareStatement(strb.toString().replace("and", "where"));
return pstmt.executeQuery();
}
//删除学院
public int delete(Connection con, String id) throws Exception {
String sql = "delete from secondary where id=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, id);
return pstmt.executeUpdate();
}
//修改学院
public int update(Connection con, Secondary secondary) throws Exception {
String sql = "update secondary set secondary_name=? ,secondary_desc=? where id =?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, secondary.getSecondary_name());
pstmt.setString(2, secondary.getSecondary_desc());
pstmt.setInt(3, secondary.getId());
return pstmt.executeUpdate();
}
}
StudentClassDao部分
public class StudentClassDao {
//班级添加
public int add(Connection con, StudentClass studentClass) throws Exception {
String sql = "Insert into studentClass values(null,?,?)";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, studentClass.getStudentClass_name());
pstmt.setString(2, studentClass.getStudentClass_desc());
return pstmt.executeUpdate();
}
//查询班级
public ResultSet list(Connection con, StudentClass studentClass) throws Exception {
StringBuilder stub = new StringBuilder("select * from studentClass");
if (StringUtil.isNotEmpty(studentClass.getStudentClass_name())) {
stub.append(" and studentClass_name like '%" + studentClass.getStudentClass_name() + "%'");
}
PreparedStatement pstmt = con.prepareStatement(stub.toString().replace("and", "where"));
return pstmt.executeQuery();
}
//删除学院
public int delete(Connection con, String id) throws Exception {
String sql = "delete from studentclass where id=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, id);
return pstmt.executeUpdate();
}
//修改班级
public int update(Connection con, StudentClass studentClass) throws Exception {
String sql = "update studentclass set studentclass_name=? ,studentclass_desc=? where id =?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, studentClass.getStudentClass_name());
pstmt.setString(2, studentClass.getStudentClass_desc());
pstmt.setInt(3, studentClass.getId());
return pstmt.executeUpdate();
}
}
StudentDao部分
public class StudentDao {
//学生添加
public int add(Connection con, Student student) throws Exception {
String sql = "Insert into student values(null,?,?,?,?,?,?,?,?,?,?,?,?)";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, student.getStudentId());
pstmt.setString(2, student.getName());
pstmt.setString(3, student.getSex());
pstmt.setString(4, student.getYear());
pstmt.setString(5, student.getMonth());
pstmt.setString(6, student.getDay());
pstmt.setString(7, student.getPolitics_status());
pstmt.setString(8, student.getAddress());
pstmt.setString(9, student.getPhone_num());
pstmt.setString(10, student.getDormitory_num());
pstmt.setInt(11, student.getSecondaryId());
pstmt.setInt(12, student.getClassId());
return pstmt.executeUpdate();
}
//学生查询
public ResultSet list(Connection con, Student student) throws Exception {
StringBuilder strb = new StringBuilder("select *from student stu,secondary scd,studentclass stc where stu.secondaryId=scd.id AND stu.classId=stc.id");
if (StringUtil.isNotEmpty(student.getName())) {
strb.append(" and stu.name like '%" + student.getName() + "%'");
}
if (StringUtil.isNotEmpty(student.getStudentId())) {
strb.append(" and stu.studentId like '%" + student.getStudentId() + "%'");
}
if (student.getSecondaryId() != null && student.getSecondaryId() != -1) {
strb.append(" and stu.secondaryId =" + student.getSecondaryId());
}
if (student.getClassId() != null && student.getClassId() != -1) {
strb.append(" and stu.classId =" + student.getClassId());
}
PreparedStatement pstmt = con.prepareStatement(strb.toString());
return pstmt.executeQuery();
}
//学生修改
public int update(Connection con, Student student) throws Exception {
String sql = "update student set studentId=? ,name=? ,sex=? ,year=? ,month=? ,day=? ,politics_status=? ,address=? ,phone_num=? ,dormitory_num=? ,secondaryId=? ,classId=? where id =?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, student.getStudentId());
pstmt.setString(2, student.getName());
pstmt.setString(3, student.getSex());
pstmt.setString(4, student.getYear());
pstmt.setString(5, student.getMonth());
pstmt.setString(6, student.getDay());
pstmt.setString(7, student.getPolitics_status());
pstmt.setString(8, student.getAddress());
pstmt.setString(9, student.getPhone_num());
pstmt.setString(10, student.getDormitory_num());
pstmt.setInt(11, student.getSecondaryId());
pstmt.setInt(12, student.getClassId());
pstmt.setInt(13, student.getId());
return pstmt.executeUpdate();
}
//删除学生
public int delete(Connection con, String id) throws Exception {
String sql = "delete from student where id=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, id);
return pstmt.executeUpdate();
}
}
UserDao部分
public class UserDao {
public User login(Connection con, User user) throws Exception {
User resultUser = null;
String sql = "select*from user where userName=? and password=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, user.getUserName());
pstmt.setString(2, user.getPassword());
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
resultUser = new User();
resultUser.setId(rs.getInt("id"));
resultUser.setUserName(rs.getString("userName"));
resultUser.setPassword(rs.getString("password"));
}
return resultUser;
}
//用户注册
public int addUser(Connection con, User user) throws Exception {
String sql = "insert into user values(null,?,?)";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, user.getUserName());
pstmt.setString(2, user.getPassword());
return pstmt.executeUpdate();
}
}
5.数据库设计