javaweb简单的增删改查之“模糊查询”
Posted 亮哥哥哥
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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>
这只是部分代码,剩余代码在以往博客中有。
以上是关于javaweb简单的增删改查之“模糊查询”的主要内容,如果未能解决你的问题,请参考以下文章