public List<StudentBuy> getBuyCourseList(String studentNo) { Connection con = null; PreparedStatement pre = null; ResultSet resultSet = null; try { con = DBUtils.getConnection(); String sql = "select * from tb_student_buy where student_no = ?"; pre = con.prepareStatement(sql); pre.setString(1, studentNo); resultSet = pre.executeQuery(); List<StudentBuy> studentBuyList = new ArrayList<>(); while (resultSet.next()) { String studentNo1 = resultSet.getString("student_no"); String studentName = resultSet.getString("student_name"); String isbn = resultSet.getString("isbn"); String bookName = resultSet.getString("book_name"); Date createTime = resultSet.getDate("create_time"); StudentBuy buys = new StudentBuy(); buys.setStudentNo(studentNo1); buys.setStudentName(studentName); buys.setBookName(bookName); buys.setIsbn(isbn); buys.setCreateTime(createTime); studentBuyList.add(buys); } return studentBuyList; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBUtils.closeConnection(con, pre, resultSet); } return null; }
时间: 2024-02-14 13:24:20 浏览: 35
这是一个Java类,名为StudentBuyDao,其中包含一个getBuyCourseList(String studentNo)方法。这个方法从数据库中获取指定学生购买的所有课程的信息,并将它们存储在一个List<StudentBuy>对象中。在方法中,它首先获取一个数据库连接,然后通过执行一个带有参数的SQL查询语句来获取指定学生的购买记录。接着,它使用一个while循环来遍历所有的结果集,并将每个购买记录的信息存储在一个StudentBuy对象中。最后,它将每个StudentBuy对象添加到一个List<StudentBuy>对象中,并返回该对象。需要注意的是,在方法的最后,它关闭了所有的数据库连接和资源,以确保正确的释放资源。如果执行过程中出现异常,则会打印异常信息,并返回null值。
相关问题
public class CoursesService { /** * 获取列表 * @return */ public List<Courses> getCourseList() { CoursesDao coursesDao = new CoursesDao(); return coursesDao.getCourseList(); } /** * 获取已购教材 * @return */ public List<StudentBuy> getBuyCourseList(String studentNo) { CoursesDao coursesDao = new CoursesDao(); return coursesDao.getBuyCourseList(studentNo); } /** * 通过isbn获取教材 * @return */ public Courses getISBN(String isbn) { CoursesDao courseDao = new CoursesDao(); Courses course = courseDao.getISBN(isbn); return course; } /** * 添加 * @return */ public Courses addCourse(Courses course) { CoursesDao courseDao = new CoursesDao(); courseDao.addCourse(course); return courseDao.getISBN(course.getIsbn()); } /** * 更新 * @param course */ public void updateCourse(Courses course) { CoursesDao courseDao = new CoursesDao(); courseDao.updateCourse(course); } public void addStudentBuy(StudentBuy buy) { CoursesDao courseDao = new CoursesDao(); courseDao.addStudentBuy(buy); } }
这段代码是一个Java类,它定义了一个CoursesService服务类,用于提供与教材课程相关的服务。其中包括获取教材列表、获取已购教材列表、通过ISBN获取教材、添加教材、更新教材信息等方法。这些方法通过调用CoursesDao类中的方法来实现具体的业务逻辑,例如获取教材列表会调用CoursesDao的getCourseList方法。该类的方法返回类型都是Courses或List<Courses>或List<StudentBuy>,表示返回的数据是与教材相关的对象或对象列表。
public class StudentBuy { private String studentNo; private String studentName; private String isbn; private String bookName; private Date createTime; @Override public String toString() { return "StudentBuy{" + "studentNo='" + studentNo + '\'' + ", studentName='" + studentName + '\'' + ", isbn='" + isbn + '\'' + ", bookName='" + bookName + '\'' + ", createTime=" + createTime + '}'; } public String getStudentNo() { return studentNo; } public void setStudentNo(String studentNo) { this.studentNo = studentNo; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public String getIsbn() { return isbn; } public void setIsbn(String isbn) { this.isbn = isbn; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } }
这是一个 Java 类,名为 StudentBuy,它代表了学生购买书籍的记录。该类包含了学生学号(studentNo)、学生姓名(studentName)、书籍 ISBN(isbn)、书籍名称(bookName)和购买时间(createTime)等属性。该类还覆盖了 toString 方法,用于将该对象转化为字符串以便打印输出。该类的属性都提供了 get 和 set 方法,用于获取和设置属性的值。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.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)