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; } }
时间: 2024-02-10 11:22:24 浏览: 65
这是一个 Java 类,名为 StudentBuy,它代表了学生购买书籍的记录。该类包含了学生学号(studentNo)、学生姓名(studentName)、书籍 ISBN(isbn)、书籍名称(bookName)和购买时间(createTime)等属性。该类还覆盖了 toString 方法,用于将该对象转化为字符串以便打印输出。该类的属性都提供了 get 和 set 方法,用于获取和设置属性的值。
相关问题
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; }
这是一个Java类,名为StudentBuyDao,其中包含一个getBuyCourseList(String studentNo)方法。这个方法从数据库中获取指定学生购买的所有课程的信息,并将它们存储在一个List<StudentBuy>对象中。在方法中,它首先获取一个数据库连接,然后通过执行一个带有参数的SQL查询语句来获取指定学生的购买记录。接着,它使用一个while循环来遍历所有的结果集,并将每个购买记录的信息存储在一个StudentBuy对象中。最后,它将每个StudentBuy对象添加到一个List<StudentBuy>对象中,并返回该对象。需要注意的是,在方法的最后,它关闭了所有的数据库连接和资源,以确保正确的释放资源。如果执行过程中出现异常,则会打印异常信息,并返回null值。
@WebServlet("/admin/addBuyCourse") public class AddBuyCourseController extends HttpServlet { private static final long serialVersionUID = 1L; public AddBuyCourseController() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String isbn = request.getParameter("isbn"); String bookName = request.getParameter("bookName"); User user = (User) request.getSession().getAttribute("loginUser"); Date createTime = new Date(); StudentBuy buy = new StudentBuy(); buy.setIsbn(isbn); buy.setBookName(bookName); String studentNo = user.getUsername(); String username = user.getDisplayName(); buy.setStudentNo(studentNo); buy.setStudentName(username); buy.setCreateTime(createTime); CoursesService courseService = new CoursesService(); ScoreService scoreService = new ScoreService(); Score query = scoreService.getISBN(isbn); if(query != null) { request.setAttribute("msg", "isbn为"+isbn+"的教材已经存在"); request.getRequestDispatcher("/WEB-INF/pages/error.jsp").forward(request, response); return; } // courseService.addStudentBuy(buy); request.getRequestDispatcher("/coursesUrl").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
这是一个Java Servlet类,用于处理添加用户购买课程的请求。它接收来自客户端的HTTP请求,并从请求中获取必要的参数,如书的ISBN、书名等。接着,它从HTTP会话中获取当前登录用户的信息,并将这些信息保存到一个StudentBuy对象中。然后,它通过调用CoursesService类的addStudentBuy()方法将StudentBuy对象保存到数据库中。如果ISBN已经存在,则会将错误消息设置为请求属性,并将请求转发到错误页面。最后,它将请求转发到另一个URL,以便显示用户购买的课程。
阅读全文