求助模糊查询~~望高手指导!!!

csyd1101 2010-06-12 08:07:22
通过表单提交查询条件,文本框内容什么都不填写的时候就查询所有的,然后文本框输入什么条件就查询相应的内容!我就是不会控制那个根据不同的提交条件变换SQl语句,然后查询以表格显示!!望指点~~~(javabean+jsp+mysql)

用到的代码如下——
message.java


package supermarket;

import java.sql.*;
import java.util.*;

public class message {



private String goodsID,supplierID,gname,barCode,metricUnit,kind,birthPlace;//表的相应信息
private double unitCost;

private double checkMoneyB,checkMoneyS;//单价大于、单价小于查看(获取从表单提交的信息)

private String sql;//储存sql语句的

private boolean flag;





public message() {

}


public ArrayList<message> checkMessage()//返回查询结果的方法
{
message cm=null;
ArrayList<message> list=new ArrayList<message>();

try {


//这里控制相应的sql语句


login check=new login();//此类里面有连接数据库和查询方法
System.out.println(sql);
ResultSet rs=check.executeQuery(sql);


while (rs.next())
{
cm=new message();

cm.setGoodsID(rs.getString(1));
cm.setSupplierID(rs.getString(2));
cm.setGname(rs.getString(3));
cm.setBarCode(rs.getString(4));
cm.setMetricUnit(rs.getString(5));
cm.setKind(rs.getString(6));
cm.setBirthPlace(rs.getString(7));
cm.setUnitCost(rs.getDouble(8));

list.add(cm);



}
rs.close();


}catch (SQLException e) {

e.printStackTrace();

}

return list;
}


public String getBarCode() {
return barCode;
}

public void setBarCode(String barCode) {
this.barCode = barCode;
}

public String getBirthPlace() {
return birthPlace;
}

public void setBirthPlace(String birthPlace) {
this.birthPlace = birthPlace;
}



public String getGname() {
return gname;
}


public void setGname(String gname) {
this.gname = gname;
}


public String getGoodsID() {
return goodsID;
}

public void setGoodsID(String goodsID) {
this.goodsID = goodsID;
}

public String getKind() {
return kind;
}

public void setKind(String kind) {
this.kind = kind;
}

public String getMetricUnit() {
return metricUnit;
}

public void setMetricUnit(String metricUnit) {
this.metricUnit = metricUnit;
}

public String getSupplierID() {
return supplierID;
}

public void setSupplierID(String supplierID) {
this.supplierID = supplierID;
}

public double getUnitCost() {
return unitCost;
}

public void setUnitCost(double unitCost) {
this.unitCost = unitCost;
}

public double getCheckMoneyB() {
return checkMoneyB;
}

public void setCheckMoneyB(double checkMoneyB) {
this.checkMoneyB = checkMoneyB;
}

public double getCheckMoneyS() {
return checkMoneyS;
}

public void setCheckMoneyS(double checkMoneyS) {
this.checkMoneyS = checkMoneyS;
}

public void setFlag(boolean flag) {
this.flag = flag;
}




这个是提交的表单样式message.jsp——

<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%request.setCharacterEncoding("gb2312"); %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>message.jsp</title>
<%--信息管理模块主页面 --%>

</head>
<body>
<table width="634" height="441" border="0" align="center" background="bg02.jpg">
<tr>
<td width="364" height="118">
<%--根据不同条件查询商品信息 --%>
<div class="check" style=" height:100pt;">
<p><font size="+3" ><b>商品信息查询</b></font></p>
<p>
<form action="checkgoods.jsp" method="post" name="f1">
<b>查询所有商品信息:</b><input type="hidden" name="all" value="all"/><br>


<b>按商品名称查询:</b><input type="text" name="gname" /><br>


<b>按商品类别查询:</b><select name="kind">
<option value="饮品" selected="selected">饮品</option>
<option value="日用品">日用品</option>
<option value="食品">食品</option>
</select><br>

<b>按商品的单价查询:</b>
<p><b>大于<input type="text" name="checkMoneyB" />元</b><br>

<b>小于<input type="text" name="checkMoneyS" />元</b><br>
<input type="submit" name="submit" value="查询"/>
<input type="reset" name="reset" value="修改"/>
</form>

</p>
</div>
</td>
<td width="260">
<div class="add" style="height:30pt; width:100pt; margin:220pt 30pt 50pt 40pt;" >
<%--点击进入添加商品页面--%>
<a href="goods.jsp"><font size="+2" style=" color:#006600; font-weight:bolder;">添加商品</font></a><br>
<a href="supermarket.jsp"><font size="+2" style=" color:#006600; font-weight:bolder;">返回主页面</font></a>
</div>
</td>
</tr>

</table>
</body>
</html>




这个是表单信息提交处理页面——
checkgoods.jsp


<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="gb2312"%>
<%request.setCharacterEncoding("gb2312"); %>
<%@page import="java.util.ArrayList"%>
<%@page import="supermarket.message"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>check.jsp</title>
<jsp:useBean id="check" scope="request" class="supermarket.message">
<jsp:setProperty name="check" property="*"/>
</jsp:useBean>
</head>
<body>
<%--处理商品查询条件的页面 --%>


<%

try
{

ArrayList list=check.checkMessage();
int length=list.size();

if(length==0)
{
%>
<script type="text/javascript">

alter("查无此商品信息!");


</script>



<% }

else
{

%>
<table width="634" border="0">
<tr>

<th>商品编号</th>
<th>供货商号;</th>
<th>名称</th>
<th>条形码</th>
<th>计量单位</th>
<th>类别</th>
<th>产地</th>
<th>单价</th>
</tr>

<%
for(int i=0;i<length;i++)
{
message m=(message)list.get(i);

%>
<tr>

<td><%=m.getGoodsID() %></td>
<td><%=m.getSupplierID() %></td>
<td><%=m.getGname()%></td>
<td><%=m.getBarCode() %></td>
<td><%=m.getMetricUnit() %></td>
<td><%=m.getKind() %></td>
<td><%=m.getBirthPlace() %></td>
<td><%=m.getUnitCost() %></td>
<td>
<a href='delgoods.jsp'?goodsID=<%=m.getGoodsID() %>>删除</a>
<a href='modify.jsp'?goodsID=<%=m.getGoodsID() %>>修改</a> </td>
<%System.out.print(m.getGoodsID()); %>


</tr>
<%

}

%>
</table>
<%

}
}catch(NullPointerException e)
{
e.printStackTrace();
}
%>
<a href="message.jsp">返回信息管理主页面</a>
<a href="stock.jsp">查看商品库存</a>
</body>
</html>
...全文
306 35 打赏 收藏 转发到动态 举报
写回复
用AI写文章
35 条回复
切换为时间正序
请发表友善的回复…
发表回复
kuailexiaobuding4 2010-06-14
  • 打赏
  • 举报
回复
/**
* 根据根据userPid、交易状态、交易时间、交易流水号进行查询多个条件查询根据页面的下拉列表获取的值。可以不选查询也可以都选查询随便选择。
*/
public List searchSucc(String userPid, int tradeStatus, String tradeTime,
String tradeNumber) {
Criteria c = sf.getCurrentSession().createCriteria(Shopnote.class);// 初始化Criteria
if(userPid!=null&&
!userPid.equals("")){
c.add(Restrictions.like("userPid",userPid,MatchMode.ANYWHERE));//根据userPid查询
}
if(tradeStatus>-1){
c.add(Restrictions.sqlRestriction("tradeStatus ="+tradeStatus));//根据交易状态查询
}
if(tradeTime!=null&&
!tradeTime.equals("")){
c.add(Restrictions.like("tradeTime",tradeTime,MatchMode.ANYWHERE));//根据交易时间查询
}
if(tradeNumber!=null&&
!tradeNumber.equals("")){
c.add(Restrictions.like("tradeNumber",tradeNumber,MatchMode.ANYWHERE));//根据交易流水号查询
}
return c.list();
}
qiheia 2010-06-14
  • 打赏
  • 举报
回复
select * from Goods where 1=1 and gName like '%null%' and kind like '%null%' and metricUnit >%0.0% and metricUnit <%0.0%
%号是用在模糊查询中的

你这个goodsID应该要在引号内,要不然找不到变量
<a href='delgoods.jsp'?goodsID=<%=m.getGoodsID() %>>删除</a>
<a href='modify.jsp'?goodsID=<%=m.getGoodsID() %>>修改</a>
csyd1101 2010-06-13
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 hell_liul 的回复:]
没看代码,
主要就是把你值都传过去之后进行拼串,拼成相应的sql查询条件就行了, 你试着先拼出来一个,拼出之后再考虑优化的问题!!
[/Quote]

