這個項目主要是加深一下對於servlet和jsp知識相關的了解以及簡單業務邏輯的處理。
用戶更新的邏輯:
1.點擊修改用戶的那一行可以獲取到用戶的id
2.跳轉到一個servlet,去查詢該用戶的基本信息
3.查詢到后去到一個回顯用戶修改之前的基本信息的頁面
4.用戶點擊修改完成,跳轉一個servlet中去獲取修改信息
5.信息修改格式是否正確去調用一個服務層的方法
6.正確到用戶列表那一欄,錯誤到用戶修改界面。
分頁的實現:
/** * 查詢所有用戶 * @return */ public static List<User> selAllUser(int pageNow,int showNum,String keyword) { Connection conn=null; PreparedStatement pstm=null; List<User> users = new ArrayList<>(); //聲明結果集 ResultSet rs = null; //獲取連接對象 try { conn = BaseDao.getConn(); String sql=""; if(keyword!=null){ sql="select * from user where USERNAME like ? order by USERBIRTHDAY limit ?,?"; pstm=conn.prepareStatement(sql); pstm.setString(1,"%"+keyword+"%"); //(當前頁數-1)*一頁要展示多少條記錄(當前頁最開始的下標) pstm.setInt(2,(pageNow-1)*showNum); pstm.setInt(3,showNum); }else{ sql="select * from user order by USERBIRTHDAY limit ?,?"; pstm=conn.prepareStatement(sql); pstm.setInt(1,(pageNow-1)*showNum); pstm.setInt(2,showNum); } rs=pstm.executeQuery(); while(rs.next()){ User user = new User( rs.getString("USERID"), rs.getString("USERNAME"), rs.getString("USERPASSWORD"), rs.getString("USERSEX"), rs.getString("USERBIRTHDAY"), rs.getString("USERIDENITYCODE"), rs.getString("USEREMAIL"), rs.getString("USERMOBILE"), rs.getString("USERADDRESS"), rs.getInt("USERSTATUS") ); users.add(user); } } catch (SQLException e) { e.printStackTrace(); }finally { BaseDao.closeall(rs,pstm,conn); } return users; }
/** * arr[0]表示總記錄條數、arr[1]表示總頁數 * @param showNum * @return arr */ public static int[] totalPage(int showNum,String keyword){ int []arr = {0,0}; Connection conn = null; PreparedStatement pstm=null; ResultSet rs=null; try { conn=BaseDao.getConn(); String sql=""; if(keyword!=null){ sql = "select count(*) from user where USERNAME like ?"; pstm = conn.prepareStatement(sql); pstm.setString(1, "%"+keyword+"%"); }else{ sql="select count(*) from user"; pstm=conn.prepareStatement(sql); } rs=pstm.executeQuery(); while(rs.next()){ arr[0]=rs.getInt(1); if(arr[0]%showNum==0){ arr[1]=arr[0]/showNum; }else{ arr[1]=(arr[0]/showNum)+1; } } } catch (SQLException e) { e.printStackTrace(); }finally { BaseDao.closeall(rs,pstm,conn); } return arr; }
文件上傳部分代碼:
package com.zyb.servlet.product; import com.jspsmart.upload.*; import com.zyb.pojo.product; import com.zyb.service.ProductService; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.Writer; @WebServlet("/manage/admin_doproductadd") public class DoProductAdd extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //創建Smartupload對象 SmartUpload smartUpload = new SmartUpload(); //初始化 smartUpload.initialize(this.getServletConfig(),request,response); //上傳過程 try { smartUpload.upload(); } catch (SmartUploadException e) { e.printStackTrace(); } //獲取上傳文件對象 Files files=smartUpload.getFiles(); //獲取第一個 File file=files.getFile(0); //獲取上傳文件名 String fileName=file.getFileName(); System.out.println(fileName); try { smartUpload.save("images/product"); } catch (SmartUploadException e) { e.printStackTrace(); } //獲取上傳的請求對象 Request req=smartUpload.getRequest(); String name=req.getParameter("productName"); //name=new String(name.getBytes("gbk"),"gbk"); String id=req.getParameter("parentId"); String price=req.getParameter("productPrice"); String desc=req.getParameter("productDesc"); //desc=new String(desc.getBytes("gbk"),"utf-8"); String stock=req.getParameter("productStock"); System.out.println("產品名稱"+name); product p = new product( 0, name, desc, Integer.parseInt(price), Integer.parseInt(stock), Integer.parseInt(id.split("-")[0] ), Integer.parseInt(id.split("-")[1] ), fileName ); Writer out=response.getWriter(); int mark = ProductService.insertProduct(p); if(mark>0){ out.write("<script>"); out.write("alert('添加成功');"); out.write("location.href='admin_productsel';"); out.write("</script>"); out.close(); }else{ out.write("<script>"); out.write("alert('添加失敗');"); out.write("location.href='admin_doproductadd';"); out.write("</script>"); out.close(); } } }
Dao層對jdbc的簡單封裝:
package com.zyb.dao; import java.sql.*; public class BaseDao { static { try { Class.forName("com.mysql.cj.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConn() throws SQLException { Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/shop?useSSL=false&serverTimezone=UTC","root","root"); return conn; } public static int exeCRUD(String sql,Object []params){ int cnt=0; PreparedStatement pstm=null; Connection conn = null; try { conn= BaseDao.getConn(); pstm = conn.prepareStatement(sql); for(int i=0; i<params.length; i++) { pstm.setObject(i+1, params[i]); } cnt = pstm.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); }finally { BaseDao.closeall(null, pstm, conn); } return cnt; } public static void closeall(ResultSet rs, PreparedStatement ps, Connection conn) { try { if(rs!=null) rs.close(); if(ps!=null) ps.close(); if(conn!=null) conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
二級目錄實現:
Category結構:
分類id 分類名 分類的父id
(注意如果是以及目錄它的父id就是0)
產品結構:
大概思想就是一個雙重循環,如果當前分類的父id==上層循環的id,就將該分類作為外面循環分類的子分類。
項目結構:
項目相關截圖:
前台展示:
前台結算界面:
后台圖書管理界面:
后台賬號管理界面:
二級目錄界面: