教务学生选课系统的实现
【摘要】 任务: 教务选课系统的实现一、数据库/*Navicat MySQL Data TransferSource Server : DBCON1Source Server Version : 50722Source Host : localhost:3306Source Database : db2Target Server Type : MYSQLTarget Server Ve...
任务:
教务选课系统的实现
一、数据库
/*
Navicat MySQL Data Transfer
Source Server : DBCON1
Source Server Version : 50722
Source Host : localhost:3306
Source Database : db2
Target Server Type : MYSQL
Target Server Version : 50722
File Encoding : 65001
Date: 2022-03-25 21:59:08
*/
SET FOREIGN_KEY_CHECKS=0;
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
`cno` tinyint(4) NOT NULL AUTO_INCREMENT,
`cname` varchar(50) NOT NULL COMMENT '课程名',
`ccredit` tinyint(4) DEFAULT NULL COMMENT '学分',
`cpno` tinyint(4) DEFAULT NULL COMMENT '先行课',
PRIMARY KEY (`cno`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4;
-DROP TABLE IF EXISTS `depart`;
CREATE TABLE `depart` (
`dno` tinyint(3) unsigned NOT NULL AUTO_INCREMENT COMMENT '系部编号',
`dname` varchar(50) DEFAULT NULL COMMENT '每部名称',
PRIMARY KEY (`dno`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8mb4;
-DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`sno` char(10) NOT NULL,
`sname` varchar(50) NOT NULL COMMENT '姓名',
`sage` tinyint(3) unsigned DEFAULT NULL,
`gender` enum('男','女') DEFAULT '男',
`dno` tinyint(3) unsigned DEFAULT NULL,
PRIMARY KEY (`sno`),
KEY `stu_dep` (`dno`),
CONSTRAINT `stu_dep` FOREIGN KEY (`dno`) REFERENCES `depart` (`dno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
DROP TABLE IF EXISTS `stu_course`;
CREATE TABLE `stu_course` (
`sno` char(10) NOT NULL,
`cno` tinyint(4) NOT NULL,
`grade` tinyint(3) unsigned DEFAULT NULL,
PRIMARY KEY (`sno`,`cno`),
KEY `cno` (`cno`),
CONSTRAINT `stu_course_ibfk_1` FOREIGN KEY (`sno`) REFERENCES `student` (`sno`),
CONSTRAINT `stu_course_ibfk_2` FOREIGN KEY (`cno`) REFERENCES `course` (`cno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
二、实现的主要功能
1.实现学生登录注册
2.学生查看自己的选课及成绩功能
3.统计每门课的平均成绩
4.添加学生成绩
5.添加系部信息
6.添加学生信息
7.添加学生选课信息
三、创建实体类
1.Course.java — 课程实体类
public class Course {
private int cno;
private String cname;
private int ccredit;
private int cpno;
public Course(int cno, String cname, int ccredit, int cpno) {
this.cno = cno;
this.cname = cname;
this.ccredit = ccredit;
this.cpno = cpno;
}
// Getters and Setters
public int getCno() {
return cno;
}
public void setCno(int cno) {
this.cno = cno;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public int getCcredit() {
return ccredit;
}
public void setCcredit(int ccredit) {
this.ccredit = ccredit;
}
public int getCpno() {
return cpno;
}
public void setCpno(int cpno) {
this.cpno = cpno;
}
}
2.Course.java — 课程实体类
public class Course {
private int cno;
private String cname;
private int ccredit;
private int cpno;
public Course(int cno, String cname, int ccredit, int cpno) {
this.cno = cno;
this.cname = cname;
this.ccredit = ccredit;
this.cpno = cpno;
}
// Getters and Setters
public int getCno() {
return cno;
}
public void setCno(int cno) {
this.cno = cno;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public int getCcredit() {
return ccredit;
}
public void setCcredit(int ccredit) {
this.ccredit = ccredit;
}
public int getCpno() {
return cpno;
}
public void setCpno(int cpno) {
this.cpno = cpno;
}
}
3.Department.java — 部门实体类
javaCopy Code
public class Department {
private int dno;
private String dname;
public Department(int dno, String dname) {
this.dno = dno;
this.dname = dname;
}
// Getters and Setters
public int getDno() {
return dno;
}
public void setDno(int dno) {
this.dno = dno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
}
4.Student.java — 学生实体类
javaCopy Code
public class Student {
private String sno;
private String sname;
private int sage;
private String gender;
private int dno;
public Student(String sno, String sname, int sage, String gender, int dno) {
this.sno = sno;
this.sname = sname;
this.sage = sage;
this.gender = gender;
this.dno = dno;
}
// Getters and Setters
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getSage() {
return sage;
}
public void setSage(int sage) {
this.sage = sage;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getDno() {
return dno;
}
public void setDno(int dno) {
this.dno = dno;
}
}
5.StuCourse.java — 学生选课实体类
javaCopy Code
public class StuCourse {
private String sno;
private int cno;
private int grade;
public StuCourse(String sno, int cno, int grade) {
this.sno = sno;
this.cno = cno;
this.grade = grade;
}
// Getters and Setters
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public int getCno() {
return cno;
}
public void setCno(int cno) {
this.cno = cno;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
}
四、数据库连接工具类
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBUtils {
private static final String URL = "jdbc:mysql://localhost:3306/db2";
private static final String USER = "root";
private static final String PASSWORD = "root"; // 根据实际情况修改
// 获取数据库连接
public static Connection getConnection() throws SQLException {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
return DriverManager.getConnection(URL, USER, PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new SQLException("数据库驱动加载失败");
}
}
}
五、业务类
1. 学生管理业务类 (StudentService)
import java.sql.*;
public class StudentService {
// 学生注册
public boolean registerStudent(Student student) {
String sql = "INSERT INTO student (sno, sname, sage, gender, dno) VALUES (?, ?, ?, ?, ?)";
//说明:以下这种写法,在该语句块结束时,所有在括号内声明的资源会自动关闭。
try (Connection conn = DBUtils.getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, student.getSno());
stmt.setString(2, student.getSname());
stmt.setInt(3, student.getSage());
stmt.setString(4, student.getGender());
stmt.setInt(5, student.getDno());
int rows = stmt.executeUpdate();
return rows > 0;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
// 学生登录
public boolean loginStudent(String sno) {
String sql = "SELECT * FROM student WHERE sno = ?";
try (Connection conn = DBUtils.getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, sno);
ResultSet rs = stmt.executeQuery();
return rs.next(); // 如果能找到记录,说明登录成功
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
// 查看学生选课和成绩
public void viewCoursesAndGrades(String sno) {
String sql = "SELECT c.cname, sc.grade FROM stu_course sc JOIN course c ON sc.cno = c.cno WHERE sc.sno = ?";
try (Connection conn = DBUtils.getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, sno);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
String courseName = rs.getString("cname");
int grade = rs.getInt("grade");
System.out.println(courseName + " - 成绩: " + grade);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2. 课程管理业务类 (CourseService)
java
import java.sql.*;
public class CourseService {
// 统计每门课程的平均成绩
public void calculateAverageGrade(int cno) {
String sql = "SELECT AVG(grade) FROM stu_course WHERE cno = ?";
try (Connection conn = DBUtils.getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, cno);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
double average = rs.getDouble(1);
System.out.println("课程 " + cno + " 的平均成绩是: " + average);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
// 添加成绩
public boolean addGrade(String sno, int cno, int grade) {
String sql = "INSERT INTO stu_course (sno, cno, grade) VALUES (?, ?, ?)";
try (Connection conn = DBUtils.getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, sno);
stmt.setInt(2, cno);
stmt.setInt(3, grade);
int rows = stmt.executeUpdate();
return rows > 0;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
}
3.系部管理业务类 (DepartmentService)
java
import java.sql.*;
public class DepartmentService {
// 添加系部信息
public boolean addDepartment(int dno, String dname) {
String sql = "INSERT INTO depart (dno, dname) VALUES (?, ?)";
try (Connection conn = DBUtils.getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, dno);
stmt.setString(2, dname);
int rows = stmt.executeUpdate();
return rows > 0;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
}
六、主类
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StudentService studentService = new StudentService();
CourseService courseService = new CourseService();
DepartmentService departmentService = new DepartmentService();
while (true) {
System.out.println("欢迎使用学生管理系统!");
System.out.println("1. 注册");
System.out.println("2. 登录");
System.out.println("3. 查看选课及成绩");
System.out.println("4. 统计课程平均成绩");
System.out.println("5. 添加成绩");
System.out.println("6. 添加系部信息");
System.out.println("7. 退出");
int choice = scanner.nextInt();
scanner.nextLine(); // 清除换行符
switch (choice) {
case 1: {
// 注册
System.out.println("请输入学号:");
String sno = scanner.nextLine();
System.out.println("请输入姓名:");
String sname = scanner.nextLine();
System.out.println("请输入年龄:");
int sage = scanner.nextInt();
System.out.println("请输入性别(男/女):");
String gender = scanner.next();
System.out.println("请输入系部编号:");
int dno = scanner.nextInt();
Student student = new Student(sno, sname, sage, gender, dno);
if (studentService.registerStudent(student)) {
System.out.println("注册成功!");
} else {
System.out.println("注册失败!");
}
break;
}
case 2: {
// 登录
System.out.println("请输入学号:");
String sno = scanner.nextLine();
if (studentService.loginStudent(sno)) {
System.out.println("登录成功!");
} else {
System.out.println("学号不存在!");
}
break;
}
case 3: {
// 查看选课及成绩
System.out.println("请输入学号:");
String sno = scanner.nextLine();
studentService.viewCoursesAndGrades(sno);
break;
}
case 4: {
// 统计课程平均成绩
System.out.println("请输入课程编号:");
int cno = scanner.nextInt();
courseService.calculateAverageGrade(cno);
break;
}
case 5: {
// 添加成绩
System.out.println("请输入学号:");
String sno = scanner.nextLine();
System.out.println("请输入课程编号:");
int cno = scanner.nextInt();
System.out.println("请输入成绩:");
int grade = scanner.nextInt();
if (courseService.addGrade(sno, cno, grade)) {
System.out.println("成绩添加成功!");
} else {
System.out.println("成绩添加失败!");
}
break;
}
case 6: {
// 添加系部信息
System.out.println("请输入系部编号:");
int dno = scanner.nextInt();
scanner.nextLine(); // 清除换行符
System.out.println("请输入系部名称:");
String dname = scanner.nextLine();
if (departmentService.addDepartment(dno, dname)) {
System.out.println("系部添加成功!");
} else {
System.out.println("系部添加失败!");
}
break;
}
case 7: {
// 退出
System.out.println("感谢使用学生管理系统,再见!");
scanner.close();
return;
}
default: {
System.out.println("无效的选项,请重新选择!");
break;
}
}
}
}
}
总结:
教务管理系统是高校或教育机构的重要管理工具,负责整合教学管理、学生管理、教务信息发布等关键职能。系统运行效率及信息准确度直接关系到学校教育工作的整体水平,和学生关联度比较密切,是实践项目中比较实用的案例。
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)