我用myeclipse做了个搜索栏,但发布之后,发现只输入关键字只能查询价格为0的商品,但是只输入价格区间可得到正确的结果,下面是我的代码: <%@ page language="java" import="java.util.*"  pageEncoding="GB18030"%> 
<%@ page import="java.sql.*" %> 
<html> 
  <head> 
    <title>My JSP 'ss.jsp' starting page </title> 
  </head> 
  <body> 
  <div  align="center"> 
        <form action="ss.jsp" method="post"  style="padding:10px"> 
        <fieldset>  
        <label>关键字 <input  type="text"name="goodsname"style="width:150px;" /> </label> 
        <label>价格从 <input type="text" name="price01" style="width:70"> </label> 
        <label>到 <input type="text" name="price02" style="width:70"> </label> 
        <button type="submit" name="search" style="font-weight:bold;width:100px" />搜 索 </button> </fieldset> </form> 
</div>   <%Connection conn=null; 
  Statement stat=null; 
  ResultSet rs=null; 
  String goodsname=null; 
  String price01=null; 
  String price02=null; 
  goodsname=request.getParameter("goodsname"); 
  price01=request.getParameter("price01"); 
  price02=request.getParameter("price02"); 
  String sql=null; 
  Class.forName("com.mysql.jdbc.Driver").newInstance(); 
  conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root",""); 
  stat=conn.createStatement(); 
  if ((price01!=null)&&(goodsname!=null)){ 
  sql="select * from goods where g_name like  '%"+goodsname+"%' and g_price between '"+price01+"'  and '"+price02+"'"; 
  } 
  if(price01==null) 
  {sql="select * from goods where g_name like  '%"+goodsname+"%' ";} 
  if(goodsname==null) 
  {sql="select * from goods where g_price between '"+price01+"' and '"+price02+"'";} 
  rs=stat.executeQuery(sql);%> 
    
<table cellSpacing="0" width="80%" align="center" style="font-size:10pt;border:1pt"> 
<tr style="border:solid 1pt"> 
        <td align="center" style="border:solid 1pt">商品名 </td> 
<td align="center" style="border:solid 1pt">价格 </td> 
<td align="center" style="border:solid 1pt">数量 </td> 
</tr> 
<% 
while(rs.next()){ 
out.print(" <tr style='border:solid 1pt'>"); 
out.print(" <td style='border:solid 1pt'>"+rs.getString("g_name")+" </td>"); 
out.print(" <td style='border:solid 1pt'>"+rs.getDouble("g_price")+" </td>"); 
out.print(" <td style='border:solid 1pt'>"+rs.getInt("g_number")+" </td>"); 
out.print(" </tr>"); 

if(rs!=null){ 
rs.close(); 

if(stat!=null){ 
stat.close(); 

if(conn!=null){ 
conn.close(); 

%> 
</table> 
</body> 
</html> 我建的数据库名称是goods,其中有varchar g_name,double g_price,int g_number,3个属性,在里面放入 
aaa,0,1    abc,100,0  cad,0,10    apple,200,10  
如果在关键字里输如a ,而价格区间不输入,得到的结果是,aaa,0,1      cad,0,10 
如果在价格区间输入0,200,得到的结果是 aaa,0,1    abc,100,0    cad,0,10    apple,200,10 
如果在关键字输入a,价格区间输入0,100 结果是 aaa,0,1    abc,100,0    cad,0,10 
请问这是什么原因?

解决方案 »

  1.   

    你把price01打印出来,就知道price01是不是为null了,然后你就知道自己错在哪里了
      

  2.   

    你把那个要执行的sql打印出来,一看便知
      

  3.   

    应该是你的sql语句有问题  你的大概意思是进行模糊查询吧   你的那个条件有问题 你用and的话 就是全部都要成立才可以进行模糊查询  但是你输入的条件跟你的sql语句就不匹配了 所以就查不出来  只有你输入的条件貌似相同的情况下才可以搜出来.
      

  4.   


    StringBuffer sqlStr=new StringBuffer();
    sqlStr.append("select * from goods ");
    if (("".equals(price01)&&price01!=null)&&("".equals(goodsname)&&goodsname!=null)){ 
    sqlStr.append("where g_name like  '%"+goodsname+"%' and g_price between '"+price01+"'  and '"+price02+"'"); 
        } 
        if("".equals(price01)||price01==null)
        { 
         sqlStr.append("select * from goods where g_name like  '%"+goodsname+"%' ";}
        }
        if("".equals(goodsname)||goodsname==null)
        { 
         sqlStr.append("where g_price between '"+price01+"' and '"+price02+"'");

        }
    rs=stat.executeQuery(sqlStr.toString());模糊查找的时候用好用concat方法来组合。%和变量。
      

  5.   

    对啊 你先打印出sql出来看下是不是你想要的答案 或者你也可以用debug试试
      

  6.   

    我在后面添加了out.print(goodsname);
    out.print(price01);
    out.print(price02);
    out.println(sql);
    发现如果只在关键字中输入a,得到的结果就是这样
    商品名 价格 数量 
    vax 0.0 10 
    xia 0.0 10 
    a  select * from goods where g_name like '%a%' and g_price between '' and '' 
    这里显示price01是空的啊,但是为什么不执行 
     if(price01==null) 
      {sql="select * from goods where g_name like  '%"+goodsname+"%' ";} 
    但好像它认为price01和price02为0;如果之在价格区间输入0,200,得到的是
    商品名 价格     数量 
    aaa   100.0     10 
    abc   50.0      10 
    zyx   0.0        0 
    swx   5.0        1 
    hwl   9.0        1 
    nxl   50.0       9 
    vax   0.0       10 
    xia   0.0       10 
    afrid 1.0        0 
          20.0      30 
          0.0        0 
    kuzi 100.0      10 
    0  100  select * from goods where g_name like '%%' and g_price between '0' and '100'  
    这里正确的执行了
    if(goodsname==null) 
      {sql="select * from goods where g_price between '"+price01+"' and '"+price02+"'";} 
      

  7.   

    我想到问题出在哪了,就是无论是否在文本框中输入值,goodsname和price01,price02都 不 会为null,所以一直在执行语句;
    if ((price01!=null)&&(goodsname!=null)){ 
      sql="select * from goods where g_name like  '%"+goodsname+"%' and g_price between '"+price01+"'  and '"+price02+"'"; 
      } 
    其他的两条if语句根本没起作用。request.getParameter()语句是不是不管文本框中有无输入都会返回一个值呢?
    请问有没有上面方法判断文本框中是否被输入值了呢。
    热切等待回答。
      

  8.   

    if ((price01!=null&&!price01.equals(""))&&(goodsname!=null&&!goodsname.equals(""))){ 
      sql="select * from goods where g_name like  '%"+goodsname+"%' and g_price between '"+price01+"'  and '"+price02+"'"; 
      } 
      

  9.   

    看了大家的回复,思量了好久终于想到了解决办法。将那段条件查询的代码改成如下就OK了
    if ((price01!="")&&(goodsname!="")){
      sql="select * from goods where g_name like  '%"+goodsname+"%' and g_price between '"+price01+"' and '"+price02+"'";
      }
      if(price01=="")
        {sql="select * from goods where g_name like  '%"+goodsname+"%' ";}
      if(goodsname=="")
        {sql="select * from goods where g_price between '"+price01+"' and '"+price02+"'";}
    呵呵!!现在看来问题很简单啊
      

  10.   

    给个建议你   对一些关键变量  做输出处理
      在出问题的时候  只需要到console中看看那些变量值是否是自己需要的就知道错误出在哪了