JavaWeb同步学习笔记之二十七、JavaWeb_MVC案例之删除
程序员文章站
2024-02-26 10:01:28
...
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 void deleteStudent(Integer flowID) {
Connection connection = null;
PreparedStatement preparedStatement = null;
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 = "DELETE FROM examstudent WHERE flow_Id = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, flowID);
preparedStatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (preparedStatement != null) {
preparedStatement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//查询操作
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;
}
}
success.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>
操作成功!
<br><br>
<a href="listAllStudent">ListAllStudent</a>
</body>
</html>
DeleteStudentServlet .java
package com.xs.javaweb.mvc;
import java.io.IOException;
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("/deleteStudent")
public class DeleteStudentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
StudentDao studentDao = new StudentDao();
String flowId = request.getParameter("flowId");
studentDao.deleteStudent(Integer.parseInt(flowId));
request.getRequestDispatcher("/success.jsp").forward(request, response);
}
}
MVC小结及当前设计存在的问题
- 1.关于 MVC:
1)M:Model。Dao
2)V:View。JSP,在页面上填写 Java 代码实现显示
3)C:Controller。Serlvet:
①受理请求
②获取请求参数
③调用 DAO 方法
④可能会把 DAO 方法的返回值放入 request 中
⑤转发(或重定向)页面 - 2.什么时候转发,什么时候重定向 ? 若目标的响应页面不需要从 request 中读取任何值,则可以使用重定向。(还可以防止表单的重复提交)
- 3.不足:
1)使用数据库连接池,DBUtils,JDBCUtils 工具类,DAO 基类。
2)一个请求一个 Serlvet 不好!一个模块使用一个 Serlvet,即多个请求可以使用一个 Servlet 。
3)在页面上加入 jQuery 提示。
上一篇: JavaWeb_MVC架构_:)
下一篇: (树莓派3b)OpenCV 安装教程