IDEA写模糊查询
模糊查詢結果圖:
1.在Dao層和service中:
//根據商品名稱查詢,所有商品 List<Tbl_purchase> Mocha(String pName);2.Dao層的實現方法
public List<Tbl_purchase> Mocha(String pName) {Connection conn = helper.getConnection();List<Tbl_purchase> list = new ArrayList<Tbl_purchase>();try {PreparedStatement ps = conn.prepareStatement("select p.*,a.paDate,a.paState from tbl_purchase p, tbl_pur_approval a where a.pId=p.pId and p.pName like '%" + pName + "%'");ResultSet rs = ps.executeQuery();while (rs.next()) {Tbl_purchase p = new Tbl_purchase();p.setpId(rs.getInt("pId"));p.setpType(rs.getString("pType"));p.setpName(rs.getString("pName"));p.setpUnit(rs.getString("pUnit"));p.setpNum(rs.getInt("pNum"));p.setpPerson(rs.getString("pPerson"));Tbl_pur_approval a = new Tbl_pur_approval();a.setPaDate(rs.getString("paDate"));a.setPaState(rs.getString("paState"));p.setApproval(a);list.add(p);//裝載集合}return list;} catch (SQLException e) {e.printStackTrace();}return null;}.3service的實現
public List<Tbl_purchase> Mocha(String pName) {return purchaseDao.Mocha(pName);}4.在servlet中
@WebServlet("/Mocha") public class Mocha extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request,response);}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8");String pName = request.getParameter("pName");PurchaseService purchaseService = new PurchaseServiceImpl();List<Tbl_purchase> list = purchaseService.Mocha(pName);System.out.println(pName);System.out.println("list =模糊查詢 " +list);request.setAttribute("list",list);request.getRequestDispatcher("WEB-INF/jsp/show.jsp").forward(request,response);} }5.在show.jsp中 idea的jsp頁面一定要加上 isELIgnored="false"
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %><html> <head><title>Title</title> </head> <body> <table align="center" border="1px"><tr><td colspan="9" align="center"><%--絕對路徑--%><form action="${pageContext.request.contextPath}/Mocha" method="post">采購品牌名稱:<input type="text" name="pName"><input type="submit" value="查詢"></form></td></tr><tr><td>采購編號</td><td>采購類型</td><td>采購商品名稱</td><td>商品單位</td><td>采購數量</td><td>采購人</td><td>審批時間</td><td>狀態</td><td>操作內容</td></tr><c:forEach items="${list}" var="l"><tr><td>${l.pId}</td><td>${l.pType}</td><td>${l.pName}</td><td>${l.pUnit}</td><td>${l.pNum}</td><td>${l.pPerson}</td><td>${l.approval.paDate}</td><td><c:choose><c:when test="${l.approval.paState=='未審批' }"><span style="color: red; font-weight: bolder;">${l.approval.paState}</span></c:when><c:otherwise>${l.approval.paState}</c:otherwise></c:choose></td><c:if test="${l.approval.paState=='未審批' }"><td><a href="${pageContext.request.contextPath}/Shen?pId=${l.pId}">審批</a><a href="${pageContext.request.contextPath}/Deleone?pId=${l.pId}">刪除</a></td></c:if><c:if test="${l.approval.paState=='已審批' }"><td colspan="9" align="center"><a href="${pageContext.request.contextPath}/Deleone?pId=${l.pId}">刪除</a></td></c:if></tr></c:forEach><tr><td colspan="9" align="center"><a href="${pageContext.request.contextPath}/AddBefore">添加</a></td></tr></table></body> </html>總結
- 上一篇: 网络工程的工作任务课堂作业
- 下一篇: android studio 跳转后保留