jsp如何实现模糊查询

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jsp如何实现模糊查询相关的知识,希望对你有一定的参考价值。

怎么样能够使用jsp实现模糊查询的功能?
。。。。。。。%我也知道啊,可是如果是输入了多个关键字呢?这才是问题的关键啊、、、、

jsp模糊查询是根据页面上输入的关键字进行部分匹配来实现的。分为前向检索和后项检索。
这个主要是在sql端实现的,只要页面参数选择正确:

比如:
查询所有员工姓名中第二个字符为A的员工信息
select * from emp where ename like \'_A%\';
查询员工姓名中包含E的员工信息
select * from emp where ename like \'%E%\';
查询20部门员工姓名中包含E的员工信息
查询20部门员工,职位为\'MANAGER\',姓名中包含E的员工信息
查询20部门员工,职位为\'MANAGER\',姓名的第二个字符为A,最后一个字符为E的员工信息
查询20部门员工,职位为\'MANAGER\',姓名的包含S并且包含E的员工信息
参考技术A   比如:
  查询所有员工姓名中第二个字符为A的员工信息
select * from emp where ename like '_A%';
查询员工姓名中包含E的员工信息
select * from emp where ename like '%E%';
查询20部门员工姓名中包含E的员工信息
查询20部门员工,职位为'MANAGER',姓名中包含E的员工信息
查询20部门员工,职位为'MANAGER',姓名的第二个字符为A,最后一个字符为E的员工信息
查询20部门员工,职位为'MANAGER',姓名的包含S并且包含E的员工信息
参考技术B 这个和一般的查询功能类似,只不过在sql语句里加上条件而已。
比如你在javabean里写一个专门用于查询的方法,这个方法需要传1个String变量进去,方法的返回类型是ArrayList.
先建立与数据库的链接,在查询时使用
PreparedStatement ps = conn.prepareStatement("select * from student where sname=?");
//name是你从外面传进来的值,也就是文本框所输入的查询内容
ps.setString(1,"%"+name+"%");
ResuleSet rs = ps.……省略
明白?
参考技术C 补充:
lz是否指在一个文本框中输入多个关键字?如果是的话,用分隔符(一般是逗号)把这几个关键词分解出来:
conditions.split(" ");
然后把每个关键词都拼接到查询语句中去就行了。最后拼接的过程如:
where columnname like "'%" + condition[0] +"%'" and columnname like "'%" + condition[1] +"%'"
===================
如果lz是使用数据库,那么查询时,使用 where columnname like "'%" + condition +"%'"就可以了,%是通配符本回答被提问者采纳
参考技术D 对于模糊查询T-SQL有四个通配符
%:包含0个或多个字符
_:匹配指定;
[]:指定范围
[^]:排除范围
对于稍复杂点的关键词搜索(比如新闻),常用的办法是在数据库中添加一个keyword字段,来配合通配符进行模糊查询或分类查询或热门关键字查询。关于复杂点的模糊查询,更好的方法是不用这些通配符来实现,而是通过js来实现(例如输入时产生搜索提示),或其他方法来作,思路是尽量少的对数据库进行操作。
    官方服务
      官方网站官方网站

JS、AJAX等实现:HTML/JSP页面中即时显示查询的数据,怎么写?

比如在查询框中输入“你好”,那么在数据库或table或div数据中含有“你好”这两个字的数据都列出来,模糊查询,不用点击查询按钮,你每输入一个字就马上列出符合条件的,怎么做?谢谢

struts2 action 代码import java.io.IOException;
import java.io.PrintWriter;public class AutoComplete extends CommonAction /**
* 用于实践ajax google 的样式
* 用于接收服务器端请求的
*/
//抓取从页面穿过来的字符串 用于和服务器端的单词进行匹配
private String word ;

public AutoComplete()
public String onblurquery() throws Exception
//保存要查询的东西
//注意ajax 中 这个所谓的视图层不返回页面 只返回数据
this.getRequestMap().put("word", word);
//System.out.println("struts - > "+word);
this.getResponse().setContentType("text/xml;charset=gb2312;");
return "toshow";


