JavaWeb 增删改查功能

1.今天来写一个简单的增删改查功能,这个项目还是接着在昨天写的 --JavaWeb实现简单登录注册-- 来写的。

登录成功进来后对学生信息的增删改查

JavaWeb 增删改查功能_第1张图片

2.项目文件为:

 JavaWeb 增删改查功能_第2张图片JavaWeb 增删改查功能_第3张图片

 

3.好了,直接贴上今天新写的代码,昨天的代码就不重复贴了。

src文件下的代码:

 controller:

 1 package com.maike.controller;
 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 import com.maike.model.Student;
13 import com.maike.model.StudentDto;
14 import com.maike.service.UserService;
15 import com.maike.service.UserServiceImpl;
16 
17 /**
18  * Servlet implementation class AddStuServlet
19  */
20 @WebServlet("/AddStuServlet")
21 public class AddStuServlet extends HttpServlet {
22     UserService userService = new UserServiceImpl();
23     private static final long serialVersionUID = 1L;
24        
25     /**
26      * @see HttpServlet#HttpServlet()
27      */
28     public AddStuServlet() {
29         super();
30         // TODO Auto-generated constructor stub
31     }
32 
33     /**
34      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
35      */
36     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
37         // TODO Auto-generated method stub
38         response.getWriter().append("Served at: ").append(request.getContextPath());
39     }
40 
41     /**
42      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
43      */
44     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
45         // TODO Auto-generated method stub
46         String stuName = request.getParameter("stuName");
47         String stuSex = request.getParameter("stuSex");
48         String stuAge = request.getParameter("stuAge");
49         String stuClass = request.getParameter("stuClass");
50         
51         StudentDto studentDto = new StudentDto();
52         studentDto.setStuName(stuName);
53         studentDto.setStuSex(stuSex);
54         studentDto.setStuAge(stuAge);
55         studentDto.setStuClass(stuClass);
56         
57         int k = userService.addStu(studentDto);
58         if(k > 0) {
59             List list = userService.seleStuAll();
60             request.setAttribute("stuList", list);
61             request.getRequestDispatcher("showStu.jsp").forward(request, response);
62         }else {
63             response.getWriter().append("添加失败").append(request.getContextPath());
64             request.getRequestDispatcher("addStu.jsp").forward(request, response);
65         }
66     }
67 
68 }
AddStuServlet
 1 package com.maike.controller;
 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 import com.maike.model.Student;
13 import com.maike.service.UserService;
14 import com.maike.service.UserServiceImpl;
15 
16 /**
17  * Servlet implementation class DeleteStuServlet
18  */
19 @WebServlet("/DeleteStuServlet")
20 public class DeleteStuServlet extends HttpServlet {
21     UserService userService = new UserServiceImpl();
22     private static final long serialVersionUID = 1L;
23        
24     /**
25      * @see HttpServlet#HttpServlet()
26      */
27     public DeleteStuServlet() {
28         super();
29         // TODO Auto-generated constructor stub
30     }
31 
32     /**
33      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
34      */
35     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
36         // TODO Auto-generated method stub
37         String stuName = request.getParameter("stuName");
38         int  k = userService.deleteStu(stuName);
39         List list = userService.seleStuAll();
40         request.setAttribute("stuList", list);
41         request.getRequestDispatcher("showStu.jsp").forward(request, response);
42     }
43 
44     /**
45      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
46      */
47     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
48         // TODO Auto-generated method stub
49         doGet(request, response);
50     }
51 
52 }
DeleteStuServlet
 1 package com.maike.controller;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.annotation.WebServlet;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 /**
11  * Servlet implementation class ToAddStuServlet
12  */
13 @WebServlet("/ToAddStuServlet")
14 public class ToAddStuServlet extends HttpServlet {
15     private static final long serialVersionUID = 1L;
16        
17     /**
18      * @see HttpServlet#HttpServlet()
19      */
20     public ToAddStuServlet() {
21         super();
22         // TODO Auto-generated constructor stub
23     }
24 
25     /**
26      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
27      */
28     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
29         // TODO Auto-generated method stub
30         request.getRequestDispatcher("addStu.jsp").forward(request, response);
31         
32     }
33 
34     /**
35      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
36      */
37     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
38         // TODO Auto-generated method stub
39         doGet(request, response);
40     }
41 
42 }
ToAddStuServlet
 1 package com.maike.controller;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.annotation.WebServlet;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 import com.maike.model.Student;
