代碼分享鏈接 https://pan.baidu.com/s/1UM0grvpttHW9idisiqa6rA 提取碼:hx7c 圖示 項目結構 1.SelectAllUser.jsp 1 <%@ page language="java" contentType="text/html; charset ...
代碼分享鏈接
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 <!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