javaweb简单的增删改查之“模糊查询”

String sql="select * from  表名 where (name like '%"+name+"%')";
这里的name可以根据自己的需要进行修改
多条件模糊查询
String sql="select * from 表名 where (name like '%"+name+"%') and (age like '%"+age+"%')";
这里的name和age都是可以根据自己的需要进行更换
Connection con=Databass.Databass.getConn();
    //实现模糊查询
    public List<Member> search(String name){
        List<Member> list=new ArrayList<Member>();
        PreparedStatement pre=null;
        ResultSet rs=null;
        String sql="select * from duoText where(name like '%"+name+"%')";
        try {
            pre=con.prepareStatement(sql);
            
            
            rs=pre.executeQuery();
            while(rs.next()) {
                Member ww=new Member();
                ww.setName(rs.getString("name"));
                ww.setAge(rs.getString("age"));
                ww.setHeight(rs.getString("height"));
                ww.setWeight(rs.getString("weigth"));
                list.add(ww);
            }
        }catch(Exception w) {
            w.printStackTrace();
        }finally {
            Databass.Databass.close(rs, pre, con);
        }
        return list;
    }

}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置请求字体
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String name=request.getParameter("name");
        Method ww=new Method();
        List<Member> list=ww.search(name);
        request.setAttribute("list", list);
        request.getRequestDispatcher("/search1.jsp").forward(request, response);

    }
1
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="ServletSearch" method="post">
姓名<label>(模糊查询)</label>
<input type="text" name="name"><br>

<input type="submit" value="查询">
</form>
</body>
</html>
2.
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 <%@page import="Method.*,Member.*,java.util.List,Servlet.*" %>
 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
            <td>姓名</td>
            <td>年龄</td>
            <td>高度</td>
            <td>宽度</td>
            </tr>
<%
String name=request.getParameter("name");
Method ww=new Method();
List<Member> list=ww.search(name);
request.setAttribute("list", list);

%>
<c:forEach var="u" items="${list}">
<tr>
            <td>${u.name}</td>
            <td>${u.age}</td>
            <td>${u.height}</td>
            <td>${u.weight}</td>
            </tr>
            </c:forEach>
</table>
</body>
</html>
这只是部分代码,剩余代码在以往博客中有。
posted @ 2019-12-18 10:22  程序员亮亮丫!!  阅读(4049)  评论(0编辑  收藏  举报