MVC模型和分层模式完成添加和修改和搜索

添加:

<a href="addGoods.jsp">添加</a>
<table>
    <tr>
        <th>商品编号</th>
        <th>商品名称</th>
        <th>商品价格</th>
        <th>商品说明</th>
        <th>操作</th>
    </tr>
    <c:forEach items="${goodsList}" var="goods">
        <tr>
            <td>${goods.gid}</td>
            <td>${goods.gname}</td>
            <td>${goods.price}</td>
            <td>${goods.mark}</td>
            <td>
                <a href="DelGoods?gid=${goods.gid}">删除</a>
                <a href="FinByld?gid=${goods.gid}">修改</a>
            </td>
        </tr>
    </c:forEach>
</table>
</body>
</html>

GoodsDao中的数据库代码

public int AddGoods(Goods goods){
        int n=0;
        try{
            //加载驱动包
            con = JDBCUtil.getCon();
            //编写sql语句
            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());
            //执行sql语句
            n= pstm.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtil.close(pstm, con);
        }
        return n;
    }
@WebServlet("/addGoods")
public class AddGoods extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        Goods goods=new Goods();
        goods.setGname(request.getParameter("gname"));
        goods.setPrice(Double.parseDouble(request.getParameter("price")));
        goods.setMark(request.getParameter("mark"));

        GoogsDao goods1=new GoogsDao();
        int row=goods1.AddGoods(goods);
        if (row>0){
            request.getRequestDispatcher("selectAllGoods").forward(request,response);
        }else{
            request.setAttribute("error_msg","添加商品信息失败");
            request.getRequestDispatcher("error.jsp").forward(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       doPost(request,response);
    }
}

添加成功

修改代码:

<a href="FinByld?gid=${goods.gid}">修改</a>

GoodsDao中的数据库代码

 public int goodsUpdate(Goods goods){
        int n=0;
        try{
            //加载驱动包
            con = JDBCUtil.getCon();
            //编写sql语句
            String sql="update t_goods set price=?,mark=?,gname=? where gid=?";
            //获取预处理对象
            pstm = con.prepareStatement(sql);
            //传参
            pstm.setObject(1,goods.getPrice());
            pstm.setObject(2,goods.getMark());
            pstm.setObject(3,goods.getGname());
            pstm.setObject(4,goods.getGid());

            //执行sql语句
            n= pstm.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtil.close(pstm, con);
        }
        return n;
    }
}
@WebServlet("/FinByld")
public class FinByld extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int gid=Integer.parseInt(request.getParameter("gid"));
        //根据gid查询商品的信息
        System.out.println(gid);
        GoogsDao googsDao=new GoogsDao();
        Goods goods=googsDao.selectById(gid);
        System.out.println(goods);
        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);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

修改成功

搜索代码:

<body>
<h2>欢迎来自${user.address}的${user.username}来到主页!</h2>
<form action="seach" method="post">
<input type="text" name="keyword" value=""/>
    <input type="submit" value="搜索"/>
</form>

GoodsDao中的数据库代码

public List<Goods> seach(String keyword){
        List<Goods> goodsList = new ArrayList<>();
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url, user, password);
            String sql = "select*from t_goods where gname like concat('%',?,'%')";
            pstm = con.prepareStatement(sql);
            pstm.setObject(1,keyword);
            rs = pstm.executeQuery();
            while (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"));
                goodsList.add(goods);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(rs,pstm,con);
        }
        return goodsList;
    }
@WebServlet("/seach")
public class Seach extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        String keyword=request.getParameter("keyword");
        //执行jdbc的模糊查询 select*from t_goods where gname like ?
        GoogsDao googsDao=new GoogsDao();
        List<Goods> goodsList=googsDao.seach(keyword);
        System.out.println(goodsList);
        HttpSession session=request.getSession();
        session.setAttribute("goodsList",goodsList);
        //跳转到主页进行信息显示
        response.sendRedirect("zhuye.jsp");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}

搜索成功。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值