我试了,可是不管传过来的是什么值,拼串涉及的每个列值,都存在。就是我提交的是所有的都不填的话,提交的应该是空值吧?我已经设置了空值时候它不拼接,可是它还是会拼接所有的。比如先查最简单是就是什么条件都不输入。提交的都为空值,那应该都不拼接对吧?就是select * from Goods where 1=1 。可是它实际出来的是——

select * from Goods where 1=1 and gName like '%null%' and kind like '%null%' and metricUnit >%0.0% and metricUnit <%0.0% 一直查不出来吖~~谢谢你,帮我解决一下吧~~
等待逆袭 2010-06-13
  • 打赏
  • 举报
回复
没看代码,
主要就是把你值都传过去之后进行拼串,拼成相应的sql查询条件就行了, 你试着先拼出来一个,拼出之后再考虑优化的问题!!
Happy_of_smile 2010-06-13
  • 打赏
  • 举报
回复
public ArrayList<message> checkMessage()//返回查询结果的方法
这个方法带一个message 对象public ArrayList<message> checkMessage(Message message)
if(message!=null){
if(message.get***){
sql+=" and ***="+message.get***();
}
csyd1101 2010-06-13
  • 打赏
  • 举报
回复
[Quote=引用 29 楼 zktbduandd 的回复:]
什么年代还用javabean和jsp
[/Quote]


额。。。老师就是这样教的,初次接触,也就只能先把这个搞明白了~~嘿嘿
lblblb1111 2010-06-13
  • 打赏
  • 举报
回复
用ibatis啊
如这样配置
<select id="qlCount" parameterClass="com.zwdt.core.PageModel" resultClass="java.lang.Integer">
select count(*) from ql_inf_inter
<dynamic prepend="where">
<isNotEmpty prepend="AND" property="levelLB">
QL_KIND like '%'||#levelLB#||'%'
</isNotEmpty>
<isNotEmpty prepend="AND" property="ywml">
TRANSACT_DEPNAME like '%'||#ywml#||'%'
</isNotEmpty>
<isNotEmpty prepend="AND" property="emailID">
QL_REG_ID like '%'||#emailID#||'%'
</isNotEmpty>
<isNotEmpty prepend="AND" property="content">
QL_NAME like '%'||#content#||'%'
</isNotEmpty>
<isNotEmpty prepend="AND" property="region">
EXT_DQBM like '%'||#region#||'%'
</isNotEmpty>
</dynamic>
</select>
csyd1101 2010-06-13
  • 打赏
  • 举报
回复
[Quote=引用 26 楼 ivorytower 的回复:]
引用 17 楼 csyd1101 的回复:

嗯嗯,可是打印出来的语句是——比如我要查全部信息,就什么都不填写对吧?可是打印出来的语句是
select * from Goods where 1=1 and gName like '%null%' and kind like '%null%' and metricUnit >%0.0% and metricUnit <%0.0%肯定查不到吖
……
[/Quote]
谢谢我试试~~~
zktbduandd 2010-06-13
  • 打赏
  • 举报
回复
什么年代还用javabean和jsp
hfCoder 2010-06-13
  • 打赏
  • 举报
回复
u p
falconkabu 2010-06-13
  • 打赏
  • 举报
回复
用 like
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 csyd1101 的回复:]

