[原创]java WEB学习笔记19:初识MVC 设计模式:查询,删除 练习(理解思想)

Posted jason_zhangz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[原创]java WEB学习笔记19:初识MVC 设计模式:查询,删除 练习(理解思想)相关的知识,希望对你有一定的参考价值。

本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明

本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用

内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。

本人互联网技术爱好者,互联网技术发烧友

微博:伊直都在0221

QQ:951226918

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1.在上一学习笔记中,了解了MVC设计的思想,这一学习笔记,主要手动写一个MVC的查询程序,比较糙,重理解思想

 

2.需求:通过index.jsp 页面发过请求,查询学生的信息,将学生的信息输出到des.jsp页面上;可以删除学生的信息。

 

3.代码结构

  1)index.jsp  : 一个查询的超链接页面;

  2)des.jps     :  显示查询的页面;

  3)success.jsp :删除成功后跳转的页面

  4)ListAllStudentsServlet.java : 负责处理index 页面请求的servlet,同时与dao交互;

  5)DeletStudentServlet.java   :通过传入的flowId 进行删除;

  6)StudentDao.java : 定义方法getA() 用于与数据库交互,查询数据 ,返回结果;deleteByFlowId(int flowId)方法,按照flowId删除相应的学生信息;

  7)Student.java  :bean  同数据库表的字段一致,get set方法,带参,不带餐构造器,用于检查的 toString 方法;

 

 

4.具体代码

 

  1)index.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>index</title>
 8 </head>
 9 <body>
10     <a href="listAllStudents">List All Students</a>
11 
12 </body>
13 </html>

 

   

  2)des.jsp

 

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ page import="java.util.List"%>
 4 <%@ page import="com.jason.testMVC.Student"%>
 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 6 <html>
 7 <head>
 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 9 <title>des</title>
10 </head>
11 <body>
12     <%
13         List<Student> stus = (List<Student>) request
14                 .getAttribute("students");
15 
16         if (stus == null) {
17             System.out.println("stus is null");
18         } else {
19     %>
20     <table border="1" cellpadding="10" cellspacing="0">
21         <tr>
22             <th>FlowId</th>
23             <th>Type</th>
24             <th>IdCard</th>
25             <th>ExamCard</th>
26             <th>StudentName</th>
27             <th>Location</th>
28             <th>Grade</th>
29             <th>Delete</th>
30         </tr>
31         <%
32             for (Student stu : stus) {
33         %>
34         <tr>
35             <td><%=stu.getExamCard()%></td>
36             <td><%=stu.getType()%></td>
37             <td><%=stu.getIdCard()%></td>
38             <td><%=stu.getExamCard()%></td>
39             <td><%=stu.getStudentName()%></td>
40             <td><%=stu.getLocation()%></td>
41             <td><%=stu.getGrade()%></td>
42             <td><a href="deletStudent?flowId=<%=stu.getFlowId() %>"/>Delete</td>   //通过这个种方式,向servlet传入flowId参数
43         </tr>
44         <%    
45                 }
46             }
47         %>
48     </table>
49 
50 </body>
51 </html>

 

 

 

 

  3)success.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     
11     
12     <h1>删除成功</h1>
13     <a href="listAllStudents">List All Students</a>
14     
15     
16 </body>
17 </html>

 

 

 

 

  4)ListAllStudentsServlet.java

 1 package com.jason.testMVC;
 2 
 3 import java.io.IOException;
 4 import java.util.List;
 5 
 6 import javax.servlet.ServletException;
 7 import javax.servlet.annotation.WebServlet;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 
12 /**
13  * Servlet implementation class ListAllStudentsServlet
14  */
15 @WebServlet("/listAllStudents")
16 public class ListAllStudentsServlet extends HttpServlet {
17     private static final long serialVersionUID = 1L;
18 
19     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
20     
21             StudentDao studentDao = new StudentDao();
22             List<Student>  students = studentDao.getAll();
23             
24             request.setAttribute("students", students);
25         
26             request.getRequestDispatcher("/students.jsp").forward(request, response);
27     }
28 
29 }

   

  5)DeletStudentServlet.java 

 1 package com.jason.testMVC;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.ServletException;
 6 import javax.servlet.annotation.WebServlet;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10 
