Experiment 15: 20211127 Java Big Data 1+X Intermediate Practical Examination (id: 2660)

1. Project background description

The student performance management system is a system used by primary and secondary schools and other units. This project uses the console to simulate some of its functions.

  • The entity classes involved in the student performance management system are: Student, Course, and Score.
  • The corresponding data tables include: student table, course table, and score table.
  • The functions mainly involve: student information management, course information management, score entry and query, etc.

2. Table structure

  • student table
    The meaning of this table is students. It has 4 fields. The details are as follows:
table fields Database data type meaning Remark
name VARCHAR student name primary key
pwd VARCHAR student password
age INT Student age
grade INT Student grade
rate INT Overall student rating
  • course table
    The meaning of this table is courses. There are 3 fields in total. The details are as follows:
table fields Database data type Remark Remark
name VARCHAR Course Title primary key
teacher VARCHAR Instructor
difficulty VARCHAR Course Difficulty
  • score table.
    The meaning of this table is scores. There are 3 fields in total. The details are as follows:
table fields Database data type meaning Remark
takes off VARCHAR student name Foreign key->Student table (name)
cname VARCHAR Course Title Foreign key->Course schedule (name)
score INT course points

3. Steps

[5 points] Step 1: Project preparation

Copy and paste, omitted.

[5 points] Step 2: Complete the entity class Student

topic:

First we need to design a series of entity classes, which are org.lanqiao.bean.Student, org.lanqiao.bean.Course, and org.lanqiao.bean.Score. Now the Student class needs you to complete it.

org.lanqiao.bean.Student is an entity class with five attributes, which correspond to the database table student and its fields one-to-one. The corresponding relationship is as follows.

[The fields of the database table student and their correspondence with the attributes of the entity class Student]

serial number class attributes table fields Database data type Remark
1 name name VARCHAR primary key
2 pwd pwd VARCHAR
3 age age INT
4 grade grade INT
5 rate rate INT

Please fill in the missing codes according to the comment requirements.

public class Student {
    
    
    private String name;//学生姓名
    private String pwd;//学生密码
    private int age;//学生年龄
    private int grade;//学生年级
    private int rate;//学生综合评级

