当前位置:首页 > Web开发 > 正文

图书商城(基于Jsp+Servlet)

2024-03-31 Web开发

用户更新的逻辑:

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的简单封装:

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/40550.html