1、需求分析
1.1、需求分析概述
需求分析是我们在软件开发中的重要环节,是软件开发的第一步也是最基础的环节,这将决定我们所实现的目标以及系统的各个组成部分、各部分的任务职能、以及使用到的数据结构、各个部门之间的组成关系和数据流程,为我们的系统设计打下基础。
1.2、学生管理系统需求分析
1、实现数据的录入(INSERT)、删除(DELETE)、修改(UPDATE)、查找(SELECT)。
2、根据学生姓名查询信息
3、更新学生的班级
4、根据班级编号查询学生信息
5、根据学生id删除学生信息
6、插入学生分数记录
7、修改学生成绩
8、删除学生成绩
9、删除成绩
10、根据班级id查询所有学生的成绩
11、根据班级名称查询班级信息
2、设计思路
1、将数据库中的Student、Classes、Course、Score四张表封装为实体类
2、设计三种Classes、Score、Student对象的工具类,工具类的主要工作就是与数据库进行交互,实现录入(INSERT)、删除(DELETE)、修改(UPDATE)、查找(SELECT)四种操作。
3、主函数实现界面的修饰,根据我们的需求实现对某个对象的具体操作,界面的功能主要是通过调用我们所实现的工具类进行完成操作,所以说,界面能实现哪些功能取决于我们能够在工具类中所能实现哪些功能
2.1、流程图
根据上述系统功能的分析,按照结构化程序设计的要求,得到系统的结构图,如下图所示
3、详细设计
3.1、设计四张表的实体类
Classes表
public class Classes {
private int id;
private String name;
private String desc;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
Course表
public class Course {
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private int id;
private String name;
}
Score表
import java.math.BigDecimal;
public class Score {
private int id;
private BigDecimal score;
private int studentId;
private int courseId;
private Student student;
private Course course;
public Student getStudent() {
return student;
}
@Override
public String toString() {
return "Score{" +
"id=" + id +
", score=" + score +
", studentId=" + studentId +
", courseId=" + courseId +
'}';
}
public void setStudent(Student student) {
this.student = student;
}
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public BigDecimal getScore() {
return score;
}
public void setScore(BigDecimal score) {
this.score = score;
}
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public int getCourseId() {
return courseId;
}
public void setCourseId(int courseId) {
this.courseId = courseId;
}
}
Student表
public class Student {
private int id;
private int sn;
private String name;
private String mail;
private int classId;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getSn() {
return sn;
}
public void setSn(int sn) {
this.sn = sn;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public int getClassId() {
return classId;
}
public void setClassId(int classId) {
this.classId = classId;
}
}
上述四张表在我之前的博客都有创建过,想要了解的可以去看看这所用到的四张表https://blog.csdn.net/m0_53714343/article/details/13105454
3.2、连接数据库的工具类
调用连接数据库工具类,就可以连接到自己的数据库,调用连接数据库之后如何释放资源,关闭连接等方法。
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBUtil {
//定义要返回的对象
private static DataSource dataSource;
//关于数据源的配置项
private static final String URL="jdbc:mysql://localhost:3306/java_4?characterEncoding=utf8&useSSL=false";
private static final String USER="root";
private static final String PassWord="qwb251635..";
//在使用之前要初始化dataSource,写在static静态代码块中让类在加载JVM的时候就完成初始化操作
static{
//设置配置参数
MysqlDataSource mysqlDataSource=new MysqlDataSource();
mysqlDataSource.setURL(URL);
mysqlDataSource.setUser(USER);
mysqlDataSource.setPassword(PassWord);
dataSource=mysqlDataSource;
}
//不允许外部创建这个类的实例,也就是不允许new这个对象
private DBUtil(){
};
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
public static void close(ResultSet resultSet, Statement statement, Connection connection){
if (resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if(statement!=null){
try {
statement.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
3.3、实现三种与数据库交互的工具类
ClassesDao工具类
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class ClassesDao {
/**
* 根据班级名称查询班级信息
* @param className 班级名称
* @return 班级信息
*/
public Classes selectByName(String className){
Connection connection=null;
PreparedStatement statement=null;
ResultSet resultSet=null;
try {
//获取连接对象
connection= DBUtil.getConnection();
//构建sql语句
String sql="select id,name,`desc` from classes where name=?";
//获取预处理对象
statement = connection.prepareStatement(sql);
//将占位符替换为真实值
statement.setString(1, className);
//执行sql并获取返回结果
resultSet=statement.executeQuery();
//处理返回结果,构造最终返回对象
Classes result=null;
if(resultSet.next()){
if(result==null){
result=new Classes();
}
//设置属性的值
result.setId(resultSet.getInt(1));
result.setName(resultSet.getString(2));
result.setDesc(resultSet.getString(3));
}
//返回结果
return result;
} catch (SQLException e) {
e.printStackTrace();
}finally{
//释放资源,关闭连接
DBUtil.close(resultSet,statement,connection);
}
//返回结果
return null;
}
}
ScoreDao工具类
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class ScoreDao {
/**
* 插入学生分数记录
*
* @param score
*/
public void insert(Score score) {
Connection connection = null;
PreparedStatement statement = null;
try {
//获取连接对象
connection = DBUtil.getConnection();
//构造sql语句
String sql = "insert into score values(?,?,?)";
//获取预处理对象
statement = connection.prepareStatement(sql);
//替换占位符
statement.setBigDecimal(1, score.getScore());
statement.setInt(2, score.getStudentId());
statement.setInt(3, score.getCourseId());
//执行sql语句,并获取返回结果
int result = statement.executeUpdate();
if (result > 0) {
System.out.println("添加学生信息成功!!!");
} else {
System.out.println("执行指令失败!!!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
//释放资源,关闭连接
DBUtil.close(null, statement, connection);
}
}
/**
* 修改学生成绩
*
* @param score
*/
public void update(Score score) {
Connection connection = null;
PreparedStatement statement = null;
try {
//获取数据库连接对象
connection = DBUtil.getConnection();
//构造sql语句
String sql = "update score set score=? where student_id=? and course_id=?";
//获取预处理对象
statement = connection.prepareStatement(sql);
//替换占位符
statement.setBigDecimal(1, score.getScore());
statement.setInt(2, score.getStudentId());
statement.setInt(3, score.getCourseId());
//执行sql语句
int result = statement.executeUpdate();
if (result > 0) {
System.out.println("修改学生成绩成功!!!");
} else {
System.out.println("执行指令失败!!!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(null, statement, connection);
}
}
/**
* 删除学生成绩
*
* @param score
*/
public void delete(Score score) {
Connection connection = null;
PreparedStatement statement = null;
try {
//获取数据库连接对象
connection = DBUtil.getConnection();
//构造sql语句
String sql = "delete from score where student_id=? and course_id=?";
//获取预处理对象
statement = connection.prepareStatement(sql);
//替换占位符
statement.setInt(1, score.getStudentId());
statement.setInt(2, score.getCourseId());
//执行sql语句
int result = statement.executeUpdate();
if (result > 0) {
System.out.println("删除成功, studentId = " + score.getStudentId() + ", classesId = " + score.getCourseId() + ".");
} else {
System.out.println("执行指令失败!!!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(null, statement, connection);
}
}
/**
* 删除成绩
* @param studentId 学生编号
* @param courseId 课程编号
*/
public void deleteByStudentIdAndClassesId (int studentId, int courseId) {
Connection connection = null;
PreparedStatement statement = null;
try {
// 获取数据库连接
connection = DBUtil.getConnection();
// 定义SQL语句
String sql = "delete from score where student_id = ? and course_id = ?";
// 获取预处理对象
statement = connection.prepareStatement(sql);
// 替换占位符
statement.setInt(1, studentId);
statement.setInt(2, courseId);
// 执行SQL获取结果
int result = statement.executeUpdate();
if (result > 0) {
System.out.println("删除成功, studentId = " + studentId + ", classesId = " + courseId + ".");
} else {
System.out.println("删除失败");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 根据班级id查询所有学生的成绩
*
* @param classesId
* @return
*/
public List<Score> selectByClassesId(int classesId) {
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
//返回结果
List<Score> result = null;
try {
connection = DBUtil.getConnection();
//构造sql语句
String sql = "select s.score,s.student_id,s.course_id,stu.id,stu.sn,stu.name,co.id,co.name from classes c,student stu,Course co,score s where c.id=stu.classes_id and stu.id=s.student_id and s.course_id=co.id and c.id=?";
//执行sql语句
connection.prepareStatement(sql);
//替换占位符
statement.setInt(1, classesId);
//获取返回结果集
resultSet = statement.executeQuery();
if (resultSet.next()) {
if (result == null) {
result = new ArrayList<>();
}
//构建Score对象
Score score = new Score();
score.setScore(resultSet.getBigDecimal(1));
score.setStudentId(resultSet.getInt(2));
score.setCourseId(resultSet.getInt(3));
//关联学生对象
Student student = new Student();
student.setId(resultSet.getInt(4));
student.setSn(resultSet.getInt(5));
student.setName(resultSet.getString(6));
score.setStudent(student);
// 关联课程对象
Course course = new Course();
course.setId(resultSet.getInt(7));
course.setName(resultSet.getString(8));
score.setCourse(course);
// 加入到返回的结果集合
result.add(score);
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
DBUtil.close(resultSet,statement,connection);
}
return result;
}
}
StudentDao工具类
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class StudentDao {
/**
* 新增学生信息
* @param student
* @return
*/
public int insert(Student student){
Connection connection=null;
PreparedStatement statement=null;
int result=0;
try {
//建立数据库连接
connection= DBUtil.getConnection();
//构造SQL语句
String sql="insert into student (sn,name,qq_mail,classes_id) values(?,?,?,?)";
//获取预处理对象
statement=connection.prepareStatement(sql);
//替换占位符
statement.setInt(1,student.getSn());
statement.setString(2, student.getName());
statement.setString(3, student.getMail());
statement.setInt(4,student.getClassId());
//执行sql语句,返回结果
result=statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.close(null,statement,connection);
}
return result;
}
/**
* 根据学生姓名查询信息
* @param name
* @return
*/
public List<Student> selectByName(String name){
Connection connection=null;
PreparedStatement statement=null;
ResultSet resultSet=null;
List<Student> result=null;
try {
//建立数据库连接
connection=DBUtil.getConnection();
//构造sql语句
String sql="select * from student where name=?";
//获取预处理对象
statement=connection.prepareStatement(sql);
//替换占位符
statement.setString(1,name);
resultSet = statement.executeQuery();
while(resultSet.next()){
//第一次进来时先判断集合是否为空,为空就创建一个
if(result==null){
result=new ArrayList<>();
}
//构造学生对象
Student student=new Student();
student.setId(resultSet.getInt(1));
student.setSn(resultSet.getInt(2));
student.setName(resultSet.getString(3));
student.setMail(resultSet.getString(4));
student.setClassId(resultSet.getInt(5));
//把学生对象加入集合中
result.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.close(resultSet,statement,connection);
}
//返回查询结果
return result;
}
/**
* 更新学生的班级
* @param studentId 学生班级号
* @param classesId 班级编号
* @return
*/
public int updateClassId(int studentId,int classesId){
Connection connection=null;
PreparedStatement statement=null;
int result=0;
try {
//建立连接
connection=DBUtil.getConnection();
//构建sql语句
String sql="update student set classes_id=? where id=?";
//获取预处理对象
statement=connection.prepareStatement(sql);
//替换占位符
statement.setInt(1,classesId);
statement.setInt(2,studentId);
//执行sql并返回结果
result =statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.close(null,statement,connection);
}
return result;
}
/**
* 根据班级编号查询学生信息
* @param classesId
* @return
*/
public List<Student> selectByClassId(int classesId){
Connection connection=null;
PreparedStatement statement=null;
ResultSet resultSet=null;
List<Student> result=null;
try {
//建立数据库连接
connection=DBUtil.getConnection();
//构造sql语句
String sql="select * from student where classes_id=?";
//获取预处理对象
statement=connection.prepareStatement(sql);
//替换占位符
statement.setInt(1,classesId);
//执行sql语句,并返回结果
resultSet=statement.executeQuery();
while(resultSet.next()){
//判断返回数据集是否为空
if(result==null){
result =new ArrayList<>();
}
//创建学生对象
Student student=new Student();
student.setId(resultSet.getInt(1));
student.setSn(resultSet.getInt(2));
student.setName(resultSet.getString(3));
student.setMail(resultSet.getString(4));
student.setClassId(resultSet.getInt(5));
//把学生对象加入集合中
result.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.close(resultSet,statement,connection);
}
//返回结果集
return result;
}
/**
* 根据学生id删除学生信息
* @param id
* @return
*/
public int deleteId(int id){
Connection connection=null;
PreparedStatement statement=null;
int result = 0;
try {
//连接数据库
connection=DBUtil.getConnection();
//构建sql语句
String sql="delete from student where id=?";
//获取预处理对象
statement= connection.prepareStatement(sql);
//替换占位符
statement.setInt(1,id);
//执行sql,并获取返回结果
result=statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
//返回受影响的行数
return result;
}
}
4、实现的界面功能
4.1、指定某个班级新增一位同学
import Dao.Text01.ClassesDao;
import Dao.Text01.StudentDao;
import Model.Text01.Classes;
import Model.Text01.Student;
import java.text.MessageFormat;
import java.util.Scanner;
/**
* 指定某个班级新增一位同学
*/
public class Exe_01 {
//按照逻辑顺序一步一步实现功能,遇到需要的数据库操作去工具包Dao中去调用即可
//先分析需求实现步骤,把能够用到的sql操作语句都实现了
//最后处理用户交互问题
public static void main(String[] args) {
while(true){
Scanner sc=new Scanner(System.in);
//1、先让用户输入班级名称
System.out.println("请输入班级名称:");
String className=sc.next();
//根据班级名称去数据库中查询班级编号
ClassesDao classesDao=new ClassesDao();
Classes classes= classesDao.selectByName(className);
//修改错误信息出现的异常
if(classes==null){
System.out.println("你要找的班级名称不存在"+className);
return;
}
//处理打印信息
String classInfo= MessageFormat.format("班级编号:{0}\t班级名称:{1}\t",classes.getId(),classes.getName());
System.out.println(classInfo);
//录入学生信息
System.out.println("============录入学生信息===========");
System.out.println("请输入学号:");
int sn=sc.nextInt();
System.out.println("请输入姓名:");
String name=sc.next();
System.out.println("请输入邮箱:");
String mail=sc.next();
System.out.println("请输入班级学号:");
int classes_id=sc.nextInt();
//将上面获取到的信息封装成学生类
Student student=new Student();
student.setSn(sn);
student.setName(name);
student.setMail(mail);
student.setClassId(classes_id);
//把这个学生对象添加到数据库中
StudentDao studentDao=new StudentDao();
int result=studentDao.insert(student);
//result是数据库返回了更改的行数
if(result>0){
System.out.println("新增学生成功!!!"+student.getName());
}else{
System.out.println("新增学生失败!!!");
}
}
}
}
4.2、修改指定学生的班级
import Dao.Text01.StudentDao;
import Model.Text01.Student;
import java.text.MessageFormat;
import java.util.List;
import java.util.Scanner;
/**
* 修改指定学生的班级
*/
public class Exe_02 {
public static void main(String[] args) {
//1、先要获取某个同学的信息
while (true) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生的姓名:");
String studentName = sc.next();
//2、根据学生姓名获取详细信息
StudentDao studentDao = new StudentDao();
List<Student> students = studentDao.selectByName(studentName);
//判断获取到的信息是否为空
if (students == null) {
System.out.println("非常抱歉,没有找到对应的学生信息:" + studentName);
}
//循环打印学生信息
for (Student student : students) {
String info = MessageFormat.format("编号:{0}\t| 学号:{1}\t| 姓名:{2}\t| 邮箱:{3}\t| 班级编号:{4}",
student.getId(), student.getSn(), student.getName(), student.getMail(), student.getClassId());
System.out.println(info);
}
//获取目标班级的编号
System.out.println("请输入要调班的学生编号:");
int studentId = sc.nextInt();
System.out.println("请输入要调入的目标班级编号:");
int classesId = sc.nextInt();
//调用StudentDao工具包中的Update方法,更新学生班级
int result = studentDao.updateClassId(studentId, classesId);
if (result > 0) {
System.out.println("学生调班成功");
} else {
System.out.println("调班失败!!");
}
}
}
}
4.3、根据班级名称查询这个班级所有学生
import Dao.Text01.ClassesDao;
import Dao.Text01.StudentDao;
import Model.Text01.Classes;
import Model.Text01.Student;
import java.text.MessageFormat;
import java.util.List;
import java.util.Scanner;
/**
* 根据班级名称查询这个班级所有学生
*/
public class Exe_03 {
public static void main(String[] args) {
while(true){
Scanner sc=new Scanner(System.in);
//1、让用户输入班级名称
System.out.println("请输入班级名称:");
String className=sc.next();
//2、根据班级名称去数据库中查询班级编号
ClassesDao classesDao=new ClassesDao();
Classes classes=classesDao.selectByName(className);
//3.修改错误信息出现的空指针异常
if (classes == null) {
System.out.println("你要找的班级不存在" + className);
return;
}
//4、获取对应班级的所有学生
StudentDao studentDao=new StudentDao();
List<Student> students=studentDao.selectByClassId(classes.getId());
//5、判断是否为空
if(students==null){
System.out.println("没有找到对应班级的学生信息"+className);
}
//6、打印结果集
for (Student student:students) {
String Info= MessageFormat.format("编号:{0}\t| 学号:{1}\t| 姓名:{2}\t| 邮箱:{3}\t| 班级编号:{4}",
student.getId(), student.getSn(), student.getName(), student.getMail(), student.getClassId());
System.out.println(Info);
}
}
}
}
4.4、删除某个同学的信息
import Dao.Text01.StudentDao;
import Model.Text01.Student;
import java.text.MessageFormat;
import java.util.List;
import java.util.Scanner;
/**
* 删除某个同学的信息
*/
public class Exe_04 {
public static void main(String[] args) {
//先要获取某个同学的信息
while (true) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生姓名:");
String studentName = sc.next();
//校验是否为空
if (studentName == null || studentName.equals("")) {
System.out.println("输入学生姓名为空");
continue;
}
//根据学生姓名获取详细信息
StudentDao studentDao = new StudentDao();
List<Student> students = studentDao.selectByName(studentName);
//判断获取学生数据集是否为空
if (students == null) {
System.out.println("非常抱歉,没有找到对应的学生信息:studentName" + studentName);
continue;
}
//打印结果集
for (Student student : students) {
String Info = MessageFormat.format("编号:{0}\t| 学号:{1}\t| 姓名:{2}\t| 邮箱:{3}\t| 班级编号:{4}",
student.getId(), student.getSn(), student.getName(), student.getMail(), student.getClassId());
System.out.println(Info);
}
//让用户输入要删除的学生编号
System.out.println("请输入要删除的学生编号:");
int studentId = sc.nextInt();
int result = studentDao.deleteId(studentId);
//result是数据库返回的一个更改行数
if (result > 0) {
System.out.println("删除学生信息成功!!,id="+studentId+"name ="+studentName);
} else {
System.out.println("删除学生信息失败!!,id="+studentId+"name ="+studentName);
}
}
}
}
4.5、修改学生的成绩
import Dao.Text01.ScoreDao;
import Model.Text01.Score;
import java.math.BigDecimal;
import java.util.Scanner;
/**
* 修改学生的成绩
*/
public class Exe_05 {
public static void main(String[] args) {
System.out.println("添加学生成绩");
System.out.println("==============================");
while (true) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生编号:");
int studentId = scanner.nextInt();
System.out.println("请输入课程编号:");
int courseId = scanner.nextInt();
System.out.println("请输入分数:");
BigDecimal score = new BigDecimal(scanner.next());
// 打印日志
System.out.println(studentId + "-" + courseId + "-" + score);
// 构建Score对象
Score scoreObj = new Score();
scoreObj.setScore(score);
scoreObj.setStudentId(studentId);
scoreObj.setCourseId(courseId);
// 调和ScoreDao完成插入操作
ScoreDao scoreDao = new ScoreDao();
scoreDao.insert(scoreObj);
}
}
}
4.6、修改学生的成绩
import Dao.Text01.ScoreDao;
import Model.Text01.Score;
import java.math.BigDecimal;
import java.util.Scanner;
/**
* 修改学生的成绩
*/
public class Exe_06 {
public static void main(String[] args) {
System.out.println("=========== 修改学生成绩 ==========");
while (true) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生Id:");
int studentId = scanner.nextInt();
System.out.println("请输入课程Id");
int courseId = scanner.nextInt();
System.out.println("请输入成绩:");
BigDecimal score = new BigDecimal(scanner.next());
// 调用DAO的更新方法
Score scoreObj = new Score();
scoreObj.setScore(score);
scoreObj.setStudentId(studentId);
scoreObj.setCourseId(courseId);
// 执行更新
ScoreDao scoreDao = new ScoreDao();
scoreDao.update(scoreObj);
}
}
}
4.7、删除某个同学的成绩
import Dao.Text01.ScoreDao;
import java.util.Scanner;
/**
* 删除某个同学的成绩
*/
public class Exe_07 {
public static void main(String[] args) {
System.out.println("============== 删除学生的成绩 ==============");
while (true) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生编号:");
int studentId = scanner.nextInt();
System.out.println("请输入课程编号:");
int courseId = scanner.nextInt();
// 调用Dao中的删除方法
ScoreDao scoreDao = new ScoreDao();
scoreDao.deleteByStudentIdAndClassesId(studentId, courseId);
}
}
}
4.8、查询指定班级同学的成绩
public class Exe_08 {
public static void main(String[] args) {
System.out.println("=========== 查询指定班级同学的成绩 ===========");
while (true) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入班级名称:");
String classesName = scanner.next();
// 获取班级信息
ClassesDao classesDao = new ClassesDao();
Classes classes = classesDao.selectByName(classesName);
if (classes == null) {
System.out.println("指定的班级不存在,请检查。班级名 = " + classesName);
break;
}
// 调用ScoreDao查询结果
ScoreDao scoreDao = new ScoreDao();
List<Score> scores = scoreDao.selectByClassesId(classes.getId());
if (scores == null) {
System.out.println("没有找到班级为:" + classes.getName() + " 的成绩信息.");
break;
}
for (Score score : scores) {
String str = MessageFormat.format("班级:{0} | 学号:{1} | 姓名:{2} \t| 分数:{3} | 课程:{4}",
classes.getName(),
score.getStudent().getSn(),
score.getStudent().getName(),
score.getScore(),
score.getCourse().getName());
System.out.println(str);
}
System.out.println("==========================================================");
}
}
}
5、总结
我所做的课题学生成绩管理系统部分功能基本完成。其功能基本符合的用户要求,能够对学生成绩的基本信息进行查询、修改、添加、删除。