从 JOIN table 到 JSP 检索数据
Retrieve data from a JOIN table to JSP
我正在学校开发一个基本的 Web 应用程序,我正在创建一个包含 tables [学生、员工、课程和管理(这个 table 旨在加入其他三个 tables)],
当我尝试显示来自 JOIN table 的寄存器时,我的问题出现了,我相信我的错误在于如何从 JSP、[= 上的数组列表中检索数据17=]
DAO 方法
public ArrayList<Administration> retreive_course_register(String course_id) {
Administration register = null;
Student student = null;
Course course = null;
Employee employee = null;
ArrayList<Administration> register_of_course = new ArrayList<Administration>();
PreparedStatement statement;
try {
statement = connection.prepareStatement(" SELECT s.student_id, s.first_name, s.last_name,s.date_of_birth,s.email,s.password, " +
" e.employee_id, e.first_name , e.last_name , e.email, e.hire_date , e.role, e.password, " +
" c.course_id, c.course_name, c.class_room , c.start_date," +
" a.schedule " +
" FROM administration a " +
" JOIN students s " +
" ON (a.student_id = a.student_id) " +
" JOIN employees e" +
" ON (a.employee_id = e.employee_id) " +
" JOIN courses c " +
" ON (a.course_id = c.course_id) " +
" WHERE c.course_id = ? ");
statement.setString(1, course_id);
ResultSet result = statement.executeQuery();
while (result.next()) {
register = new Administration();
student = new Student();
course = new Course();
employee = new Employee();
student.setStudentID(result.getString("student_id"));
student.setFirstName(result.getString("first_name"));
student.setLastName(result.getString("last_name"));
student.setDateOfBirth(result.getString("date_of_birth"));
student.setEmail(result.getString("email"));
student.setPassword(result.getString("password"));
course.setCourseID(result.getString("course_id"));
course.setCourseName(result.getString("course_name"));
course.setClassRoom(result.getInt("class_room"));
course.setStartDate(result.getString("start_date"));
employee.setEmployeeID(result.getString("employee_id"));
employee.setFirstName(result.getString("first_name"));
employee.setLastName(result.getString("last_name"));
employee.setEmail(result.getString("email"));
employee.setHireDate(result.getString("hire_date"));
employee.setRole(result.getString("role"));
employee.setPassword(result.getString("password"));
register.setStudent(student);
register.setCourse(course);
register.setEmployee(employee);
register.setSchedule(result.getString("schedule"));
register_of_course.add(register);
}
} catch (SQLException e) {
e.printStackTrace();
}
return register_of_course;
}
控制器
@WebServlet("/AdministrationController")
public class AdministrationController extends HttpServlet {
// Declaring variables
private static final long serialVersionUID = 1L;
private static String LIST_OF_REGISTERS = "/Registration.jsp";
private static String STUDENT_REGISTERS = "/Registers_student.jsp";
private static String COURSE_REGISTERS = "/Registers_course.jsp";
private static String EMPLOYEE_REGISTERS = "/Registers_employee.jsp";
private CourseDAOInterface course_dao;
private AdministrationDAOInterface register_dao;// Setting the variable administration DAO to make it a global one
private String action; // the string will keep the parameter obtained from the request
// and based on user´s election will call the desired method
public AdministrationController() throws ServletException, IOException, ClassNotFoundException {
super();
register_dao = new AdministrationDAOImplementation();
course_dao = new CourseDAOImplementation();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
action = request.getParameter("action");
if (action.equalsIgnoreCase("retreive_course_registers")) {
search_course(request,response);
retreive_course_registered(request,response);
RequestDispatcher view = request.getRequestDispatcher(COURSE_REGISTERS);
view.forward(request, response);
}
public void retreive_course_registered (HttpServletRequest request, HttpServletResponse response) {
ArrayList<Administration> registers_course;
String courseID = request.getParameter("course_id");
registers_course = register_dao.retreive_course_register(courseID);
request.setAttribute("Registers_course",registers_course);
}
public void search_course(HttpServletRequest request, HttpServletResponse response) {
String courseID = request.getParameter("course_id");//Retrieving the value from the web form
request.setAttribute("course", course_dao.readCourse(courseID));//showing the course selected
}
}
JSP
<form method="post" action="AdministrationController" >
<table>
<tr>
<td> <h2>Please introduce the course ID:</h2> </td>
<td><input type="text" name="course_id" value="${course.courseID}" required/></td>
<td><input type="submit" value="Submit"></td>
<td><input type="hidden" name="action" value="retreive_course_registers"></td>
</tr>
<tr> <td> <b>Course name :</b> <c:out value="${course.courseName}" /> </td> </tr>
<tr> <td> <b>Start Date : </b> <c:out value="${course.startDate}" /> </td> </tr>
<tr> <td> <b>Classroom : </b> <c:out value="${course.classRoom}" /> </td> </tr>
</table>
<table border=1 style="text-align:center;">
<thead>
<tr>
<th>Student ID</th>
<th>Student Name</th>
<th>Schedule</th>
<th>Teacher Name</th>
<th>Teacher ID</th>
</tr>
</thead>
<tbody>
<c:forEach items="${Registers_course}" var="registers_course">
<tr>
<td>${student.studentID}</td>
<td>${student.firstName} ${student.lastName}</td>
<td>${administration.schedule}</td>
<td>${teacher.firstName} ${teacher.firstName}</td>
<td>${teacher.employee_ID}</td>
</tr>
</c:forEach>
</tbody>
</table>
</form>
</body>
</html>
我的输出
我应该先调用 registers_course 包含学生对象的数组
<table>
<thead>
<tr>
<th>Student ID</th>
<th>Student Name</th>
<th>Schedule</th>
<th>Teacher Name</th>
<th>Teacher ID</th>
</tr>
</thead>
<tbody>
<c:forEach items="${Registers_course}" var="registers_course">
<tr>
<td>${registers_course.student.studentID}</td>
<td>${registers_course.student.firstName}
${registers_course.student.lastName}</td>
<td>${registers_course.schedule}</td>
<td>${registers_course.employee.firstName}
${registers_course.employee.lastName}</td>
<td>${registers_course.employee.employeeID}</td>
</tr>
</c:forEach>
</tbody>
我正在学校开发一个基本的 Web 应用程序,我正在创建一个包含 tables [学生、员工、课程和管理(这个 table 旨在加入其他三个 tables)],
当我尝试显示来自 JOIN table 的寄存器时,我的问题出现了,我相信我的错误在于如何从 JSP、[= 上的数组列表中检索数据17=]
DAO 方法
public ArrayList<Administration> retreive_course_register(String course_id) {
Administration register = null;
Student student = null;
Course course = null;
Employee employee = null;
ArrayList<Administration> register_of_course = new ArrayList<Administration>();
PreparedStatement statement;
try {
statement = connection.prepareStatement(" SELECT s.student_id, s.first_name, s.last_name,s.date_of_birth,s.email,s.password, " +
" e.employee_id, e.first_name , e.last_name , e.email, e.hire_date , e.role, e.password, " +
" c.course_id, c.course_name, c.class_room , c.start_date," +
" a.schedule " +
" FROM administration a " +
" JOIN students s " +
" ON (a.student_id = a.student_id) " +
" JOIN employees e" +
" ON (a.employee_id = e.employee_id) " +
" JOIN courses c " +
" ON (a.course_id = c.course_id) " +
" WHERE c.course_id = ? ");
statement.setString(1, course_id);
ResultSet result = statement.executeQuery();
while (result.next()) {
register = new Administration();
student = new Student();
course = new Course();
employee = new Employee();
student.setStudentID(result.getString("student_id"));
student.setFirstName(result.getString("first_name"));
student.setLastName(result.getString("last_name"));
student.setDateOfBirth(result.getString("date_of_birth"));
student.setEmail(result.getString("email"));
student.setPassword(result.getString("password"));
course.setCourseID(result.getString("course_id"));
course.setCourseName(result.getString("course_name"));
course.setClassRoom(result.getInt("class_room"));
course.setStartDate(result.getString("start_date"));
employee.setEmployeeID(result.getString("employee_id"));
employee.setFirstName(result.getString("first_name"));
employee.setLastName(result.getString("last_name"));
employee.setEmail(result.getString("email"));
employee.setHireDate(result.getString("hire_date"));
employee.setRole(result.getString("role"));
employee.setPassword(result.getString("password"));
register.setStudent(student);
register.setCourse(course);
register.setEmployee(employee);
register.setSchedule(result.getString("schedule"));
register_of_course.add(register);
}
} catch (SQLException e) {
e.printStackTrace();
}
return register_of_course;
}
控制器
@WebServlet("/AdministrationController")
public class AdministrationController extends HttpServlet {
// Declaring variables
private static final long serialVersionUID = 1L;
private static String LIST_OF_REGISTERS = "/Registration.jsp";
private static String STUDENT_REGISTERS = "/Registers_student.jsp";
private static String COURSE_REGISTERS = "/Registers_course.jsp";
private static String EMPLOYEE_REGISTERS = "/Registers_employee.jsp";
private CourseDAOInterface course_dao;
private AdministrationDAOInterface register_dao;// Setting the variable administration DAO to make it a global one
private String action; // the string will keep the parameter obtained from the request
// and based on user´s election will call the desired method
public AdministrationController() throws ServletException, IOException, ClassNotFoundException {
super();
register_dao = new AdministrationDAOImplementation();
course_dao = new CourseDAOImplementation();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
action = request.getParameter("action");
if (action.equalsIgnoreCase("retreive_course_registers")) {
search_course(request,response);
retreive_course_registered(request,response);
RequestDispatcher view = request.getRequestDispatcher(COURSE_REGISTERS);
view.forward(request, response);
}
public void retreive_course_registered (HttpServletRequest request, HttpServletResponse response) {
ArrayList<Administration> registers_course;
String courseID = request.getParameter("course_id");
registers_course = register_dao.retreive_course_register(courseID);
request.setAttribute("Registers_course",registers_course);
}
public void search_course(HttpServletRequest request, HttpServletResponse response) {
String courseID = request.getParameter("course_id");//Retrieving the value from the web form
request.setAttribute("course", course_dao.readCourse(courseID));//showing the course selected
}
}
JSP
<form method="post" action="AdministrationController" >
<table>
<tr>
<td> <h2>Please introduce the course ID:</h2> </td>
<td><input type="text" name="course_id" value="${course.courseID}" required/></td>
<td><input type="submit" value="Submit"></td>
<td><input type="hidden" name="action" value="retreive_course_registers"></td>
</tr>
<tr> <td> <b>Course name :</b> <c:out value="${course.courseName}" /> </td> </tr>
<tr> <td> <b>Start Date : </b> <c:out value="${course.startDate}" /> </td> </tr>
<tr> <td> <b>Classroom : </b> <c:out value="${course.classRoom}" /> </td> </tr>
</table>
<table border=1 style="text-align:center;">
<thead>
<tr>
<th>Student ID</th>
<th>Student Name</th>
<th>Schedule</th>
<th>Teacher Name</th>
<th>Teacher ID</th>
</tr>
</thead>
<tbody>
<c:forEach items="${Registers_course}" var="registers_course">
<tr>
<td>${student.studentID}</td>
<td>${student.firstName} ${student.lastName}</td>
<td>${administration.schedule}</td>
<td>${teacher.firstName} ${teacher.firstName}</td>
<td>${teacher.employee_ID}</td>
</tr>
</c:forEach>
</tbody>
</table>
</form>
</body>
</html>
我的输出
我应该先调用 registers_course 包含学生对象的数组
<table>
<thead>
<tr>
<th>Student ID</th>
<th>Student Name</th>
<th>Schedule</th>
<th>Teacher Name</th>
<th>Teacher ID</th>
</tr>
</thead>
<tbody>
<c:forEach items="${Registers_course}" var="registers_course">
<tr>
<td>${registers_course.student.studentID}</td>
<td>${registers_course.student.firstName}
${registers_course.student.lastName}</td>
<td>${registers_course.schedule}</td>
<td>${registers_course.employee.firstName}
${registers_course.employee.lastName}</td>
<td>${registers_course.employee.employeeID}</td>
</tr>
</c:forEach>
</tbody>