After successful registration and login, the function of adding, deleting, modifying and checking is implemented on the page.

1. Log in first. After successful login, the operation interface will be displayed. According to the prompts on the interface, perform the functions of registration, login, add, modify, delete and query.

 1. Implement user registration function

 2. Implement user login function

 3. Implement additional functions for users.

 3. Implement user modifications, but consider whether the user actually exists

 4. Implement the user’s deletion function and query function.

 5. Implement user query function, query all and query based on userName

 

 

 

 

2. Realize the four functions of adding, deleting, modifying and checking

1. Add functions 

If the addition is successful, check whether the return page (if any) and the database record are consistent with the new addition. If the addition fails, check whether the prompt is reasonable.

1. Fill in all the elements to increase success

2. Check each required field. What will happen if you add it without filling it in (an error message or a successful addition?)

3. Check the business rules of the field. For example: ID can only be numbers (why? ID is of type int)

4. Pop-up window confirms and cancels to see if the process is added or cancelled.

<html>
<head>
    <title>商品信息添加</title>
</head>
<body>
      <h2>商品添加</h2>
      <form action="addGoods" method="post">
          商品名称:<input type="text" name="gname" value="" placeholder="商品名称" /> <br/>
          商品价格:<input type="number" step="0.01" name="price" value="" placeholder="商品价格" /> <br/>
          商品说明:<input type="text" name="mark" value="" placeholder="商品说明" /> <br/>
          <input type="submit" value="添加">
      </form>
</body>
</html>
public int add(Goods goods){
        try {
            Class.forName(JDBCdriver);
            con=DriverManager.getConnection(JDBCurl,JDBCusername,JDBCpassword);
            String sql="insert into t_goods(gname,price,mark) values(?,?,?)";
            pstm=con.prepareStatement(sql);
            pstm.setObject(1,goods.getGname());
            pstm.setObject(2,goods.getPrice());
            pstm.setObject(3,goods.getMark());
            row = pstm.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(pstm!=null){
                    pstm.close();
                }
                if(con!=null){
                    con.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return row;
    }
@WebServlet("/addGoods")
public class AddGoods extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        //获取请求参数并封装到Goods对象中
        Goods goods = new Goods();
        goods.setGname(request.getParameter("gname"));
        //使用Double包装类把字符串数字转换为double数据
        goods.setPrice(Double.parseDouble(request.getParameter("price")));
        goods.setMark(request.getParameter("mark"));

        //执行JDBC的添加操作
        GoodsDao goodsDao = new GoodsDao();
        int row = goodsDao.add(goods);

        if(row>0){
            //添加成功,请求selectAllGoods地址,执行对应的Servlet(查询商品信息,存入session中,转到主页)
            request.getRequestDispatcher("selectAllGoods").forward(request,response);
        }else {
            request.setAttribute("error_msg","添加商品出错啦");
        }
    }
}

 2. Delete function

 Successful deletion requires checking the return page (if any) and database records.

1. Soft deletion or hard deletion (hard deletion means physical deletion to see if the database record exists. For soft deletion, check whether the corresponding database field has been changed, for example: the status field value is changed to disable)

2. Delete prompt

3. Pop-up window confirms and cancels to see if the process is deleted or cancelled.

4. Association relationship: For example, your system user is deleted. See if you can still log in to the corresponding system using this user

5. Delete permissions. From no permissions to having permissions, from having permissions to no permissions, pay attention to the changes in permissions

