码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-12-18 11:08:55      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:jsp   nec   parameter   ref   int   NPU   except   name   text   

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>
这只是部分代码,剩余代码在以往博客中有。

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

标签:jsp   nec   parameter   ref   int   NPU   except   name   text   

原文地址:https://www.cnblogs.com/lianggegege123/p/12058471.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!