动态网站项目(Dynamic Web Project)CRUD(增删改查)功能的实现(mvc(五层架构)+jdbc+servlet+tomcat7.0+jdk1.8),前端使用JSP+JSTL+EL组
無笙 人气:3代码分享链接
http://pan.baidu.com/s/1UM0grvpttHW9idisiqa6rA 提取码:hx7c
图示
项目结构
1.SelectAllUser.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 <a href="SelectAllUserServlet">查询所有用户</a> 11 </body> 12 </html>
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"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="Add.jsp">增加用户</a>
<table border="1px">
<tr>
<td>用户ID</td>
<td>用户姓名</td>
<td>用户密码</td>
<td>用户昵称</td>
<td>用户性别</td>
<td>用户年龄</td>
<td>用户头像</td>
<td>操作</td>
</tr>
<c:forEach var="xyz" items="${requestScope.user }">
<tr>
<td>${xyz.userid }</td>
<td>${xyz.username }</td>
<td>${xyz.password }</td>
<td>${xyz.nikename }</td>
<td>${xyz.sex }</td>
<td>${xyz.age }</td>
<td><img src="${xyz.image }" alt="图片加载失败" height="50px" weight="50px"/></td>
<td>
<a href="UpdateSelectUserByIdServlet?userid=${xyz.userid }">修改</a>
<a href="DeleteUserServlet?userid=${xyz.userid }">删除</a>
</td>
</tr>
</c:forEach>
</table>
<!-- 分页 -->
一共${num }页
<a href=""#>首页</a>
<a href=""#>上一页</a>
<a href=""#>下一页</a>
<a href=""#>尾页</a>
</body>
</html>
3.Add.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 <form action="AddUserServle" method="post"> 11 用户名<input type="text" name="username"><br> 12 密码<input type="text" name="password"><br> 13 昵称<input type="text" name="nikename"><br> 14 性别<input type="text" name="sex"><br> 15 年龄<input type="text" name="age"><br> 16 头像<input type="file" name="image"><br> 17 <input type="submit" value="提交"> 18 </form> 19 </body> 20 </html>
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 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 12 13 <form action="UpdateUserServlet" method="post" > 14 用户id:${user.userid } 15 <input type="hidden" name="userid" value="${user.userid }"><br> 16 用户名<input type="text" name="username" value="${user.username }"><br> 17 密码<input type="text" name="password" value="${user.password }"><br> 18 昵称<input type="text" name="nikename" value="${user.nikename }"><br> 19 性别<input type="text" name="sex" value="${user.sex }"><br> 20 年龄<input type="text" name="age" value="${user.age }"><br> 21 头像<input type="file" name="image" value="${user.image }"><br> 22 <input type="submit" value="提交"> 23 </form> 24 25 </body> 26 </html>
5.(com.icss.vo)User.java
1 /** 2 * <p>Title: User.java</p> 3 * <p>Description: </p> 4 * <p>Copyright: Copyright (c) 2020</p> 5 * <p>Company: </p> 6 * @author liuxin 7 * @date 2020年3月27日 8 * @version 1.0 9 */ 10 package com.icss.vo; 11 12 /** 13 * <p>Title: User</p> 14 * <p>Description: </p> 15 * @author liuxin 16 * @date 2020年3月27日 17 */ 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 /** 28 * @return the userid 29 */ 30 public int getUserid() { 31 return userid; 32 } 33 /** 34 * @param userid the userid to set 35 */ 36 public void setUserid(int userid) { 37 this.userid = userid; 38 } 39 /** 40 * @return the username 41 */ 42 public String getUsername() { 43 return username; 44 } 45 /** 46 * @param username the username to set 47 */ 48 public void setUsername(String username) { 49 this.username = username; 50 } 51 /** 52 * @return the password 53 */ 54 public String getPassword() { 55 return password; 56 } 57 /** 58 * @param password the password to set 59 */ 60 public void setPassword(String password) { 61 this.password = password; 62 } 63 /** 64 * @return the nikename 65 */ 66 public String getNikename() { 67 return nikename; 68 } 69 /** 70 * @param nikename the nikename to set 71 */ 72 public void setNikename(String nikename) { 73 this.nikename = nikename; 74 } 75 /** 76 * @return the sex 77 */ 78 public String getSex() { 79 return sex; 80 } 81 /** 82 * @param sex the sex to set 83 */ 84 public void setSex(String sex) { 85 this.sex = sex; 86 } 87 /** 88 * @return the age 89 */ 90 public int getAge() { 91 return age; 92 } 93 /** 94 * @param age the age to set 95 */ 96 public void setAge(int age) { 97 this.age = age; 98 } 99 /** 100 * @return the image 101 */ 102 public String getImage() { 103 return image; 104 } 105 /** 106 * @param image the image to set 107 */ 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 /** 2 * <p>Title: UserDao.java</p> 3 * <p>Description: </p> 4 * <p>Copyright: Copyright (c) 2020</p> 5 * <p>Company: </p> 6 * @author liuxin 7 * @date 2020年3月27日 8 * @version 1.0 9 */ 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 /** 25 * <p>Title: UserDao</p> 26 * <p>Description: </p> 27 * @author liuxin 28 * @date 2020年3月27日 29 */ 30 public class UserDao { 31 //查询所有用户 32 public ArrayList<User> selectAllUserDao() throws ClassNotFoundException, SQLException{ 33 PreparedStatement ps = DBUtil.getConnection().prepareStatement 34 ("select * from user_0327"); 35 ResultSet rs = ps.executeQuery(); 36 ArrayList<User> al = new ArrayList<User>(); 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 /** 2 * <p>Title: UserService.java</p> 3 * <p>Description: </p> 4 * <p>Copyright: Copyright (c) 2020</p> 5 * <p>Company: </p> 6 * @author liuxin 7 * @date 2020年3月27日 8 * @version 1.0 9 */ 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 /** 19 * <p>Title: UserService</p> 20 * <p>Description: </p> 21 * @author liuxin 22 * @date 2020年3月27日 23 */ 24 public class UserService { 25 public ArrayList<User> 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)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 /** 17 * Servlet implementation class SelectAllUserServlet 18 */ 19 @WebServlet("/SelectAllUserServlet") 20 public class SelectAllUserServlet extends HttpServlet { 21 private static final long serialVersionUID = 1L; 22 23 /** 24 * @see HttpServlet#HttpServlet() 25 */ 26 public SelectAllUserServlet() { 27 super(); 28 // TODO Auto-generated constructor stub 29 } 30 31 /** 32 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 33 */ 34 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 35 doPost(request, response); 36 } 37 38 /** 39 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 40 */ 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> 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 /** 16 * Servlet implementation class AddUserServle 17 */ 18 @WebServlet("/AddUserServle") 19 public class AddUserServle extends HttpServlet { 20 private static final long serialVersionUID = 1L; 21 22 /** 23 * @see HttpServlet#HttpServlet() 24 */ 25 public AddUserServle() { 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 doPost(request, response); 35 } 36 37 /** 38 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 39 */ 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("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 /** 16 * Servlet implementation class UpdateSelectUserByIdServlet 17 */ 18 @WebServlet("/UpdateSelectUserByIdServlet") 19 public class UpdateSelectUserByIdServlet extends HttpServlet { 20 private static final long serialVersionUID = 1L; 21 22 /** 23 * @see HttpServlet#HttpServlet() 24 */ 25 public UpdateSelectUserByIdServlet() { 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 doPost(request, response); 35 } 36 37 /** 38 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 39 */ 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 /** 16 * Servlet implementation class UpdateUserServlet 17 */ 18 @WebServlet("/UpdateUserServlet") 19 public class UpdateUserServlet extends HttpServlet { 20 private static final long serialVersionUID = 1L; 21 22 /** 23 * @see HttpServlet#HttpServlet() 24 */ 25 public UpdateUserServlet() { 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 doPost(request, response); 35 } 36 37 /** 38 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 39 */ 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("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 /** 15 * Servlet implementation class DeleteUserServlet 16 */ 17 @WebServlet("/DeleteUserServlet") 18 public class DeleteUserServlet extends HttpServlet { 19 private static final long serialVersionUID = 1L; 20 21 /** 22 * @see HttpServlet#HttpServlet() 23 */ 24 public DeleteUserServlet() { 25 super(); 26 // TODO Auto-generated constructor stub 27 } 28 29 /** 30 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 31 */ 32 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 33 doPost(request, response); 34 } 35 36 /** 37 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 38 */ 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("SelectAllUserServlet").forward(request, response); 49 } catch (ClassNotFoundException | SQLException e) { 50 // TODO Auto-generated catch block 51 e.printStackTrace(); 52 } 53 } 54 55 }
加载全部内容