11 import com.sun.org.apache.xalan.internal.xsltc.compiler.sym;
12 
13 /**
14  * Servlet implementation class DeletStudentServlet
15  */
16 
17 
18 @WebServlet("/deletStudent")
19 public class DeletStudentServlet extends HttpServlet {
20     private static final long serialVersionUID = 1L;
21        
22     
23     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
24         
25         String flowIdStr =   request.getParameter("flowId");
26 //    int flowId = Integer.parseInt(flowIdStr);
27 //        System.out.println(flowIdStr);
28         
29         StudentDao  studentDao = new StudentDao();
30         boolean flage = studentDao.deleteByFlowId(Integer.parseInt(flowIdStr));
31         
32         if(flage){
33             request.getRequestDispatcher("/success.jsp").forward(request, response);
34         }else{
35             System.out.println("删除失败");
36         }
37         
38         
39     }
40 
41     
42 
43 }

 

 

  6)StudentDao.java

 

  1 package com.jason.testMVC;
  2 
  3 import java.sql.Connection;
  4 import java.sql.DriverManager;
  5 import java.sql.PreparedStatement;
  6 import java.sql.ResultSet;
  7 import java.sql.SQLException;
  8 import java.util.ArrayList;
  9 import java.util.List;
 10 
 11 public class StudentDao {
 12         
 13     
 14     public List<Student> getAll() {
 15 
 16         List<Student> students = new ArrayList<Student>();
 17         Connection connection = null;
 18         PreparedStatement preparedStatement = null;
 19         ResultSet resultSet = null;
 20 
 21         try {
 22 
 23             String driverClass = "com.mysql.jdbc.Driver";
 24             String url = "jdbc:mysql://127.0.0.1:3306/atguigu";
 25             String user = "root";
 26             String password = "zhangzhen";
 27             // 加载驱动类
 28             Class.forName(driverClass);
 29             connection = DriverManager.getConnection(url, user, password);
 30             
 31             String sql = "SELECT *  FROM student";
 32             
 33             preparedStatement = connection.prepareStatement(sql);
 34             resultSet = preparedStatement.executeQuery();
 35 
 36             while (resultSet.next()) {
 37                 int FlowId = resultSet.getInt(1);
 38                 int type = resultSet.getInt(2);
 39                 String idCard = resultSet.getString(3);
 40                 String examCard = resultSet.getString(4);
 41                 String studentName = resultSet.getString(5);
 42                 String locatoin = resultSet.getString(6);
 43                 int grade = resultSet.getInt(7);
 44 
 45                 Student student = new Student(FlowId, type, idCard, examCard,
 46                         studentName, locatoin, grade);
 47                 
 48                 students.add(student);
 49 
 50             }
 51 
 52 
 53         } catch (Exception e) {
 54             e.printStackTrace();
 55         } finally {
 56 
 57             // 关闭资源
 58             try {
 59                 if (resultSet != null) {
 60                     resultSet.close();
 61                 }
 62             } catch (SQLException e) {
 63                 e.printStackTrace();
 64             }
 65 
 66             try {
 67                 if (preparedStatement != null) {
 68                     preparedStatement.close();
 69                 }
 70             } catch (SQLException e) {
 71                 e.printStackTrace();
 72             }
 73 
 74             try {
 75                 if (connection != null) {
 76                     connection.close();
 77                 }
 78             } catch (SQLException e) {
 79                 e.printStackTrace();
 80             }
 81         }
 82 
 83         return students;
 84     }
 85 
 86     public boolean deleteByFlowId(int flowId){
 87         
 88         Connection connection = null;
 89         PreparedStatement preparedStatement = null;
 90         boolean  flage = false;
 91         
 92         try {
 93 
 94             String driverClass = "com.mysql.jdbc.Driver";
 95             String url = "jdbc:mysql://127.0.0.1:3306/atguigu";
 96             String user = "root";
 97             String password = "zhangzhen";
 98             // 加载驱动类
 99             Class.forName(driverClass);
100             connection = DriverManager.getConnection(url, user, password);
101             
102             String sql = "DELETE  FROM student WHERE FlowID = ?";
103             
104             preparedStatement = connection.prepareStatement(sql);
105             preparedStatement.setInt(1, flowId);
106             int result = preparedStatement.executeUpdate();
107             if(result >= 0){
108                 flage = true;
109             }
110 
111         } catch (Exception e) {
112             e.printStackTrace();
113         } finally {
114 
115             // 关闭资源
116             
117 
118             try {
119                 if (preparedStatement != null) {
120                     preparedStatement.close();
121                 }
122             } catch (SQLException e) {
123                 e.printStackTrace();
124             }
125 
126             try {
127                 if (connection != null) {
128                     connection.close();
129                 }
130             } catch (SQLException e) {
131                 e.printStackTrace();
132             }
133         }
134         
135         return flage;
136     }
137     
138     
139 }

 

 

 

 

 

  6)Student.java

  1 package com.jason.testMVC;
  2 
  3 /**
  4  * 
  5  * @author: jason
  6  * @time:2016年5月24日下午11:31:08
  7  * @description:
  8  */
  9 public class Student {
 10     private int flowId;
 11     
 12     private int type;
 13     
 14     private[原创]java WEB学习笔记20:案例完整实践(part 1)---MVC架构分析

[原创]java WEB学习笔记26:MVC案例完整实践(part 7)---修改的设计和实现

[原创]java WEB学习笔记24:MVC案例完整实践(part 5)---删除操作的设计与实现

[原创]java WEB学习笔记25:MVC案例完整实践(part 6)---新增操作的设计与实现

[原创]java WEB学习笔记11:HttpServlet

[原创]java WEB学习笔记10:GenericServlet