public String getWord()
return word;
public void setWord(String word)
this.word = word;
jsp 代码<%@ page contentType="text/html; charset=gb2312"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>google</title>

<meta http-equiv="description" content="This is my page">
<style type="text/css">
/*当文本没有被选中的时候就使用这个样式*/
.auto-1
background-color: #FFCC99;
color: gray;
cursor: pointer;
width: 100%;

/*当文本被选中的时候就是用这个样式*/
.auto-2
background-color: #CCFF99;
color: green;
cursor: pointer;
width: 100%;

</style>

<script type="text/javascript" src="employees/jquery.js"></script>
<script type="text/javascript" src="employees/auto.js"></script>

</head>

<body>
google:
<input type="text" id="word">
<input type="button" value="查询" id="chk"><br>
<div id="auto"></div>
</body>
</html>jquery 代码 //表示当前被选中的节点
var highlightindex = -1;
var timeoutId;
$(document).ready(function ()
//alert("准备好了");
//保存文本输入框
var wordinput=$("#word");
//保存文本的
var wordinputOffset = wordinput.offset();
//弹出框应该等于在文本的下面 那么就是文本的宽等于div的宽
$("#auto").hide().css("border","1px solid #CDD2CB").css("position","absolute")
.css("top",wordinputOffset.top+wordinput.height()+5+"px").css("left",wordinputOffset.left+"px").width(wordinput.width()+40+"px");
//给文本框添加键盘按下并谈起的实践
$("#word").keyup(function (event)
//处理键盘实践
var myEvent = event || window.event;
//如果输入的是字母 应该是将文本中最新的信息发送到服务器
//如果是退格或是删除键 那么就将文本中最新的信息发送给服务器
var keyCode = myEvent.keyCode;
if(keyCode >= 65 && keyCode <= 90 || keyCode == 8 || keyCode == 46)
//1 得到文本框中的内容
var wordtext = $("#word").val();
if(wordtext!="")
//2 把这的信息网服务器中发送
window.clearTimeout(timeoutId);
//对发送到服务器进行交互延迟500毫秒 比秒打字太快了 没有抓取到
timeoutId = window.setTimeout(function ()

//第一个参数 请求的地址 第二个参数 传递的参数 第三个参数 回调函数 第四个参数 数据回传的数据类型
$.post("auto_onblurquery.aiyu",word:wordtext,function(data)
// 将dom对象data 转化成JQuery 对象昂
//alert(data);
var jqueryObj = $(data);
//alert(data);
// 到xml 中找到所有的woerd节点
var wordNode = jqueryObj.find("word");
//alert(wordNode);
var autoNode = $("#auto");
autoNode.html("");
// 遍历 所有恶woed 节点 取出 单词
wordNode.each(function (i)
//获取单词的内容
var wordN = $(this);
// 新建div节点 将单词放进去
//alert(wordN.text());
//将div节点加入到弹出框汇总
var newdivNode = $("<div>").attr("id",i);
newdivNode.addClass("auto-1").html(wordN.text()).appendTo(autoNode);
//给鼠标加入进入的时候就高亮
newdivNode.mouseover(function ()
if(highlightindex!=-1)
$("#auto").children("div").eq(highlightindex).removeClass("auto-2").addClass("auto-1");

highlightindex = $(this).attr("id");
$(this).removeClass("auto-1").addClass("auto-2");
);
//鼠标移出的加上的样式
newdivNode.mouseout(function ()
$(this).removeClass("auto-2").addClass("auto-1");
);
//鼠标点击的时候增加的样式
newdivNode.click(function ()
//取出文本的内容
var comtext = $(this).text();
$("#auto").hide();
//隐藏的时候就把节点重新的赋值
highlightindex = -1;

$("#word").val(comtext);
);
);

if(wordNode.length>0)
if($("#word").val()=="")
$("#auto").hide();
//隐藏的时候就把节点重新的赋值
highlightindex = -1;
else
$("#auto").show();

else
$("#auto").hide();
//隐藏的时候就把节点重新的赋值
highlightindex = -1;

//alert(data);
,"xml");

,500);

else
$("#auto").hide();
//隐藏的时候就把节点重新的赋值
highlightindex = -1;


else if(keyCode == 38 || keyCode == 40)
//如果是按得向上或是向下键
if(keyCode == 38)
//上
var autoNodes = $("#auto").children("div");
if(highlightindex!=-1)
autoNodes.eq(highlightindex).removeClass("auto-2").addClass("auto-1");
highlightindex--;
else
highlightindex = autoNodes.length-1;


if(highlightindex==-1)
//如果修改过后的索引为-1 则索引到最后的节点
highlightindex = autoNodes.length-1;

autoNodes.eq(highlightindex).removeClass("auto-1").addClass("auto-2");

if(keyCode == 40)
//下
var autoNodes = $("#auto").children("div");
if(highlightindex!=-1)
autoNodes.eq(highlightindex).removeClass("auto-2").addClass("auto-1");

highlightindex++;
if(highlightindex>=autoNodes.length)
highlightindex=0;

if(highlightindex==-1)
//如果修改过后的索引为-1 则索引到最后的节点
highlightindex = 0;

autoNodes.eq(highlightindex).removeClass("auto-1").addClass("auto-2");

else if(keyCode == 13)
//按下的回车
//下拉框中被选中有选中的东西
if(highlightindex!=-1)
//取出文本的内容
var comtext = $("#auto").children("div").eq(highlightindex).text();
$("#auto").hide();
//隐藏的时候就把节点重新的赋值
highlightindex = -1;
$("#word").val(comtext);
else
//下拉框中没有选中的东西
alert("文本框的["+$("#word").val()+"]被提交了");
$("#auto").hide();
$("#word").blur();


);

$("input[id='chk']").click(function ()
alert("文本框的["+$("#word").val()+"]被提交了");
);
); 最后返回xml 的代码<%@ page contentType="text/xml; charset=gb2312"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<words>
<s:if test="%'absolute'.startsWith(#request.word)">
<word>absolute</word>
</s:if>
<s:if test="%'anyone'.startsWith(#request.word)">
<word>anyone</word>
</s:if>
<s:if test="%'anything'.startsWith(#request.word)">
<word>anything</word>
</s:if>
<s:if test="%'apple'.startsWith(#request.word)">
<word>apple</word>
</s:if>
<s:if test="%'break'.startsWith(#request.word)">
<word>break</word>
</s:if>
<s:if test="%'boolean'.startsWith(#request.word)">
<word>boolean</word>
</s:if>
<s:if test="%'breach'.startsWith(#request.word)">
<word>breach</word>
</s:if>
</words>
参考技术A jsp页面,嵌入查询数据库,有点原始了你可以把数据库查询封装到一个bean里,然后提供一个查询并返回json的方法在你的ajax页面(这个页面是一个jsp文件),Import这个bean,然后实例化,并调用哪个方法即可大概是"<td>"+getDBJson(1)+“</td>” 参考技术B 这个很简单,ajax配置下jsp上的js就可以了,模糊查询我也做出来过,也在使用中 我有简单的ajax实现代码,可以给你...一两句说不清楚QQ84024257

以上是关于jsp如何实现模糊查询的主要内容,如果未能解决你的问题,请参考以下文章

jsp如何按指定条件进行模糊查询

JSP如何实现模糊查询,比如我有一个<input type="text" name="serach">

在JSP页面通过form表单传递5个模糊查询的条件 如何 在底层 写模糊查询 方法

JAVA项目/JSP页面 中 怎样实现模糊查询

jsp中要做一个 模糊查询 例如输入姓名时 输入“张”下拉框中即出现"张*、张**”等 代码该如何实现?

图书管理系统( JSP + JDBC + Servlet)实现-07:模糊查询功能