JavaWeb同步学习笔记之二十六、JavaWeb_MVC案例之查询
程序员文章站
2024-02-26 09:47:58
...
JavaWeb_MVC案例之查询
MVC案例之查询
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- index.jsp页面 -->
<a href="listAllStudent">ListAllStudent</a>
</body>
</html>
students.jsp
<%@page import="com.xs.javaweb.mvc.Student"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- students.jsp页面 -->
<%
List<Student> students = (List<Student>)request.getAttribute("students");
%>
ListAllStudent
<table border="1">
<tr>
<th>flowId</th>
<th>type</th>
<th>idCard</th>
<th>examCard</th>
<th>studentName</th>
<th>location</th>
<th>grade</th>
</tr>
<%
for(Student student: students){
%>
<tr>
<td><%= student.getFlowId() %></td>
<td><%= student.getType() %></td>
<td><%= student.getIdCard() %></td>
<td><%= student.getExamCard() %></td>
<td><%= student.getStudentName() %></td>
<td><%= student.getLocation() %></td>
<td><%= student.getGrade() %></td>
</tr>
<%
}
%>
</table>
</body>
</html>
ListAllStudentServlet.java
package com.xs.javaweb.mvc;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/listAllStudent")
public class ListAllStudentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
//ListAllStudentServlet文件
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
StudentDao studentDao = new StudentDao();
List<Student> students = studentDao.getAll();
request.setAttribute("students",students);
request.getRequestDispatcher("/students.jsp").forward(request, response);
}
}
Student.java
package com.xs.javaweb.mvc;
public class Student {
//Student类
private int flowId;
private int type;
private String idCard;
private String examCard;
private String studentName;
private String location;
private int grade;
/**
* @return the flowId
*/
public int getFlowId() {
return flowId;
}
/**
* @param flowId: the flowId to set
*/
public void setFlowId(int flowId) {
this.flowId = flowId;
}
/**
* @return the type
*/
public int getType() {
return type;
}
/**
* @param type: the type to set
*/
public void setType(int type) {
this.type = type;
}
/**
* @return the idCard
*/
public String getIdCard() {
return idCard;
}
/**
* @param idCard: the idCard to set
*/
public void setIdCard(String idCard) {
this.idCard = idCard;
}
/**
* @return the examCard
*/
public String getExamCard() {
return examCard;
}
/**
* @param examCard: the examCard to set
*/
public void setExamCard(String examCard) {
this.examCard = examCard;
}
/**
* @return the studentName
*/
public String getStudentName() {
return studentName;
}
/**
* @param studentName: the studentName to set
*/
public void setStudentName(String studentName) {
this.studentName = studentName;
}
/**
* @return the location
*/
public String getLocation() {
return location;
}
/**
* @param location: the location to set
*/
public void setLocation(String location) {
this.location = location;
}
/**
* @return the grade
*/
public int getGrade() {
return grade;
}
/**
* @param grade: the grade to set
*/
public void setGrade(int grade) {
this.grade = grade;
}
/**
* @Title: Student
* @Description: TODO
* @param flowId
* @param type
* @param idCard
* @param examCard
* @param studentName
* @param location
* @param grade
*/
public Student(int flowId, int type, String idCard, String examCard, String studentName, String location,
int grade) {
super();
this.flowId = flowId;
this.type = type;
this.idCard = idCard;
this.examCard = examCard;
this.studentName = studentName;
this.location = location;
this.grade = grade;
}
/**
* @Title: Student
* @Description: TODO
*/
public Student() {
super();
}
}
StudentDao.java
package com.xs.javaweb.mvc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class StudentDao {
//StudentDao,使用Oracle数据库
public List<Student> getAll() {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
List<Student> students = new ArrayList<>();
try {
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user = "scott";
String password = "tiger";
Class.forName("oracle.jdbc.driver.OracleDriver");
connection = DriverManager.getConnection(url, user, password);
String sql = "SELECT flow_Id,type,id_Card,exam_Card,student_Name,location,grade FROM examstudent";
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
int flowId = resultSet.getInt(1);
int type = resultSet.getInt(2);
String idCard = resultSet.getString(3);
String examCard = resultSet.getString(4);
String studentName = resultSet.getString(5);
String location = resultSet.getString(6);
int grade = resultSet.getInt(7);
Student student = new Student(flowId, type, idCard, examCard, studentName, location, grade);
students.add(student);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (resultSet != null) {
resultSet.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (preparedStatement != null) {
preparedStatement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return students;
}
}
上一篇: 树莓派4B安装Tomcat服务器