动态网站项目(Dynamic Web Project)CRUD(增删改查)功能的实现(mvc(五层架构)+jdbc+servlet+tomcat7.0+jdk1.8),前端使用JSP+JSTL+EL组
代码分享链接
https://pan.baidu.com/s/1UM0grvpttHW9idisiqa6rA 提取码:hx7c
图示
项目结构
1.SelectAllUser.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3
4
5
6
7 Insert title here
8
9
10 查询所有用户
11
12
2.AddUser.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
增加用户
用户ID | 用户姓名 | 用户密码 | 用户昵称 | 用户性别 | 用户年龄 | 用户头像 | 操作 |
${xyz.userid } | ${xyz.username } | ${xyz.password } | ${xyz.nikename } | ${xyz.sex } | ${xyz.age } | 修改 删除 |
一共${num }页
首页
上一页
下一页
尾页
https://www.yht7.com/news/Add.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3
4
5
6
7 Insert title here
8
9
10
19
20
4.UpdateUser.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
4
5
6
7
8 Insert title here
9
10
11
12
13
24
25
26
5.(com.icss.vo)User.java
1
10 package com.icss.vo;
11
12
18 public class User {
19 //数据封装
20 private int userid;
21 private String username;
22 private String password;
23 private String nikename;
24 private String sex;
25 private int age;
26 private String image;
27
30 public int getUserid() {
31 return userid;
32 }
33
36 public void setUserid(int userid) {
37 this.userid = userid;
38 }
39
42 public String getUsername() {
43 return username;
44 }
45
48 public void setUsername(String username) {
49 this.username = username;
50 }
51
54 public String getPassword() {
55 return password;
56 }
57
60 public void setPassword(String password) {
61 this.password = password;
62 }
63
66 public String getNikename() {
67 return nikename;
68 }
69
72 public void setNikename(String nikename) {
73 this.nikename = nikename;
74 }
75
78 public String getSex() {
79 return sex;
80 }
81
84 public void setSex(String sex) {
85 this.sex = sex;
86 }
87
90 public int getAge() {
91 return age;
92 }
93
96 public void setAge(int age) {
97 this.age = age;
98 }
99
102 public String getImage() {
103 return image;
104 }
105
108 public void setImage(String image) {
109 this.image = image;
110 }
111
112
113
114 }
6.(com.icss.util)DBUtil.java
1 package com.icss.util;
2
3 import java.sql.Connection;
4 import java.sql.DriverManager;
5 import java.sql.SQLException;
6
7 public class DBUtil {
8 public static Connection connection;
9 public static Connection getConnection() throws ClassNotFoundException, SQLException{
10 Class.forName("oracle.jdbc.driver.OracleDriver");
11 Connection connection = DriverManager.getConnection
12 ("jdbc:oracle:thin:@localhost:1521:XE","数据库名","数据库密码");
13 return connection;
14 }
15
16 }
7.(com.icss.dao)UserDao.java
1
10 package com.icss.dao;
11
12 import java.sql.PreparedStatement;
13 import java.sql.ResultSet;
14 import java.sql.SQLException;
15 import java.util.ArrayList;
16
17 import org.apache.tomcat.dbcp.dbcp.DbcpException;
18
19 import com.icss.util.DBUtil;
20 import com.icss.vo.User;
21
22 import javafx.css.PseudoClass;
23
24
30 public class UserDao {
31 //查询所有用户
32 public ArrayList selectAllUserDao() throws ClassNotFoundException, SQLException{
33 PreparedStatement ps = DBUtil.getConnection().prepareStatement
34 ("select * from user_0327");
35 ResultSet rs = ps.executeQuery();
36 ArrayList al = new ArrayList();
37 while(rs.next()){
38 User user = new User();
39 user.setUserid(rs.getInt(1));
40 user.setUsername(rs.getString(2));
41 user.setPassword(rs.getString(3));
42 user.setNikename(rs.getString(4));
43 user.setSex(rs.getString(5));
44 user.setAge(rs.getInt(6));
45 user.setImage(rs.getString(7));
46 al.add(user);
47 }
48 ps.close();
49 rs.close();
50 DBUtil.getConnection().close();
51 return al;
52 }
53 //增加用户
54 public void addUserDao(User user) throws ClassNotFoundException, SQLException{
55 PreparedStatement ps = DBUtil.getConnection().prepareStatement
56 ("insert into user_0327 values(user0327_seq.nextval,?,?,?,?,?,?)");
57 ps.setString(1, user.getUsername());
58 ps.setString(2, user.getPassword());
59 ps.setString(3, user.getNikename());
60 ps.setString(4, user.getSex());
61 ps.setInt(5, user.getAge());
62 //这里先将头像照片定死,没有使用上传图片
63 ps.setString(6, "image/"+user.getImage());
64 ps.executeUpdate();
65 ps.close();
66 DBUtil.getConnection().close();
67 }
68 //删除用户
69 public void deleteUserDao(int id) throws ClassNotFoundException, SQLException{
70 PreparedStatement ps = DBUtil.getConnection().prepareStatement
71 ("delete from user_0327 where userid=?");
72 ps.setInt(1, id);
73 ps.executeQuery();
74 ps.close();
75 DBUtil.getConnection().close();
76 }
77 //修改之查询
78 public User updateSelectUserByIdDao(int id) throws ClassNotFoundException, SQLException{
79 PreparedStatement ps = DBUtil.getConnection().prepareStatement
80 ("select * from user_0327 where userid=?");
81 ps.setInt(1, id);
82 ResultSet rs = ps.executeQuery();
83 User user = new User();
84 while(rs.next()){
85 user.setUserid(rs.getInt(1));
86 user.setUsername(rs.getString(2));
87 user.setPassword(rs.getString(3));
88 user.setNikename(rs.getString(4));
89 user.setSex(rs.getString(5));
90 user.setAge(rs.getInt(6));
91 user.setImage(rs.getString(7));
92 }
93 ps.close();
94 rs.close();
95 DBUtil.getConnection().close();
96 return user;
97 }
98 //修改之修改
99 public void updateUserByIdDao(User user) throws ClassNotFoundException, SQLException{
100 PreparedStatement ps = DBUtil.getConnection().prepareStatement
101 ("update user_0327 set username=?,password=?,nikename=?,sex=?,age=?,image=? where userid =?");
102 ps.setString(1, user.getUsername());
103 ps.setString(2, user.getPassword());
104 ps.setString(3, user.getNikename());
105 ps.setString(4, user.getSex());
106 ps.setInt(5, user.getAge());
107 ps.setString(6, "image"+user.getImage());
108 ps.setInt(7, user.getUserid());
109 ps.executeUpdate();
110 ps.close();
111 DBUtil.getConnection().close();
112 }
113 //获得分页数量
114 public int getTotalPageDao(int pagesize) throws ClassNotFoundException, SQLException{
115 PreparedStatement ps = DBUtil.getConnection().prepareStatement
116 ("select count(userid) c from user_0327");
117 ResultSet rs = ps.executeQuery();
118 int total =0;
119 if(rs.next()){
120 int num = rs.getInt("c");
121 total = num%pagesize==0?num/pagesize:num/pagesize+1;
122 }
123 ps.close();
124 DBUtil.getConnection().close();
125 return total;
126 }
127 }
8.(com.icss.service)UserService.java
1
10 package com.icss.service;
11
12 import java.sql.SQLException;
13 import java.util.ArrayList;
14
15 import com.icss.dao.UserDao;
16 import com.icss.vo.User;
17
18
24 public class UserService {
25 public ArrayList selectAllUserService() throws ClassNotFoundException, SQLException{
26 UserDao user = new UserDao();
27 return user.selectAllUserDao();
28 }
29 public void addUserService(User user) throws ClassNotFoundException, SQLException{
30 UserDao ud = new UserDao();
31 ud.addUserDao(user);
32 }
33 public void deleteUserService(int id) throws ClassNotFoundException, SQLException{
34 UserDao ud = new UserDao();
35 ud.deleteUserDao(id);
36 }
37 public User updateSelectUserByIdService(int id) throws ClassNotFoundException, SQLException{
38 UserDao ud = new UserDao();
39 return ud.updateSelectUserByIdDao(id);
40 }
41 public void updateUserByIdService(User user) throws ClassNotFoundException, SQLException{
42 UserDao ud = new UserDao();
43 ud.updateUserByIdDao(user);
44 }
45 public int getTotalPageService(int pagesize) throws ClassNotFoundException, SQLException{
46 UserDao ud = new UserDao();
47 return ud.getTotalPageDao(pagesize);
48 }
49
50 }
9.(com.icss.controller)https://www.yht7.com/news/SelectAllUserServlet.java
1 package com.icss.controller;
2
3 import java.io.IOException;
4 import java.sql.SQLException;
5 import java.util.ArrayList;
6
7 import javax.servlet.ServletException;
8 import javax.servlet.annotation.WebServlet;
9 import javax.servlet.http.HttpServlet;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12
13 import com.icss.service.UserService;
14 import com.icss.vo.User;
15
16
19 @WebServlet("/https://www.yht7.com/news/SelectAllUserServlet")
20 public class https://www.yht7.com/news/SelectAllUserServlet extends HttpServlet {
21 private static final long serialVersionUID = 1L;
22
23
26 public https://www.yht7.com/news/SelectAllUserServlet() {
27 super();
28 // TODO Auto-generated constructor stub
29 }
30
31
34 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
35 doPost(request, response);
36 }
37
38
41 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
42 request.setCharacterEncoding("utf-8");
43 //接受请求
44 //处理请求
45 UserService us = new UserService();
46 try {
47 //************分页数**************
48 int pagesize = 3;
49 int num = us.getTotalPageService(pagesize);
50 System.out.println(num);
51 request.setAttribute("num", num);
52 //************分页数**************
53 ArrayList user = us.selectAllUserService();
54 request.setAttribute("user", user);
55 request.getRequestDispatcher("AddUser.jsp").forward(request, response);
56 } catch (ClassNotFoundException e) {
57 // TODO Auto-generated catch block
58 e.printStackTrace();
59 } catch (SQLException e) {
60 // TODO Auto-generated catch block
61 e.printStackTrace();
62 }
63
64 }
65
66 }
10.(com.icss.controller)AddUserServlet.java
1 package com.icss.controller;
2
3 import java.io.IOException;
4 import java.sql.SQLException;
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.icss.service.UserService;
13 import com.icss.vo.User;
14
15
18 @WebServlet("/AddUserServle")
19 public class AddUserServle extends HttpServlet {
20 private static final long serialVersionUID = 1L;
21
22
25 public AddUserServle() {
26 super();
27 // TODO Auto-generated constructor stub
28 }
29
30
33 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
34 doPost(request, response);
35 }
36
37
40 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
41 request.setCharacterEncoding("utf-8");
42 //接受请求
43 User user = new User();
44 //String user2 = request.getParameter("username");
45 user.setUsername(request.getParameter("username"));
46 ///System.out.println(user2);
47 user.setPassword(request.getParameter("password"));
48 user.setNikename(request.getParameter("nikename"));
49 user.setSex(request.getParameter("sex"));
50 user.setAge(Integer.parseInt(request.getParameter("age")));
51 user.setImage(request.getParameter("image"));
52 //处理请求
53 UserService us = new UserService();
54 try {
55
56 us.addUserService(user);
57 request.getRequestDispatcher("https://www.yht7.com/news/SelectAllUserServlet").forward(request, response);
58 } catch (ClassNotFoundException e) {
59 // TODO Auto-generated catch block
60 e.printStackTrace();
61 } catch (SQLException e) {
62 // TODO Auto-generated catch block
63 e.printStackTrace();
64 }
65 }
66
67 }
11.(com.icss.controller)UpdateSelectUserByIdServlet.java
1 package com.icss.controller;
2
3 import java.io.IOException;
4 import java.sql.SQLException;
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.icss.service.UserService;
13 import com.icss.vo.User;
14
15
18 @WebServlet("/UpdateSelectUserByIdServlet")
19 public class UpdateSelectUserByIdServlet extends HttpServlet {
20 private static final long serialVersionUID = 1L;
21
22
25 public UpdateSelectUserByIdServlet() {
26 super();
27 // TODO Auto-generated constructor stub
28 }
29
30
33 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
34 doPost(request, response);
35 }
36
37
40 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
41 request.setCharacterEncoding("utf-8");
42 //接受数据
43 int id = Integer.parseInt(request.getParameter("userid"));
44 //System.out.println(id);
45 //处理数据
46 UserService us = new UserService();
47 try {
48 User user = us.updateSelectUserByIdService(id);
49 request.setAttribute("user", user);
50 request.getRequestDispatcher("UpdateUser.jsp").forward(request, response);
51 } catch (ClassNotFoundException e) {
52 // TODO Auto-generated catch block
53 e.printStackTrace();
54 } catch (SQLException e) {
55 // TODO Auto-generated catch block
56 e.printStackTrace();
57 }
58 }
59
60 }
12.(com.icss.controller)UpdateUserServlet.java
1 package com.icss.controller;
2
3 import java.io.IOException;
4 import java.sql.SQLException;
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.icss.service.UserService;
13 import com.icss.vo.User;
14
15
18 @WebServlet("/UpdateUserServlet")
19 public class UpdateUserServlet extends HttpServlet {
20 private static final long serialVersionUID = 1L;
21
22
25 public UpdateUserServlet() {
26 super();
27 // TODO Auto-generated constructor stub
28 }
29
30
33 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
34 doPost(request, response);
35 }
36
37
40 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
41 request.setCharacterEncoding("utf-8");
42 //接受数据
43 User user = new User();
44 user.setUserid(Integer.parseInt(request.getParameter("userid")));
45 user.setUsername(request.getParameter("username"));
46 user.setPassword(request.getParameter("password"));
47 user.setNikename(request.getParameter("nikename"));
48 user.setSex(request.getParameter("sex"));
49 user.setAge(Integer.parseInt(request.getParameter("age")));
50 user.setImage(request.getParameter("image"));
51 //处理请求
52 UserService us = new UserService();
53 try {
54 us.updateUserByIdService(user);
55 request.getRequestDispatcher("https://www.yht7.com/news/SelectAllUserServlet").forward(request, response);
56 } catch (ClassNotFoundException e) {
57 // TODO Auto-generated catch block
58 e.printStackTrace();
59 } catch (SQLException e) {
60 // TODO Auto-generated catch block
61 e.printStackTrace();
62 }
63 }
64
65 }
13.(com.icss.controller)DeleteUserServlet.java
1 package com.icss.controller;
2
3 import java.io.IOException;
4 import java.sql.SQLException;
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.icss.service.UserService;
13
14
17 @WebServlet("/DeleteUserServlet")
18 public class DeleteUserServlet extends HttpServlet {
19 private static final long serialVersionUID = 1L;
20
21
24 public DeleteUserServlet() {
25 super();
26 // TODO Auto-generated constructor stub
27 }
28
29
32 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
33 doPost(request, response);
34 }
35
36
39 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
40 request.setCharacterEncoding("utf-8");
41 //接受请求
42 int id = Integer.parseInt(request.getParameter("userid"));
43 //System.out.println(id);
44 //处理请求
45 UserService us = new UserService();
46 try {
47 us.deleteUserService(id);
48 request.getRequestDispatcher("https://www.yht7.com/news/SelectAllUserServlet").forward(request, response);
49 } catch (ClassNotFoundException | SQLException e) {
50 // TODO Auto-generated catch block
51 e.printStackTrace();
52 }
53 }
54
55 }
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341