    //已经提供Student类的属性,补充完成该类的有参(五个参数)及无参构造方法

Answer:

//已经提供Student类的属性,补充完成该类的有参(五个参数)及无参构造方法
public Student(){
    
    }

public Student(String name, String pwd, int age, int grade, int rate){
    
    
	this.setName(name);
	this.setPwd(pwd);
	this.setAge(age);
	this.setGrade(grade);
	this.setRate(rate);
}

[10 points] Step 3: Complete the entity class Course

topic:

Now let's complete the Course class.

org.lanqiao.bean.Course is an entity class with three attributes, which correspond to the database table course and its fields one-to-one. The corresponding relationship is as follows.

[The fields of the database table course and their corresponding relationship with the attributes of the entity class Course]

serial number class attributes table fields Database data type Remark
1 name name VARCHAR primary key
2 teacher teacher VARCHAR
3 difficulty difficulty VARCHAR

Please fill in the missing codes according to the comment requirements.

package org.lanqiao.bean;

public class Course {
    
    
    //请修改该方法,并且在赋值时,课程难度只能为:高、中、低这三种之一
    public void setDifficulty(String difficulty) {
    
    
        this.difficulty = difficulty;
    }

}

Answer:

public void setDifficulty(String difficulty) {
    
    
	if (difficulty.equals("高")||difficulty.equals("中")||difficulty.equals("低")) {
    
    
		this.difficulty = difficulty;
	}
}

[10 points] Step 4: Complete the entity type Score

Now let's complete the Score class.

org.lanqiao.bean.Score is an entity class with three attributes, which correspond to the database table score and its fields one-to-one. The corresponding relationship is as follows.

[The fields of the database table score and their corresponding relationship with the attributes of the entity class Score]

serial number class attributes table fields Database data type Remark
1 takes off takes off VARCHAR
2 cname cname VARCHAR
3 score score INT

Please fill in the missing codes according to the comment requirements.

package org.lanqiao.bean;

public class Score {
    
    
    //请修改该方法,以保证打印对象时输出格式如下:(sname=zs;cname=语文;score=80)
    @Override
    public String toString() {
    
    
        return null;
    }

}

Answer:

@Override
public String toString() {
    
    
	return "(sname="+this.getSname()+";cname="+this.getCname()+";score="+this.getScore()+")";
}

[10 points] Step 5: Complete the add method of StudentDaoImpl

topic:

After the data is stored in the database, we need to perform relevant CRUD to process and display the data. We encapsulate the operations related to students in the database into org.lanqiao.daoimpl.StudentDaoImpl. Please help to complete it.

Please complete the add method according to the comment requirements.

/**
 * 插入学生
 * @param s 学生对象
 * @return
 */
public int add(Student s) {
    
    
    // 请补全sql语句
    String sql = "###";
    return studentUtil.add(sql,s.getName(),s.getPwd(),s.getAge(),s.getGrade(),s.getRate());
}

Answer:

public int add(Student s) {
    
    
        // 请补全sql语句
        String sql = "insert student (name, pwd, age, grade, rate) values (?,?,?,?,?)";
        return studentUtil.add(sql, s.getName(),s.getPwd(),s.getAge(),s.getGrade(),s.getRate());
}

[10 points] Step 6: Complete the queryNum method of StudentDaoImpl

topic:

After the data is stored in the database, we need to perform relevant CRUD to process and display the data. We encapsulate the operations related to students in the database into org.lanqiao.daoimpl.StudentDaoImpl. Please help to complete it.

Please complete the queryNum method according to the annotation requirements.

/**
 * 查询学生总人数
 * @return 返回总人数
 */
public int queryNum() {
    
    
    String sql = "select * from student";
    List<Student> list = studentUtil.getList(sql, Student.class);
    // 请修改以下代码,保证返回值为总人数,假设所有学生名字都不一样
    int num = 0;
    return num;
}

Answer:

public int queryNum() {
    
    
        String sql = "select * from student";
        List<Student> list = studentUtil.getList(sql, Student.class);
        // 请修改以下代码,保证返回值为总人数,假设所有学生名字都不一样
        int num = list.size();
        return num;
}

[10 points] Step 7: Complete the queryMinAge method of StudentDaoImpl

topic:

After the data is stored in the database, we need to perform relevant CRUD to process and display the data. We encapsulate the operations related to students in the database into org.lanqiao.daoimpl.StudentDaoImpl. Please help to complete it.

Please complete the queryMinAge method according to the comment requirements.

/**
 * 查询最小年龄的学生姓名
 * @return 返回学生姓名
 */
public String queryMinAge() {
    
    
    // 请补全sql语句
    String sql = "###";
    Student g = studentUtil.getOne(sql, Student.class);
    return g.getName();
}

Answer:

public String queryMinAge() {
    
    
        // 请补全sql语句
        String sql = "select name from student where age = (select min(age) from student)";
        Student g = studentUtil.getOne(sql, Student.class);
        return g.getName();
}

[10 points] Step 8: Complete the queryCourse method of CourseDaoImpl

topic:

After the data is stored in the database, we need to perform relevant CRUD to process and display the data. We encapsulate the course operations in the database into org.lanqiao.daoimpl.CourseDaoImpl. Please help to complete it.

Please complete the queryCourse method according to the annotation requirements.

/**
 * 根据课程名称来查询课程
 * @return 返回课程对象
 */
public Course queryCourse(String name) {
    
    
    // 请补全sql语句
    String sql = "###";
    return courseUtil.getOne(sql, Course.class, name);
}
        

Answer:

public Course queryCourse(String name) {
    
    
        // 请补全sql语句
        String sql = "select * from course where name=?";
        return courseUtil.getOne(sql, Course.class, name);
}

[10 points] Step 9: Complete the updateDifficultyByName method of CourseDaoImpl

topic:

After the data is stored in the database, we need to perform relevant CRUD to process and display the data. We encapsulate the course operations in the database into org.lanqiao.daoimpl.CourseDaoImpl. Please help to complete it.

Please complete the updateDifficultyByName method according to the comment requirements.

/**
 * 根据课程名称来更新课程难度
 * @return 更新成功返回true,没有更新成功返回false
 */
public boolean updateDifficultyByName(String name,String difficulty){
    
    
    // 请补全sql语句
    String sql = "###";
    int a = courseUtil.update(sql, difficulty, name);
    if(a>0){
    
    
            return true;
    }else{
    
    
            return false;  
    }
}
       

Answer:

public boolean updateDifficultyByName(String name,String difficulty){
    
    
        // 请补全sql语句
        String sql = "update course set difficulty=? where name=?";
        int a = courseUtil.update(sql, difficulty, name);
        if(a>0){
    
    
                return true;
        }else{
    
    
                return false;  
        }
}

[10 points] Step 10: Complete the queryAvgMax method of ScoreDaoImpl

topic:

After the data is stored in the database, we need to perform relevant CRUD to process and display the data. We encapsulate the score-related operations in the database into org.lanqiao.daoimpl.ScoreDaoImpl. Please help to complete it.

Please complete the queryAvgMax method according to the comment requirements.

/**
 * 查询平均成绩最高的学生姓名
 * @return 返回学生姓名
 */
public String queryAvgMax() {
    
    
    // 请补全sql语句
    String sql = "###";
    Score s = scoreUtil.getOne(sql, Score.class);
    return s.getSname();
}

Answer:

public String queryAvgMax() {
    
    
        // 请补全sql语句
        String sql = "select sname from score group by sname order by avg(score) desc limit 1";
        Score s = scoreUtil.getOne(sql, Score.class);
        return s.getSname();
}

[10 points] Step 11: Complete the queryName method of ScoreDaoImpl

topic:

After the data is stored in the database, we need to perform relevant CRUD to process and display the data. We encapsulate the score-related operations in the database into org.lanqiao.daoimpl.ScoreDaoImpl. Please help to complete it.

Please complete the queryName method according to the comment requirements.

/**
 * 查询至少考了2门课程的学生姓名
 * @return 返回所有满足条件的学生姓名的集合
 */
public Set<String> queryName() {
    
    
    // 查询出满足条件的成绩集合
    String sql = "select * from score where sname in(select sname from score group by sname having count(*)>=2)";
    List<Score> li = scoreUtil.getList(sql, Score.class);
    Set<String> s = new HashSet<String>();
    // 把集合 li 中的每个成绩对象的名字取出来放进集合 s 中,并返回
    // 请补全以下代码
    
    
    return s;
}

Answer:

public Set<String> queryName() {
    
    
        // 查询出满足条件的成绩集合
        String sql = "select * from score where sname in(select sname from score group by sname having count(*)>=2)";
        List<Score> li = scoreUtil.getList(sql, Score.class);
        Set<String> s = new HashSet<String>();
        // 把集合 li 中的每个成绩对象的名字取出来放进集合 s 中,并返回
        // 请补全以下代码
        for (Score score : li){
    
    
                s.add(score.getSname());
        }
        
        return s;
}

おすすめ

転載: blog.csdn.net/realoser/article/details/131077408