@WebServlet("/dell")
public class DellGoods extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int gid=Integer.parseInt(request.getParameter("gid"));
        GoodsDao goodsDao = new GoodsDao();
        int row = goodsDao.delete(gid);
        if(row>0){
          request.getRequestDispatcher("selectAllGoods").forward(request,response);
        }else {
            request.setAttribute("error.msg","删除出现了问题");
            request.getRequestDispatcher("error.jsp").forward(request,response);
        }
    }
}
public int delete(int gid){
        try {
            Class.forName(JDBCdriver);
            con=DriverManager.getConnection(JDBCurl,JDBCusername,JDBCpassword);
            String sql="delete from t_goods where gid=?";
            pstm=con.prepareStatement(sql);
            pstm.setObject(1,gid);
            row = pstm.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(pstm!=null){
                    pstm.close();
                }
                if(con!=null){
                    con.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return row;
    }

3. Modify function

1. Check whether the corresponding fields can be modified and cannot be modified. For example, in many cases, ID cannot be modified.

2. Every field that can be modified needs to be modified and submitted, and check whether the values ​​on the return page and database are modified correctly.

3. Modify and restore. The modified things are changed to the ones before modification. Pay attention to the page changes.

4. Association relationship: For example, in the permission system, if the user's permissions are modified, then the corresponding module access permissions will be modified.

The length of the remark information, etc. will be mentioned after specific requirements.

<html>
<head>
    <title>商品信息修改</title>
</head>
<body>
    <h2>商品修改</h2>
    <form action="updateGoods" method="post">
        商品编号:<input type="text" name="gid" value="${goods.gid}" placeholder="商品编号" readonly="readonly"/> <br/>
        商品名称:<input type="text" name="gname" value="${goods.gname}" placeholder="商品名称" /> <br/>
        商品价格:<input type="number" step="0.01" name="price" value="${goods.price}" placeholder="商品价格" /> <br/>
        商品说明:<input type="text" name="mark" value="${goods.mark}" placeholder="商品说明" /> <br/>
     <input type="submit" value="修改">
  </form>
 </body>
</html>
@WebServlet("/findById")
public class FindGoodsById extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          int gid = Integer.parseInt(request.getParameter("gid"));
          //根据gid查询该商品信息
          GoodsDao goodsDao = new GoodsDao();
          Goods goods = goodsDao.selectById(gid);

        if(goods!=null){
            //把数据存储到request域对象中,然后请求转发到页面
            request.setAttribute("goods",goods);
            request.getRequestDispatcher("showGoods.jsp").forward(request,response);
        }else {
            request.setAttribute("error.msg","修改出现了问题");
            request.getRequestDispatcher("error.jsp").forward(request,response);
        }
    }
}
@WebServlet("/updateGoods")
public class UpdateGoods extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        //获取表单请求的数据,封装到goods对象中
        Goods goods = new Goods();
        goods.setGid(Integer.parseInt(request.getParameter("gid")));
        goods.setGname(request.getParameter("gname"));
        goods.setPrice(Double.parseDouble(request.getParameter("price")));
        goods.setMark(request.getParameter( "mark"));
        System.out.println(goods);

        GoodsDao goodsDao = new GoodsDao();
        int row = goodsDao.update(goods);
        if(row>0){
            request.getRequestDispatcher("selectAllGoods").forward(request,response);
        }else {
            request.setAttribute("error.msg","修改出现了问题");
            request.getRequestDispatcher("error.jsp").forward(request,response);
        }
    }
}
public Goods selectById(int gid){
        Goods goods=null;
        try {
            Class.forName(JDBCdriver);
            con = DriverManager.getConnection(JDBCurl, JDBCusername, JDBCpassword);
            String sql = "select * from t_goods where gid=?";
            pstm = con.prepareStatement(sql);
            pstm.setObject(1,gid);
            rs = pstm.executeQuery();
            if (rs.next()) {
                //从结果集中获取数据,封装到Goods对象中
                goods = new Goods();
                goods.setGid(rs.getInt("gid"));
                goods.setGname(rs.getString("gname"));
                goods.setPrice(rs.getDouble("price"));
                goods.setMark(rs.getString("mark"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(rs!=null){
                    rs.close();
                }
                if(pstm!=null){
                    pstm.close();
                }
                if(con!=null){
                    con.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return goods;
    }
  public int update(Goods goods){
        try {
            Class.forName(JDBCdriver);
            con = DriverManager.getConnection(JDBCurl, JDBCusername, JDBCpassword);
            String sql = "update t_goods set gname=?,price=?,mark=? where gid=?";
            pstm = con.prepareStatement(sql);
            pstm.setObject(1,goods.getGname());
            pstm.setObject(2,goods.getPrice());
            pstm.setObject(3,goods.getMark());
            pstm.setObject(4,goods.getGid());
            row=pstm.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(pstm!=null){
                    pstm.close();
                }
                if(con!=null){
                    con.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return row;
    }

4. Query function

1. Click Query directly to find all by default

2. Check whether the requirements implement the query of the corresponding field and whether the corresponding field supports fuzzy query.

3. Field cross query, such as log information, enter the operator and time at the same time to see whether the query result is correct.

4. Generally, there will be a button to reset query conditions on the page. Check whether the content of each input box or drop-down box has been cleared.

Some query boxes require inputting letters, characters, etc. These cases are only needed when there is a request or when the number of cases is collected. Usually no one cares about these things of yours

@WebServlet("/selectAllGoods")
public class SelectAllGoods extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("SelectAllGoods...doPost");
        //查询所有商品信息
        GoodsDao goodsDao = new GoodsDao();
        List<Goods> goodsList = goodsDao.selectAll();
        System.out.println(goodsList);

        //把数据传递到前端页面
        //通过request获取session对象,该对象可以向前端传输数据的容器
        HttpSession session = request.getSession();
        //向session中存入商品信息集合
        session.setAttribute("goodsList",goodsList);
        //登录成功,跳转到主页
        response.sendRedirect("zhuye.jsp");
    }
}
 public List<Goods> selectAll() {
        List<Goods> goodsList = new ArrayList<>();
        try {
            Class.forName(JDBCdriver);
            con = DriverManager.getConnection(JDBCurl, JDBCusername, JDBCpassword);
            String sql = "select * from t_goods";
            pstm = con.prepareStatement(sql);
            rs = pstm.executeQuery();
            while (rs.next()) {
                //从结果集中获取数据,封装到Goods对象中
                Goods goods = new Goods();
                goods.setGid(rs.getInt("gid"));
                goods.setGname(rs.getString("gname"));
                goods.setPrice(rs.getDouble("price"));
                goods.setMark(rs.getString("mark"));
                //把当前行对应的对象储存到集合中
                goodsList.add(goods);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(rs!=null){
                    rs.close();
                }
                if(pstm!=null){
                    pstm.close();
                }
                if(con!=null){
                    con.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return goodsList;
    }

Finally, the four functions of querying and modifying are completed!

Guess you like

Origin blog.csdn.net/weixin_69036336/article/details/129234566