学生选课与成绩管理系统(数据库版)
1、配置MySQL数据库
2、利用java+MySQL实现系统各个功能
3、功能包括:
(1)系统交互界面,如下图所示
(2)学生信息的增删改查。学生属性包括学号(唯一),姓名,专业。
(3)课程信息的增删改查。课程属性包括编号(唯一),课程名,学分。
(4)学生实现选课。用户输入学号,进入选课功能。显示学生已选课程和未选课程。学生通过输入课程编号选课。选课后,已选课程表和未选课程表动态变化。
(5)老师录入成绩。老师输入课程编号,为选择课程的学生录入或修改成绩。输入课程编号后,显示该门课程选课的学生和对应的成绩信息。老师通过学生编号录入成绩。
(6)学生通过学生编号查询自己的成绩。在输入编号后,打印输出,学生的所选课程的成绩信息。
(7)老师通过课程编号查询每门课程的成绩信息。
开发环境:
Java jdk1.8、MySQL8.0
工具:
idea2021专业版、DBeaver数据库管理工具
package student;
import dao.StudentDao;
import java.util.Scanner;
public class Student
{
int count= StudentDao.getMaxId();
private int id; // 学生学号
private String name; // 学生姓名
private String major; // 学生专业
Scanner reader = new Scanner(System.in);
public Student(int id, String name, String major)
{
this.id = id;
this.name = name;
this.major = major;
}
public Student() throws Exception
{
System.out.println("请输入学生姓名:");
String name = reader.next();
System.out.println("请输入学生专业:");
String major = reader.next();
count = count + 1;
this.id = count;
this.name = name;
this.major = major;
}
@Override
public String toString()
{
return this.id + "\t\t" + this.name + "\t\t" + this.major;
}
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 getMajor()
{
return major;
}
public void setMajor(String major)
{
this.major = major;
}
}
package student;
import dao.StudentDao;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;
public class StudentOperation
{
Scanner reader = new Scanner(System.in);
/*
* 在数据库中插入学生信息
*/
public void insert() throws Exception
{
Student student = new Student();
StudentDao.insert(student);
}
/*
* 更新学生信息
*/
public void update() throws Exception
{
System.out.println("请输入学生id:");
int id = reader.nextInt();
Student oldStudent = StudentDao.getStudentById(id);
if(oldStudent == null)
{
System.out.println("没有这个学生!");
}else
{
System.out.println(oldStudent);
System.out.println("请输入修改后的学生姓名:");
String name = reader.next();
System.out.println("请输入修改后的学生专业:");
String major = reader.next();
oldStudent.setName(name);
oldStudent.setMajor(major);
StudentDao.update(oldStudent);
}
}
/*
* 学生信息的删除
*/
public void delete()
{
System.out.println("请输入学生id:");
int id = reader.nextInt();
Student oldStudent = StudentDao.getStudentById(id);
if(oldStudent == null)
{
System.out.println("没有这个学生!");
}else
{
System.out.println(oldStudent);
System.out.println("是否删除:Y/N");
String s = reader.next();
if(s.equals("Y") || s.equals("y"))
{
StudentDao.delete(oldStudent);
}
}
}
/*
* 学生信息的查询
*/
public void search()
{
System.out.println("请输入查询关键字,如姓名、专业(支持模糊查询):");
String key = reader.next();
List<Student> result;
result = StudentDao.findStudent(key);
assert result != null;
printStudentInfo(result);
}
/*
* 学生信息打印输出
*/
public void printStudentInfo(List<Student> students)
{
System.out.println("学号\t\t姓名\t\t专业");
System.out.println("--------------------------");
if(!students.isEmpty())
{
for(Student student : students)
{
System.out.println(student);
}
}else
{
System.out.println("没有学生信息");
}
System.out.println("--------------------------");
}
/*
* 学生信息操作方法
*/
public void studentSystemAdmin() throws Exception
{
int choice;
while(true)
{
System.out.println("--------学生信息管理--------");
System.out.println("1----新增学生");
System.out.println("2----修改学生");
System.out.println("3----删除学生");
System.out.println("4----查询学生");
System.out.println("0----退出模块");
while(true)
{
try
{
choice = reader.nextInt();
break;
}
catch (InputMismatchException e)
{
System.out.println("输入类型不匹配,请重新输入!");
reader = new Scanner(System.in);
}
}
if(choice == 1)
{
insert();
}else if(choice == 2)
{
update();
}else if(choice == 3)
{
delete();
}else if(choice == 4)
{
search();
}else if(choice == 0)
{
break;
}
}
}
}
package dao;
import student.Student;
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 extends Dao
{
/*
* 数据库插入方法
*/
public static void insert(Student s)
{
Connection connection = null;
PreparedStatement p = null;
ResultSet resultSet = null;
String sql = "insert into studentab(name,major) values(?,?)";
try
{
connection = getConn();
p = connection.prepareStatement(sql);
p.setString(1,s.getName());
p.setString(2,s.getMajor());
p.executeUpdate();
}
catch (SQLException e)
{
e.printStackTrace();
System.out.println("数据插入失败!");
}
finally
{
Dao.close(resultSet, p, connection); // 执行关闭操作
}
}
/*
* 通过学生学号找到学生
*/
public static Student getStudentById(int aId)
{
Connection connection = null;
PreparedStatement p = null;
ResultSet resultSet = null;
String sql = "select * from studentab where id=?";
try
{
connection = getConn();
p = connection.prepareStatement(sql);
p.setInt(1,aId);
resultSet = p.executeQuery();
if(resultSet.next())
{
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String major = resultSet.getString("major");
return new Student(id,name,major);
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
Dao.close(resultSet, p, connection);
}
return null;
}
/*
* 通过姓名找到学生
*/
public static Student getStudentByName(String name)
{
Connection connection = null;
PreparedStatement p = null;
ResultSet resultSet = null;
String sql = "select * from studentab where name=?";
try
{
connection = getConn();
p = connection.prepareStatement(sql);
p.setString(1,name);
resultSet = p.executeQuery();
if(resultSet.next())
{
int id = resultSet.getInt("id");
String major = resultSet.getString("major");
return new Student(id,name,major);
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
Dao.close(resultSet, p, connection);
}
return null;
}
/*
* 通过关键字查询学生信息
*/
public static List<Student> findStudent(String key)
{
List<Student> results = new ArrayList<>();
Connection connection = null;
PreparedStatement p = null;
ResultSet resultSet = null;
String sql = "select * from studentab where name like ? or major like ?";
try
{
connection = getConn();
p = connection.prepareStatement(sql);
p.setString(1,"%"+key+"%");
p.setString(2,"%"+key+"%");
resultSet = p.executeQuery();
while(resultSet.next())
{
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String major = resultSet.getString("major");
Student student = new Student(id,name,major);
results.add(student);
}
return results;
}
catch (SQLException e)
{
e.printStackTrace();
return null;
}
finally
{
Dao.close(resultSet, p, connection);
}
}
/*
* 数据库更新方法
*/
public static void update(Student student)
{
Connection connection = null;
PreparedStatement p = null;
ResultSet resultSet = null;
String sql = "update studentab set name=?,major=?" + "where id =?";
try
{
connection = getConn();
p = connection.prepareStatement(sql);
p.setString(1,student.getName());
p.setString(2,student.getMajor());
p.setInt(3,student.getId());
p.executeUpdate();
}
catch (SQLException e)
{
e.printStackTrace();
System.out.println("数据更新失败!");
}
finally
{
Dao.close(resultSet, p, connection);
}
}
/*
* 数据库删除方法
*/
public static void delete(Student student)
{
Connection connection = null;
PreparedStatement p = null;
ResultSet resultSet = null;
String sql = "delete from studentab where id=?";
try
{
connection = getConn();
p = connection.prepareStatement(sql);
p.setInt(1,student.getId());
p.executeUpdate();
}
catch (SQLException e)
{
e.printStackTrace();
System.out.println("数据删除失败!");
}
finally
{
Dao.close(resultSet, p, connection);
}
}
public static int getMaxId()
{
int maxId = 0;
Connection connection = null;
PreparedStatement p = null;
ResultSet resultSet = null;
String sql = "select max(id) max_id from studentab";
try
{
connection = getConn();
p = connection.prepareStatement(sql);
resultSet = p.executeQuery();
if(resultSet.next())
{
maxId = resultSet.getInt("max_id");
}
return maxId;
}
catch (SQLException e)
{
e.printStackTrace();
return 0;
}
finally
{
Dao.close(resultSet, p, connection);
}
}
}
package course;
import java.util.Scanner;
public class Course
{
static int count = 0;
private int no; // 课程编号
private String name; // 课程名称
private double score; // 课程学分
Scanner reader = new Scanner(System.in);
public Course(int no, String name, double score)
{
this.no = no;
this.name = name;
this.score = score;
}
public Course() throws Exception
{
System.out.println("输入课程名称:");
String name=reader.next();
System.out.println("输入课程学分:");
double score=reader.nextDouble();
count=count+1;
this.name = name;
this.no = count;
this.score = score;
}
public int getNo()
{
return no;
}
public void setNo(int no)
{
this.no = no;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public double getScore()
{
return score;
}
public void setScore(double score)
{
this.score = score;
}
@Override
public String toString()
{
return this.no + "\t\t" + this.name + "\t\t" + this.score;
}
/*
* 重写equals方法,方便后续使用removeAll方法
*/
public boolean equals(Object obj)
{
if(this == obj)
return true;
if(obj == null || getClass() != obj.getClass())
return false;
Course course = (Course) obj;
return course.getNo() == this.getNo() && course.getName().equals(this.getName()) && course.getScore() == this.getScore();
}
}
package course;
import dao.CourseDao;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;
public class CourseOperation
{
Scanner reader = new Scanner(System.in);
/*
* 在数据库中插入课程信息
*/
public void insert() throws Exception
{
Course course = new Course();
CourseDao.insert(course);
}
/*
* 更新课程信息
*/
public void update()
{
System.out.println("请输入课程的编号:");
int no;
while(true)
{
try
{
no = reader.nextInt();
break;
}catch (InputMismatchException e)
{
System.out.println("输入类型不匹配,请重新输入!");
reader = new Scanner(System.in);
}
}
Course oldCourse = CourseDao.getCourseByNo(no);
if(oldCourse == null)
{
System.out.println("没有这个课程!");
}else
{
System.out.println(oldCourse);
System.out.println("请输入修改后的课程名称:");
String name = reader.next();
System.out.println("请输入修改后的课程学分:");
double score = reader.nextDouble();
oldCourse.setName(name);
oldCourse.setScore(score);
CourseDao.update(oldCourse);
}
}
/*
* 删除课程信息
*/
public void delete()
{
System.out.println("请输入课程的编号:");
int no = reader.nextInt();
Course oldCourse = CourseDao.getCourseByNo(no);
if (oldCourse != null) {
System.out.println(oldCourse);
System.out.println("是否删除:y/n");
String s = reader.next();
if (s.equals("y") || s.equals("Y"))
{
CourseDao.delete(oldCourse);
}
} else
{
System.out.println("没有这个课程!");
}
}
/*
* 课程信息的查询
*/
public void search()
{
System.out.println("请输入课程的名称(支持模糊查询):");
String key = reader.next();
List<Course> result;
result = CourseDao.findCourse(key);
assert result != null;
printCourseInfo(result);
}
/*
* 打印课程信息
*/
public static void printCourseInfo(List<Course> courses)
{
System.out.println("编号\t\t课程名称\t\t学分");
System.out.println("-----------------------------------");
if (!courses.isEmpty())
{
for (Course c : courses)
{
System.out.println(c);;
}
} else
{
System.out.println("没有课程信息");
}
System.out.println("-----------------------------------");
}
/*
* 课程信息操作
*/
public void courseSystemAdmin() throws Exception
{
int option;
while(true)
{
System.out.println("--------课程信息管理--------");
System.out.println("1----新增课程");
System.out.println("2----修改课程");
System.out.println("3----删除课程");
System.out.println("4----查询课程");
System.out.println("0----退出模块");
while(true)
{
try
{
option = reader.nextInt();
break;
}catch(InputMismatchException e)
{
System.out.println("输入类型不匹配,请重新输入!");
reader = new Scanner(System.in);
}
}
if(option == 1)
{
insert();
}else if(option == 2)
{
update();
}else if(option == 3)
{
delete();
}else if(option == 4)
{
search();
}else if(option == 0)
{
break;
}
}
}
}
package dao;
import course.Course;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class CourseDao extends Dao
{
/*
* 数据库插入方法
*/
public static void insert(Course course)
{
Connection connection = null;
PreparedStatement p = null;
ResultSet resultSet = null;
String sql = "insert into coursetab(name,score) values(?,?)";
try
{
connection = getConn();
p = connection.prepareStatement(sql);
p.setString(1,course.getName());
p.setDouble(2,course.getScore());
p.executeUpdate();
}
catch (SQLException e)
{
e.printStackTrace();
System.out.println("数据插入失败!");
}
finally
{
Dao.close(resultSet, p, connection);
}
}
/*
* 通过课程编号找到课程
*/
public static Course getCourseByNo(int aNo)
{
Connection connection = null;
PreparedStatement p = null;
ResultSet resultSet = null;
String sql = "select * from coursetab where no=?";
try
{
connection = getConn();
p = connection.prepareStatement(sql);
p.setInt(1,aNo);
resultSet = p.executeQuery();
if(resultSet.next())
{
int no = resultSet.getInt("no");
String name = resultSet.getString("name");
double score = resultSet.getDouble("score");
return new Course(no,name,score);
}
}
catch (SQLException e)
{
e.printStackTrace();
return null;
}
finally
{
Dao.close(resultSet, p, connection);
}
return null;
}
/*
* 通过关键字查询课程信息
*/
public static List<Course> findCourse(String key)
{
List<Course> results = new ArrayList<>();
Connection connection = null;
PreparedStatement p = null;
ResultSet resultSet = null;
String sql = "select * from coursetab where name like ?";
try
{
connection = getConn();
p = connection.prepareStatement(sql);
p.setString(1,"%"+key+"%");
resultSet = p.executeQuery();
while(resultSet.next())
{
int no = resultSet.getInt("no");
String name= resultSet.getString("name");
double score = resultSet.getDouble("score");
Course course = new Course(no,name,score);
results.add(course);
}
return results;
}
catch (SQLException e)
{
e.printStackTrace();
return null;
}
finally
{
Dao.close(resultSet, p, connection);
}
}
/*
* 通过名称找到课程信息
*/
public static Course findCourseByName(String aName)
{
Connection connection = null;
PreparedStatement p = null;
ResultSet resultSet = null;
String sql = "select no,name,score from coursetab where name=?";
try
{
connection = getConn();
p = connection.prepareStatement(sql);
p.setString(1,aName);
resultSet = p.executeQuery();
if(resultSet.next())
{
int no = resultSet.getInt("no");
double score = resultSet.getDouble("score");
return new Course(no,aName,score);
}
}
catch (SQLException e)
{
e.printStackTrace();
return null;
}
finally
{
Dao.close(resultSet, p, connection);
}
return null;
}
/*
* 查询所有课程信息
*/
public static List<Course> findAllCourse()
{
List<Course> courses = new ArrayList<>();
Connection connection = null;
PreparedStatement p = null;
ResultSet resultSet = null;
String sql = "select * from coursetab";
try
{
connection = getConn();
p = connection.prepareStatement(sql);
resultSet = p.executeQuery();
while(resultSet.next())
{
int no = resultSet.getInt("no");
String name = resultSet.getString("name");
double score = resultSet.getDouble("score");
Course course = new Course(no,name,score);
courses.add(course);
}
return courses;
}
catch (SQLException e)
{
e.printStackTrace();
return null;
}
finally
{
Dao.close(resultSet, p, connection);
}
}
/*
* 课程信息的更新
*/
public static void update(Course course)
{
Connection connection = null;
PreparedStatement p = null;
ResultSet resultSet = null;
String sql = "update coursetab set name=?,score=?" + "where no=?";
try
{
connection = getConn();
p = connection.prepareStatement(sql);
p.setString(1,course.getName());
p.setDouble(2,course.getScore());
p.setInt(3,course.getNo());
p.executeUpdate();
}
catch (SQLException e)
{
e.printStackTrace();
System.out.println("数据更新失败!");
}
finally
{
Dao.close(resultSet, p, connection);
}
}
/*
* 课程信息的删除
*/
public static void delete(Course course)
{
Connection connection = null;
PreparedStatement p = null;
ResultSet resultSet = null;
String sql = "delete from coursetab where no=?";
try
{
connection = getConn();
p = connection.prepareStatement(sql);
p.setInt(1,course.getNo());
p.executeUpdate();
}
catch (SQLException e)
{
e.printStackTrace();
System.out.println("数据删除失败!");
}
finally
{
Dao.close(resultSet, p, connection);
}
}
}
package grade;
import course.Course;
import student.Student;
public class Grade
{
private Student student; // 该成绩所对应的学生
private Course course; // 该成绩所对应的课程
private double grade; // 成绩
public Grade()
{
this.student = null;
this.course = null;
this.grade = 0;
}
public Student getStudent()
{
return student;
}
public void setStudent(Student student)
{
this.student = student;
}
public Course getCourse()
{
return course;
}
public void setCourse(Course course)
{
this.course = course;
}
public double getGrade()
{
return grade;
}
public void setGrade(double grade)
{
this.grade = grade;
}
}
package grade;
import course.Course;
import course.CourseOperation;
import dao.CourseDao;
import dao.GradeDao;
import dao.StudentDao;
import student.Student;
import java.util.*;
public class GradeOperation
{
Scanner reader = new Scanner(System.in);
/*
* 成绩信息操作
*/
public void GradeSystemAdmin()
{
int option;
while(true)
{
System.out.println("--------成绩信息管理--------");
System.out.println("1----学生选课");
System.out.println("2----教师录入成绩");
System.out.println("3----成绩查询");
System.out.println("0----退出模块");
while(true)
{
try
{
option = reader.nextInt();
break;
}catch (InputMismatchException e)
{
System.out.println("输入类型不匹配,请重新输入!");
reader = new Scanner(System.in);
}
}
if(option == 1)
{
selectCourseByStudent();
}else if(option == 2)
{
inputGrade();
}else if(option == 3)
{
queryGrades();
}else if(option == 0)
{
break;
}
}
}
/*
* 学生选课
*/
public void selectCourseByStudent()
{
System.out.println("请输入学生的学号:");
int stuId = reader.nextInt();
Student student = StudentDao.getStudentById(stuId); // 通过学生编号找到学生
if(student != null)
{
while(true)
{
// 显示该学生已经选择的课程
System.out.println("学生姓名:"+student.getName());
System.out.println("------------已选课程------------");
List<Course> selectCourses = GradeDao.findCoursesSelectByStudent(student, true);
if(selectCourses != null)
{
CourseOperation.printCourseInfo(selectCourses);
}else
{
System.out.println("暂无选课信息");
}
// 显示未选择的课程
System.out.println("------------未选课程------------");
List<Course> noSelectCourses = GradeDao.findCoursesSelectByStudent(student, false);
if(noSelectCourses != null)
{
CourseOperation.printCourseInfo(noSelectCourses);
}else
{
System.out.println("所有课程已选");
}
// 开始选课
System.out.println("请输入课程编号:0--退出选课");
int courseNo = reader.nextInt();
if(courseNo == 0)break;
else
{
Course course = CourseDao.getCourseByNo(courseNo);
if(course != null)
{
// 判断该课程是否被学生选过
boolean flag = GradeDao.getGradeByStudentAndCourse(student,course);
if(flag)
{
System.out.println("该课程已选");
}else
{
Grade newGrade = new Grade();
newGrade.setStudent(student);
newGrade.setCourse(course);
GradeDao.insert(newGrade); // 新增成绩信息
}
}else
{
System.out.println("课程不存在");
}
}
}
}else
{
System.out.println("没有这个学生");
}
}
/*
* 老师录入成绩
*/
public void inputGrade()
{
System.out.println("所有课程如下:");
CourseOperation.printCourseInfo(Objects.requireNonNull(CourseDao.findAllCourse()));
System.out.println("请输入需要录入成绩的课程编号:");
int courseNo = reader.nextInt();
Course course = CourseDao.getCourseByNo(courseNo);
if(course != null)
{
while(true)
{
List<Grade> grades = GradeDao.findGradesByCourse(course);
assert grades != null;
printCourseGradeInfo(grades,course);
System.out.println("请输入学生学号:0--退出");
int stuId = reader.nextInt();
if(stuId == 0)break;
else
{
Student student = StudentDao.getStudentById(stuId);
if(student != null)
{
// 判断该学生是否选修了该门课程
boolean flag = GradeDao.getGradeByStudentAndCourse(student,course);
if(flag)
{
System.out.println("请输入 "+student.getName()+" 的成绩:");
double g = reader.nextDouble();
GradeDao.update(g, student, course); // 更新成绩信息
}else
{
System.out.println(student.getName()+"没有选修这门课");
}
}else
{
System.out.println("没有这个学生!");
}
}
}
}else
{
System.out.println("课程不存在!");
}
}
/*
* 打印课程成绩信息
*/
public void printCourseGradeInfo(List<Grade> grades, Course course)
{
System.out.println("课程名称:" + course.getName());
System.out.println("学号\t\t学生姓名\t\t成绩");
System.out.println("------------------------------------");
if (grades.size() > 0)
{
for (Grade g : grades)
{
if (g != null)
{
System.out.println(g.getStudent().getId() + "\t\t" + g.getStudent().getName() + "\t\t" + g.getGrade());
}
}
} else
{
System.out.println("\t\t没有相关数据\t\t\t");
}
System.out.println("------------------------------------");
}
/*
* 个人成绩查询
*
*/
public void printStudentGrade()
{
System.out.println("请输入学生学号:");
int no = reader.nextInt();
Student student = StudentDao.getStudentById(no);
if (student != null)
{
System.out.println("------" + student.getName() + "-------");
System.out.println("课程名称\t\t成绩\t\t获得学分");
List<Grade> grades = GradeDao.findGradesByStudent(student);
assert grades != null;
if (!grades.isEmpty())
{
double totalScore = 0;
for (Grade g : grades)
{
double s=0;
if(g.getGrade() >= 60)
{
s=g.getCourse().getScore(); // 获取对应的学分
}
System.out.println(g.getCourse().getName() + "\t\t" + g.getGrade() + "\t\t" +s );
totalScore = totalScore + s;
}
System.out.println("----获得的总学分为:" + totalScore + "-------");
}else
{
System.out.println("暂无成绩信息");
}
}else
{
System.out.println("没有学号为 "+no+" 的学生");
}
}
/*
* 成绩查询功能
*/
public void queryGrades()
{
int option;
while(true)
{
System.out.println("--------成绩查询--------");
System.out.println("1----个人成绩查询");
System.out.println("2----课程成绩查询");
System.out.println("0----退出查询");
while(true)
{
try
{
option = reader.nextInt();
break;
}catch(InputMismatchException e)
{
System.out.println("输入类型不匹配,请重新输入!");
reader = new Scanner(System.in);
}
}
if(option == 1)
{
printStudentGrade();
}else if(option == 2)
{
System.out.println("请输入课程编号:");
int courseNo = reader.nextInt();
Course course = CourseDao.getCourseByNo(courseNo);
if(course != null)
{
List<Grade> grades = GradeDao.findGradesByCourse(course);
assert grades != null;
printCourseGradeInfo(grades,course);
}else
{
System.out.println("没有这门课程!");
}
}else if(option == 0)
{
break;
}
}
}
}
package dao;
import course.Course;
import grade.Grade;
import student.Student;
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 GradeDao extends Dao
{
/*
* 数据库中新增成绩信息
*/
public static void insert(Grade grade)
{
Connection connection = null;
PreparedStatement p = null;
ResultSet resultSet = null;
String sql = "insert into gradetab(student,course,grade) values(?,?,?)";
try
{
connection = getConn();
p = connection.prepareStatement(sql);
p.setInt(1,grade.getStudent().getId());
p.setInt(2,grade.getCourse().getNo());
p.setDouble(3,grade.getGrade());
p.executeUpdate();
}
catch (SQLException e)
{
e.printStackTrace();
System.out.println("数据插入失败!");
}
finally
{
Dao.close(resultSet, p, connection);
}
}
/*
* 通过学生查找其已选课程或未选课程
*/
public static List<Course> findCoursesSelectByStudent(Student student, boolean flag)
{
List<Course> allCourses = CourseDao.findAllCourse(); // 查询所有课程
Connection connection = null;
PreparedStatement p = null;
ResultSet resultSet = null;
String sql = "select c.no,c.name,c.score from gradetab g \n" +
"left join studentab s on g.student = s.id \n" +
"left join coursetab c on g.course = c.no where s.name=?";
try
{
connection = getConn();
p = connection.prepareStatement(sql);
p.setString(1,student.getName());
resultSet = p.executeQuery();
List<Course> courses = new ArrayList<>();
while(resultSet.next())
{
int no = resultSet.getInt("no");
String name = resultSet.getString("name");
double score = resultSet.getDouble("score");
Course course = new Course(no, name, score);
courses.add(course); // 得到已选课程
}
if(flag)
{
return courses; // 如果flag为true,表示查询已选课程,直接返回
}else
{
assert allCourses != null;
allCourses.removeAll(courses); // 除去已选课程得到未选课程
return allCourses;
}
}catch (SQLException e)
{
e.printStackTrace();
return null;
}
finally
{
Dao.close(resultSet, p, connection);
}
}
/*
* 通过学生和课程判断该学生是否已经选择了该课程
*/
public static boolean getGradeByStudentAndCourse(Student student, Course course)
{
Connection connection = null;
PreparedStatement p = null;
ResultSet resultSet = null;
String sql = "select student,course,grade from gradetab g \n" +
"left join studentab s on g.student = s.id\n" +
"left join coursetab c on g.course = c.no\n" +
"where s.name=? and c.name = ?";
try
{
connection = getConn();
p = connection.prepareStatement(sql);
p.setString(1,student.getName());
p.setString(2,course.getName());
resultSet = p.executeQuery();
if(resultSet.next())
{
return true;
}
else
{
return false;
}
}
catch (SQLException e)
{
e.printStackTrace();
return false;
}
finally
{
Dao.close(resultSet, p, connection); // 关闭操作
}
}
/*
* 通过学生查找其成绩信息
*/
public static List<Grade> findGradesByStudent(Student student)
{
List<Grade> results = new ArrayList<>();
Connection connection = null;
PreparedStatement p = null;
ResultSet resultSet = null;
String sql = "select s.name sname,c.name cname,g.grade from gradetab g left join studentab s on g.student = s.id \n" +
"left join coursetab c on g.course = c.no where s.name=?";
try
{
connection = getConn();
p = connection.prepareStatement(sql);
p.setString(1,student.getName());
resultSet = p.executeQuery();
while(resultSet.next())
{
double gra = resultSet.getDouble("grade"); // 课程成绩
String cname = resultSet.getString("cname");// 课程名
Course course = CourseDao.findCourseByName(cname); // 获得课程类对象
Grade grade = new Grade();
grade.setStudent(student);
grade.setCourse(course);
grade.setGrade(gra);
results.add(grade);
}
return results;
}
catch (SQLException e)
{
e.printStackTrace();
return null;
}
finally
{
Dao.close(resultSet, p, connection); // 关闭操作
}
}
/*
* 通过课程找到该课程的所有成绩记录
*
*/
public static List<Grade> findGradesByCourse(Course course)
{
List<Grade> results = new ArrayList<>();
Connection connection = null;
PreparedStatement p = null;
ResultSet resultSet = null;
String sql = "select s.name sname,c.name cname,g.grade from gradetab g left join studentab s on g.student = s.id \n" +
"left join coursetab c on g.course = c.no where c.name=?";
try
{
connection = getConn();
p = connection.prepareStatement(sql);
p.setString(1,course.getName());
resultSet = p.executeQuery();
while(resultSet.next())
{
double gra = resultSet.getDouble("grade");
String name = resultSet.getString("sname");
Student student = StudentDao.getStudentByName(name);
Grade grade = new Grade();
grade.setStudent(student);
grade.setCourse(course);
grade.setGrade(gra);
results.add(grade);
}
return results;
}
catch (SQLException e)
{
e.printStackTrace();
return null;
}
finally
{
Dao.close(resultSet, p, connection); // 关闭操作
}
}
/*
* 更新成绩信息
*/
public static void update(double gra, Student student, Course course)
{
Connection connection = null;
PreparedStatement p = null;
ResultSet resultSet = null;
String sql = "update gradetab set grade= ? where student = ? and course = ?";
try
{
connection = getConn();
p = connection.prepareStatement(sql);
p.setDouble(1,gra);
p.setInt(2,student.getId());
p.setInt(3,course.getNo());
p.executeUpdate();
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
Dao.close(resultSet, p, connection);
}
}
}
package dao;
import java.sql.*;
public class Dao
{
/*
* 数据库的连接
*
*/
public static Connection getConn()
{
String driverName="com.mysql.cj.jdbc.Driver";
String dbURL="jdbc:mysql://localhost:3306/studentcourseadmin";
String userName="root";
String userPwd="123456";
Connection con=null;
try{
Class.forName(driverName);
con= DriverManager.getConnection(dbURL,userName,userPwd);
}catch(Exception e)
{
System.out.println("数据库连接失败");
}
return con;
}
/*
* 数据库连接使用后,必要的关闭操作
*/
public static void close(ResultSet rs, PreparedStatement p, Connection conn) {
if (rs != null)
{
try
{
rs.close(); // 关闭结果集
} catch (Exception e)
{
e.printStackTrace();
}
}
if (p != null)
{
try
{
p.close(); // 关闭SQL语句
} catch (Exception e)
{
e.printStackTrace();
}
}
if (conn != null)
{
try
{
conn.close(); // 关闭数据库连接
} catch (Exception e)
{
e.printStackTrace();
}
}
}
}
package main;
import course.CourseOperation;
import grade.GradeOperation;
import student.StudentOperation;
import java.util.InputMismatchException;
import java.util.Scanner;
public class SystemAdmin
{
Scanner reader = new Scanner(System.in);
/*
* 菜单选项
*/
public void menu() throws Exception
{
int option;
while(true)
{
System.out.println("学生选课与成绩管理系统(数据库版)");
System.out.println("------------------------------");
System.out.println("1----学生信息管理");
System.out.println("2----课程信息管理");
System.out.println("3----成绩信息管理");
System.out.println("0----退出系统");
while(true)
{
try
{
option = reader.nextInt();
break;
}catch (InputMismatchException e)
{
System.out.println("输入类型不匹配,请重新输入!");
reader = new Scanner(System.in);
}
}
switch(option)
{
case 1:
StudentOperation studentOperation = new StudentOperation();
studentOperation.studentSystemAdmin();
break;
case 2:
CourseOperation courseOperation = new CourseOperation();
courseOperation.courseSystemAdmin();
break;
case 3:
GradeOperation gradeOperation = new GradeOperation();
gradeOperation.GradeSystemAdmin();
break;
case 0:
System.exit(0); // 退出系统
}
}
}
}
package main;
import dao.Dao;
public class Main
{
/*
* 程序启动的入口
*/
public static void main(String[] args) throws Exception
{
SystemAdmin systemAdmin = new SystemAdmin();
systemAdmin.menu();
}
}