11 import com.maike.service.UserService;
12 import com.maike.service.UserServiceImpl;
13 
14 /**
15  * Servlet implementation class ToUpdateStuServlet
16  */
17 @WebServlet("/ToUpdateStuServlet")
18 public class ToUpdateStuServlet extends HttpServlet {
19     UserService userService = new UserServiceImpl();
20     private static final long serialVersionUID = 1L;
21        
22     /**
23      * @see HttpServlet#HttpServlet()
24      */
25     public ToUpdateStuServlet() {
26         super();
27         // TODO Auto-generated constructor stub
28     }
29 
30     /**
31      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
32      */
33     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
34         // TODO Auto-generated method stub
35         String stuName = request.getParameter("stuName");
36         Student student = userService.getStuByName(stuName);
37         request.setAttribute("student", student);
38         request.getRequestDispatcher("updateStu.jsp").forward(request, response);
39     }
40 
41     /**
42      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
43      */
44     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
45         // TODO Auto-generated method stub
46         doGet(request, response);
47     }
48 
49 }
ToUpdateStuServlet
 1 package com.maike.controller;
 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 import com.maike.model.Student;
13 import com.maike.service.UserService;
14 import com.maike.service.UserServiceImpl;
15 
16 /**
17  * Servlet implementation class UpdateStuServlet
18  */
19 @WebServlet("/UpdateStuServlet")
20 public class UpdateStuServlet extends HttpServlet {
21     UserService userService = new UserServiceImpl();
22     private static final long serialVersionUID = 1L;
23        
24     /**
25      * @see HttpServlet#HttpServlet()
26      */
27     public UpdateStuServlet() {
28         super();
29         // TODO Auto-generated constructor stub
30     }
31 
32     /**
33      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
34      */
35     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
36         // TODO Auto-generated method stub
37         response.getWriter().append("Served at: ").append(request.getContextPath());
38     }
39 
40     /**
41      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
42      */
43     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
44         // TODO Auto-generated method stub
45         request.setCharacterEncoding("utf-8");
46         String stuName = request.getParameter("stuName");
47         String stuSex = request.getParameter("stuSex");
48         String stuClass = request.getParameter("stuClass");
49         Student student = new Student();
50         student.setStuName(stuName);
51         student.setStuSex(stuSex);
52         student.setStuClass(stuClass);
53         int k = userService.updateStu(student);
54         List list = userService.seleStuAll();
55         request.setAttribute("stuList", list);
56         request.getRequestDispatcher("showStu.jsp").forward(request, response);
57     }
58 
59 }
UpdateStuServlet