嗯嗯,可是打印出来的语句是——比如我要查全部信息,就什么都不填写对吧?可是打印出来的语句是
select * from Goods where 1=1 and gName like '%null%' and kind like '%null%' and metricUnit >%0.0% and metricUnit <%0.0%肯定查不到吖
[/Quote]
你这个明显是拼错了sql呀。我说的拼法,有个前提就是你要先判断该字段有没null,如果不是null,你在使用
sql += "and kind like '%"+kind+"%'";

构成一条完整的sql查询语句。判空的如6楼所写的。只是最后调用一下
sb.toString();

就行了
zhou961270 2010-06-13
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 csyd1101 的回复:]
引用 11 楼 ivorytower 的回复:
引用 9 楼 csyd1101 的回复:

引用 5 楼 zhuxibang 的回复:
String sql = "select * from 表名 where 1=1";
if(gname!=null&amp;amp;amp;&amp;amp;amp;!"".equal(gname)){
sql = sql +" and……
[/Quote]

我晕拉,你不知道用空判断一下,如果是空你后面的就不会加上去了
if(gname!=null&amp;amp;amp;&amp;amp;amp;!"".equal(gname)){
如果已经这样判断了,你的值是空你还能进这个if吗?进不了那你的sql语句怎么会加进去
w111222j 2010-06-13
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 zhuxibang 的回复:]
String sql = "select * from 表名 where 1=1";
if(gname!=null&&!"".equal(gname)){
sql = sql +" and gname="+gname;
}
....其他的条件类似上面的判断
这样就能实现都不输查全部,输入哪个条件就按照哪一个查询
[/Quote]

用IF判断来修改SQL语句再执行
red_chenxi 2010-06-13
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 zhuxibang 的回复:]
String sql = "select * from 表名 where 1=1";
if(gname!=null&&!"".equal(gname)){
sql = sql +" and gname="+gname;
}
....其他的条件类似上面的判断
这样就能实现都不输查全部,输入哪个条件就按照哪一个查询
[/Quote]

正解
csyd1101 2010-06-13
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 you_xin 的回复:]
hql = "select goodsID,supplierID,gname,barCode,metricUnit,kind,birthPlace from 表名 where 1=1 and goodsID='%"+goodsID+"%'";
[/Quote]

额。。。谢谢了!可是貌似不是我要的效果~~还是谢谢你回答了!!!
you_xin 2010-06-13
  • 打赏
  • 举报
回复
hql = "select goodsID,supplierID,gname,barCode,metricUnit,kind,birthPlace from 表名 where 1=1 and goodsID='%"+goodsID+"%'";
csyd1101 2010-06-13
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 redsnow_007 的回复:]
String gname = request.getParameter("gname");
String kind= request.getParameter("kind");
String checkMoneyB = request.getParameter("checkMoneyB");
String checkMoneyS = request.getParameter("checkMo……
[/Quote]


这个没用过吖,改天试试~~谢谢你的回复吖!!!
csyd1101 2010-06-13
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 foolishdault 的回复:]
模糊原则
前后加%
然后全部转成大写,数据库也大写,条件也要大写
[/Quote]

你说的大写是什么?所有字段的名字吗?还有输入的值也是?
铁匠梁 2010-06-13
  • 打赏
  • 举报
回复
模糊原则
前后加%
然后全部转成大写,数据库也大写,条件也要大写
加载更多回复(14)

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