修改操作和模糊查询

基于MVC模型使用分层模式完成修改

主页

<html>
<head>
    <title>主页!</title>
</head>
<body>
<h2>欢迎来到主页!</h2>
<a href="addGoods.jsp">添加</a>
<table>
    <thead>
    <tr>
        <th>商品编号</th>
        <th>商品名称</th>
        <th>商品价格</th>
        <th>商品说明</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <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="findById?gid=${goods.gid}">修改</a>
                <a href="del?gid=${goods.gid}">删除</a>
        </td>
        </tr>
    </c:forEach>
    </tbody>
</table>
</body>
</html>

删除页面

package com.su.servlet;
 
import com.su.dao.GoodDao;
 
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;
@WebServlet("/del")
public class DelGoods extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      doPost(req,resp);
    }
 
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int gid=Integer.parseInt(req.getParameter("gid"));
       //根据id执行数据库的删除
        GoodDao goodDao=new GoodDao();
        int row=goodDao.deleteById(gid);
        if (row>0){
            req.getRequestDispatcher("selectAllGoods").forward(req,resp);
        }else{
            req.setAttribute("error_msg","删除 出现了问题");
            req.getRequestDispatcher("error.jsp").forward(req,resp);
        }
    }
 
}
package com.yang.servlet;
 
import com.yang.bean.Goods;
import com.yang.dao.GoodsDao;
 
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;
 
@WebServlet("/update")
public class UpdateGoods extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
        doPost(request,response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
 
        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);
        }
      }
   }

模糊查询操作

主页代码

<html>
<head>
    <title>主页!</title>
</head>
<body>
<h2>欢迎来自${user.address}的${user.username}访问主页!</h2>
<form action="seach" method="post">
    <input type="text" name="keyword" value="">
    <input type="submit" value="搜索">
</form>
<a href="addGoods.jsp">添加</a>
<table>
    <thead>
    <tr>
        <th>商品编号</th>
        <th>商品名称</th>
        <th>商品价格</th>
        <th>商品说明</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <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="findById?gid=${goods.gid}">修改</a>
            <a href="del?gid=${goods.gid}">删除</a>
        </td>
        </tr>
    </c:forEach>
    </tbody>
</table>
</body>
</html>

seach页面

package com.su.servlet;
 
import com.su.Bean.Goods;
import com.su.dao.GoodDao;
 
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 javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
@WebServlet("/seach")
public class Seach extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
 
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");//设置请求的编码格式为中文
        resp.setCharacterEncoding("utf-8");//设置响应的编码格式
        String keyword=req.getParameter("keyword");
        //执行JDBC的模糊查询 select * from t_goods where gname like?
        GoodDao goodDao=new GoodDao();
        List<Goods> goodsList=goodDao.seach(keyword);
        System.out.println(goodsList);
        //把模糊查询查询到的商品信息集合存储到session中
        HttpSession session=req.getSession();
        session.setAttribute("goodsList",goodsList);
        //跳转到主页进行信息展示
        resp.sendRedirect("zhuye.jsp");
    }
}

定义seach方法

  public int update(Goods goods) {
        try {
            Class.forName(driver);
            con= DriverManager.getConnection(url,user,password);
            String sql="update t_goods set gname=?,price=?,mark=? where gid=?";
            ps=con.prepareStatement(sql);
            ps.setObject(1,goods.getGname());
            ps.setObject(2,goods.getPrice());
            ps.setObject(3,goods.getMark());
            ps.setObject(4,goods.getGid());
            row=ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (ps!=null){
                    ps.close();
                }
 
                if (con!=null){
                    con.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return row;
    }
 
    public List<Goods> seach(String keyword) {
        List<Goods>goodsList=new ArrayList<>();
        try{
            //加载驱动
            Class.forName(driver);
            //获取连接
            con= DriverManager.getConnection(url,user,password);
            //编写sql语句
            String sql="select * from t_goods where gname like concat('%',?,'%')";
            ps=con.prepareStatement(sql);
            ps.setObject(1,keyword);
            rs=ps.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 {
            try {
                if (rs!=null){
                    rs.close();
                }
                if (ps!=null){
                    ps.close();
                }
                if (con!=null){
                    con.close();
                }
            }catch (Exception e){
 
            }
        }
        return goodsList;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值