用户在单个输入框输入多个数据进行查询,可以查询某个字段同时包含多个关键字,例如查询主题中既包含“java”又包含“C++”的记录,或者一个数字区间,例如年龄在19和22之间的记录,或者一个时间范围,例如出生在1981年1月1日和1983年12月31日之间的记录。
这里只介绍按照数字区间进行查找的,其它情况的实现基本相同。
处理文件:
<%@ page contentType="text/html;charset=gb2312"%> <% StringBuffer sql=new StringBuffer(); //查询字符串 String condition = request.getParameter("condition"); //获取查询条件 int index = condition.indexof(" "); //查找空格位置,空格作为两个数字的分隔符 String min=condition.subString(1,index); //前面的值 String max=condition.subString(index+1,condition.length-1); //后面的值 try { Integer.parseInt(min); Integer.parseInt(max); }catch(Exception e) { out.println("输入的信息不合法!"); return; } sql.append("select * from user where age between "); sql.append(min); sql.append(" and "); sql.append(max); //构造查询字符串 //接下来根据上面生成的sql进行查询即可 %>