public class ScoreService { public List<Score> getScoreList() { ScoreDao scoreDao = new ScoreDao(); return scoreDao.getScoreList(); } public void addScore(Score score) { ScoreDao scoreDao = new ScoreDao(); scoreDao.addScore(score); } public Score getISBN(String isbn) { Connection con = null; PreparedStatement pre = null; ResultSet resultSet = null; try { con = DBUtils.getConnection(); String sql = "select * from tb_student_buy where isbn = ?"; pre = con.prepareStatement(sql); pre.setString(1, isbn); resultSet = pre.executeQuery(); while (resultSet.next()) { String isbn1 = resultSet.getString("isbn"); Score score = new Score(); score.setIsbn(isbn1); return score; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBUtils.closeConnection(con, pre, resultSet); } return null; } }
时间: 2024-02-14 07:24:03 浏览: 43
这是一个 Java 类,名为 ScoreService,它提供了一些操作成绩信息的方法。其中,getScoreList() 方法用于获取所有的成绩列表,它通过实例化 ScoreDao 对象,调用其 getScoreList() 方法来实现。addScore(Score score) 方法用于添加一条成绩信息,它通过实例化 ScoreDao 对象,调用其 addScore(Score score) 方法来实现。getISBN(String isbn) 方法用于根据 ISBN 编号获取成绩信息,它通过 JDBC 连接数据库,执行一条查询语句,获取符合条件的成绩信息,将其转化为 Score 对象并返回。需要注意的是,该方法最终返回的是一个 Score 对象,而不是成绩列表。
相关问题
public class ScoreDao { // public List<Score> getScoreList() { Connection con = null; PreparedStatement pre = null; ResultSet resultSet = null; List<Score> res = new ArrayList<>(); try { con = DBUtils.getConnection(); String sql = "select * from tb_score"; pre = con.prepareStatement(sql); resultSet = pre.executeQuery(); while (resultSet.next()) { Score score = new Score(); score.setIsbn(resultSet.getString("isbn")); score.setCourseName(resultSet.getString("course_name")); score.setClassName(resultSet.getString("class_name")); score.setNum(resultSet.getInt("num")); res.add(score); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBUtils.closeConnection(con, pre, resultSet); } return res; } public boolean addScore(Score score) { Connection con = null; PreparedStatement pre = null; ResultSet resultSet = null; try { con = DBUtils.getConnection(); String sql = "insert into tb_score(isbn, course_name, class_name, num) values(?,?,?,?)"; pre = con.prepareStatement(sql); pre.setString(1, score.getIsbn()); pre.setString(2, score.getCourseName()); pre.setString(3, score.getClassName()); pre.setInt(4, score.getNum()); return pre.execute(); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtils.closeConnection(con, pre, resultSet); } return false; } }
这段代码是一个名为ScoreDao的类,其中包含了两个方法:getScoreList和addScore。getScoreList方法用于从数据库中获取所有的成绩信息,返回一个Score对象列表。addScore方法用于向数据库中插入一条新的成绩信息,接受一个Score对象作为参数,返回一个布尔值表示插入操作是否成功。该类使用了DBUtils类来获取数据库连接和关闭连接。
我们将使用如下函数检验你的函数: import java.util.*; public class Main { public static class Student { public String name; public String id; public double score; } /* 此区间是要编写的函数 */ public static List<Student> filter(List<Student> students) { //请补充完整 } /* 此区间是要编写的函数 */ public static void main(String[] args) { Scanner key = new Scanner(System.in); int n = key.nextInt(); List<Student> list = /*接收数据输入,此部分不可见*/; List<Student> result = filter(list); for (Student s : result) { System.out.printf("%s %s %.1f\n", s.id, s.name, s.score); } } }
好的,那我来给你提供一份完整的代码,你可以使用这个代码来检验你的函数是否正确:
```java
import java.util.*;
public class Main {
public static class Student {
public String name;
public String id;
public double score;
}
public static List<Student> filter(List<Student> students) {
List<Student> excellentStudents = new ArrayList<>();
for (Student student : students) {
if (student.score >= 90 && student.score <= 100) {
excellentStudents.add(student);
}
}
return excellentStudents;
}
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
int n = key.nextInt();
List<Student> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
Student student = new Student();
student.id = key.next();
student.name = key.next();
student.score = key.nextDouble();
list.add(student);
}
List<Student> result = filter(list);
for (Student s : result) {
System.out.printf("%s %s %.1f\n", s.id, s.name, s.score);
}
}
}
```
这份代码中,我们定义了一个 `Student` 类,包含学生的姓名、学号和成绩。我们的 `filter` 函数与之前的示例代码实现相同,接收一个 `List<Student>` 类型的参数 `students`,遍历 `students` 中的每一个学生,如果该学生的成绩大于等于 90 并且小于等于 100,则将该学生加入到结果列表 `excellentStudents` 中。最后将 `excellentStudents` 返回即可。
在 `main` 函数中,我们首先读入一个整数 `n`,表示学生的数量。接下来读入 `n` 行数据,每行数据包含一个学生的学号、姓名和成绩。然后我们调用 `filter` 函数,将 `list` 作为参数传入。最后遍历 `result` 中的每一个学生,按照格式输出学号、姓名和成绩即可。
你可以根据这份代码来检验你的函数是否正确。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)