图书商城(基于Jsp+Servlet)
Posted 浅滩浅
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图书商城(基于Jsp+Servlet)相关的知识,希望对你有一定的参考价值。
这个项目主要是加深一下对于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,就将该分类作为外面循环分类的子分类。
项目结构:
项目相关截图:
前台展示:
前台结算界面:
后台图书管理界面:
后台账号管理界面:
二级目录界面:
以上是关于图书商城(基于Jsp+Servlet)的主要内容,如果未能解决你的问题,请参考以下文章
基于jsp+servlet图书管理系统之后台用户信息删除操作