dao:

 1 package com.maike.dao;
 2 
 3 import java.util.List;
 4 
 5 import com.maike.model.Student;
 6 
 7 public interface StudentDao {
 8     
 9     List selectAll();
10     Student selectByName(String stuName);
11     int insert(Student student);
12     int update(Student student);
13     int delete(String stuName);
14 
15 }
StudentDao
  1 package com.maike.dao;
  2 
  3 import java.sql.Connection;
  4 import java.sql.PreparedStatement;
  5 import java.sql.ResultSet;
  6 import java.sql.SQLException;
  7 import java.util.ArrayList;
  8 import java.util.List;
  9 
 10 import com.maike.model.Student;
 11 import com.maike.util.ConnectionUtil;
 12 
 13 public class StudentDaoImpl implements StudentDao {
 14     Student student;
 15     Connection con = null;
 16     PreparedStatement pt = null;
 17     ResultSet rs = null;
 18     String sql;
 19 
 20     /**
 21      * 查询所有学生
 22      */
 23     @Override
 24     public List selectAll() {
 25         // TODO Auto-generated method stub
 26         List list = new ArrayList();
 27         sql = "select * from student";
 28         try {
 29             con = ConnectionUtil.getConnection();
 30             pt = con.prepareStatement(sql);
 31             rs = pt.executeQuery();
 32             
 33             while(rs.next()) {
 34                 student = new Student();
 35                 student.setStuId(rs.getInt("stu_id"));
 36                 student.setStuName(rs.getString("stu_name"));
 37                 student.setStuSex(rs.getString("stu_sex"));
 38                 student.setStuAge(rs.getInt("stu_age"));
 39                 student.setStuClass(rs.getString("stu_class"));
 40                 
 41                 list.add(student);
 42             }
 43         } catch (SQLException e) {
 44             // TODO Auto-generated catch block
 45             e.printStackTrace();
 46         }finally {
 47             ConnectionUtil.closeRe(con, pt, rs);
 48         }
 49         return list;
 50     }
 51 
 52     /**
 53      * 通过名字查询学生
 54      */
 55     @Override
 56     public Student selectByName(String stuName) {
 57         // TODO Auto-generated method stub
 58         sql = "select * from student where stu_name = ?";
 59         try {
 60             con = ConnectionUtil.getConnection();
 61             pt = con.prepareStatement(sql);
 62             pt.setString(1, stuName);
 63             rs = pt.executeQuery();
 64             
 65             while(rs.next()) {
 66                 student = new Student();
 67                 student.setStuId(rs.getInt("stu_id"));
 68                 student.setStuName(rs.getString("stu_name"));
 69                 student.setStuSex(rs.getString("stu_sex"));
 70                 student.setStuAge(rs.getInt("stu_age"));
 71                 student.setStuClass(rs.getString("stu_class"));
 72             }
 73         } catch (SQLException e) {
 74             // TODO Auto-generated catch block
 75             e.printStackTrace();
 76         }finally {
 77             ConnectionUtil.closeRe(con, pt, rs);
 78         }
 79         return student;
 80     }
 81 
 82     /**
 83      * 添加学生
 84      */
 85     @Override
 86     public int insert(Student student) {
 87         // TODO Auto-generated method stub
 88         int k = 0;
 89         sql = "insert into student values(null,?,?,?,?)";
 90         try {
 91             con = ConnectionUtil.getConnection();
 92             pt = con.prepareStatement(sql);
 93             pt.setString(1, student.getStuName());
 94             pt.setString(2, student.getStuSex());
 95             pt.setInt(3, student.getStuAge());
 96             pt.setString(4, student.getStuClass());
 97             
 98             k = pt.executeUpdate();
 99         } catch (SQLException e) {
100             // TODO Auto-generated catch block
101             e.printStackTrace();
102         }finally {
103             ConnectionUtil.closeRe(con, pt);
104         }
105         return k;
106     }
107 
108     /**
109      * 更新学生
110      */
111     @Override
112     public int update(Student student) {
113         // TODO Auto-generated method stub
114         int k = 0;
115         sql = "update student set stu_sex = ? ,stu_class = ? where stu_name = ?";
116         try {
117             con = ConnectionUtil.getConnection();
118             pt = con.prepareStatement(sql);
119             pt.setString(1, student.getStuSex());
120             pt.setString(2, student.getStuClass());
121             pt.setString(3, student.getStuName());
122             
123             k = pt.executeUpdate();
124         } catch (SQLException e) {
125             // TODO Auto-generated catch block
126             e.printStackTrace();
127         }finally {
128             ConnectionUtil.closeRe(con, pt);
129         }
130         return k;
131     }
132 
133     /**
134      * 通过名字删除学生
135      */
136     @Override
137     public int delete(String stuName) {
138         // TODO Auto-generated method stub
139         int k = 0;
140         sql = "delete from student where stu_name = ?";
141         try {
142             con = ConnectionUtil.getConnection();
143             pt = con.prepareStatement(sql);
144             pt.setString(1, stuName);
145             
146             k = pt.executeUpdate();
147         } catch (SQLException e) {
148             // TODO Auto-generated catch block
149             e.printStackTrace();
150         }finally {
151             ConnectionUtil.closeRe(con, pt);
152         }
153         return k;
154     }
155 
156 }
StudentDaoImpl

model:

 1 package com.maike.model;
 2 
 3 public class Student {
 4     
 5     private int stuId;
 6     
 7     private String stuName;
 8     
 9     private String stuSex;
10     
11     private int stuAge;
12     
13     private String stuClass;
14 
15     public int getStuId() {
16         return stuId;
17     }
18 
19     public void setStuId(int stuId) {
20         this.stuId = stuId;
21     }
22 
23     public String getStuName() {
24         return stuName;
25     }
26 
27     public void setStuName(String stuName) {
28         this.stuName = stuName;
29     }
30 
31     public String getStuSex() {
32         return stuSex;
33     }
34 
35     public void setStuSex(String stuSex) {
36         this.stuSex = stuSex;
37     }
38 
39     public int getStuAge() {
40         return stuAge;
41     }
42 
43     public void setStuAge(int stuAge) {
44         this.stuAge = stuAge;
45     }
46 
47     public String getStuClass() {
48         return stuClass;
49     }
50 
51     public void setStuClass(String stuClass) {
52         this.stuClass = stuClass;
53     }
54 
55     @Override
56     public String toString() {
57         return "Student [stuId=" + stuId + ", stuName=" + stuName + ", stuSex=" + stuSex + ", stuAge=" + stuAge
58                 + ", stuClass=" + stuClass + "]";
59     }
60 
61 }
Student
 1 package com.maike.model;
 2 
 3 public class StudentDto {
 4 
 5     private String stuId;
 6     
 7     private String stuName;
 8     
 9     private String stuSex;
10     
11     private String stuAge;
12     
13     private String stuClass;
14 
15     public String getStuId() {
16         return stuId;
17     }
18 
19     public void setStuId(String stuId) {
20         this.stuId = stuId;
21     }
22 
23     public String getStuName() {
24         return stuName;
25     }
26 
27     public void setStuName(String stuName) {
28         this.stuName = stuName;
29     }
30 
31     public String getStuSex() {
32         return stuSex;
33     }
34 
35     public void setStuSex(String stuSex) {
36         this.stuSex = stuSex;
37     }
38 
39     public String getStuAge() {
40         return stuAge;
41     }
42 
43     public void setStuAge(String stuAge) {
44         this.stuAge = stuAge;
45     }
46 
47     public String getStuClass() {
48         return stuClass;
49     }
50 
51     public void setStuClass(String stuClass) {
52         this.stuClass = stuClass;
53     }
54 
55     @Override
56     public String toString() {
57         return "StudentDto [stuId=" + stuId + ", stuName=" + stuName + ", stuSex=" + stuSex + ", stuAge=" + stuAge
58                 + ", stuClass=" + stuClass + "]";
59     }
60 
61 }
StudentDto

service:

 1 package com.maike.service;
 2 
 3 import java.util.List;
 4 
 5 import com.maike.model.Student;
 6 import com.maike.model.StudentDto;
 7 
 8 public interface UserService {
 9     
10     int insertUser(String userName,String userPwd,String userRePwd);
11     int judgeLogin(String userName,String userPwd);
12     
13     List seleStuAll();
14     int addStu(StudentDto studentDto);
15     int updateStu(Student student);
16     int deleteStu(String stuName);
17     Student getStuByName(String stuName);
18 
19 }
UserService
 1     /**
 2      * seleStuAll:查询所有学生信息. 
3 */ 4 @Override 5 public List seleStuAll() { 6 // TODO Auto-generated method stub 7 return studentDao.selectAll(); 8 } 9 10 /** 11 * addStu:添加学生信息 .
12 */ 13 @Override 14 public int addStu(StudentDto studentDto) { 15 // TODO Auto-generated method stub 16 int k = 0; 17 String stuName = studentDto.getStuName(); 18 String stuAge = studentDto.getStuAge(); 19 if(stuName == null || stuAge == null) { 20 return k; 21 } 22 int IntAge = Integer.parseInt(stuAge); 23 Student student = new Student(); 24 student.setStuName(stuName); 25 student.setStuSex(studentDto.getStuSex()); 26 student.setStuAge(IntAge); 27 student.setStuClass(studentDto.getStuClass()); 28 k = studentDao.insert(student); 29 return k; 30 } 31 32 /** 33 * updateStu:修改学生信息 .
34 */ 35 @Override 36 public int updateStu(Student student) { 37 // TODO Auto-generated method stub 38 return studentDao.update(student); 39 } 40 41 /** 42 * deleteStu :删除学生信息 .
43 */ 44 @Override 45 public int deleteStu(String stuName) { 46 // TODO Auto-generated method stub 47 return studentDao.delete(stuName); 48 } 49 50 /** 51 * 通过名字查询学生 52 */ 53 @Override 54 public Student getStuByName(String stuName) { 55 // TODO Auto-generated method stub 56 return studentDao.selectByName(stuName); 57 }
UserServiceImpl

jsp页面:

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core"%>
 4 
 5 
 6 
 7 
 8 学生信息
 9 
10 
11   
12
13
14

学生信息列表

15 16171819
20 21222324 25 2627282930
姓名性别年龄班级管理
${list.stuName }${list.stuSex}${list.stuAge }${list.stuClass}
31 32
showStu
 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 
 4 
 5 
 6 
 7 添加页面
 8 
 9 
10 
11
12
13

添加页面

14
15 16171819202122232425262728293031
名字
性别
年龄
班级
32
33 34
addStu
 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 
 4 
 5 
 6 
 7 修改页面
 8 
 9 
10 
11
12
13

修改页面

14
15 16171819202122232425262728293031
名字
性别
年龄
班级
32
33 34
updateStu

 

数据库创建语句:

 1 CREATE TABLE nanfeng.student (
 2     stu_id INT NOT NULL AUTO_INCREMENT,
 3     stu_name varchar(100) NULL,
 4     stu_sex varchar(100) NULL,
 5     stu_age INT NULL,
 6     stu_class varchar(100) NULL,
 7     CONSTRAINT student_pk PRIMARY KEY (stu_id)
 8 )
 9 ENGINE=InnoDB
10 DEFAULT CHARSET=utf8
11 COLLATE=utf8_general_ci;

 

你可能感兴趣的:(JavaWeb 增删改查功能)