部分代码:它提示我sql语法错误,我找了好久 感觉好好地啊 可以查询到所有图书 但是当精确查找的时候 只能查到表头 没有内容 
if(condition.equals("all"))
{
rs=stmt.executeQuery("select * from bookinfo");
printBookInfo(out,rs);
out.close();
}
else if(condition.equals("precision"))
{
String title=req.getParameter("title");
String author=req.getParameter("author");
String bookconcern=req.getParameter("bookconcern");

if((null==title || title.equals("")) && (null==author || author.equals("")) && (null==bookconcern || bookconcern.equals("")))
{
resp.sendRedirect("search.html");
return;
}
StringBuffer sb=new StringBuffer("select * from bookinfo where");
boolean bFlag=false;
if(!title.equals(""))
{
sb.append("title="+"'"+title+"'");
bFlag=true;
}
if(!author.equals(""))
{
if(bFlag)
sb.append("and author="+"'"+author+"'");
else
{
sb.append("author= "+"'"+author+"'");
bFlag=true;
}
}
if(!bookconcern.equals(""))
{
if(bFlag)
sb.append("and bookconcern="+"'"+bookconcern+"'");
else
sb.append("bookconcern="+"'"+bookconcern+"'");
}
rs=stmt.executeQuery(sb.toString());
printBookInfo(out,rs);
out.close();
}

解决方案 »

  1.   

    如果title不为空
    那么你的sql语句变为
    select * from bookinfo wheretitle=’title'
    你说上面这个sql还错啊?
      

  2.   

    你说的我不太理解是什么意思  我的sb.append(“title=”+“‘“+title+”’”);有错么?
      

  3.   

    rs=stmt.executeQuery(sb.toString());你在这句前面输出sb.toString()看看结果
      

  4.   

    楼主那个写不累吗 
             StringBuffer sb = new StringBuffer("select * from bookinfo where 1=1 ");    
            if (!title.equals("")) {
                sb.append(" and title='"+title+"' ");             
            }
            if (!author.equals("")) {            
                sb.append(" and author='" + author + "' ");           
            }
            if (!bookconcern.equals("")) {            
                sb.append(" and bookconcern='" + bookconcern + "' ");          
            }
      

  5.   

    我意思是说你最后的拼接的sql是错误的(有语法错误)
    按你的代码最终where应该和下一个字符连在一起的。
      

  6.   

    我以title举例
    你的:sb.append("title="+"'"+title+"'");
    你可以把它改为
    sb.append(" title="+"'"+title+"'");下面的author 这些条件依次类推
      

  7.   

    按照你的这种方式,不用bFlag 也读取不出来 ╮(╯▽╰)╭
      

  8.   

    你在控制台打印一下你的sql语句,应该能发现问题
      

  9.   

    这样写好,你把最后的sql语句打印出来,放到数据库中去执行吧。然后现看看有没有适合条件的出来。
      

  10.   

    是不是out.println(sb.toString())我的好像打印不出来 没反应 只输出一个表头 没内容 
      

  11.   

    你在rs=stmt.executeQuery(sb.toString());前面打印就可以了吗,也许是你的sql语句有问题没有执行到下面。所以没有打印出来  
    打印出来后  到数据库里面运行  你可以根据在数据库里面运行来判断你的字符串该如何组装  最好的就是想在数据库里面写sql得到你想要的结果  在进行组装
      

  12.   

    你在这句 StringBuffer sb=new StringBuffer("select * from bookinfo where"); “where” 的后面加个空格 要不然 sb.append();的时候,where 和后面的条件连接到一起了。或者把“where” 改成“where 1=1 ”注意空格; 然后在后面的判断flag不需要了 直接判断条件是否为空 直接sb.append();就可以了。 你这里面的问题就是条件与条件之间要注意空格。
      

  13.   

    打印出来了 不过好像是乱码
    select * from bookinfo where 1=1 and title='JSP娣卞?ョ??绋?' and author='??????' and bookconcern='????????虹??绀?  是不是非法字符啊
      

  14.   

    select * from bookinfo where 1=1 and title='JSP娣卞?ョ??绋?' and author='??????' and bookconcern='????????虹??绀? 是不是非法字符啊  哥们  你乱码了  肯定查不出来呀  你全部用字母查询估计就行了  解决乱码才是王道啊
      

  15.   

    这样写组合查询.真难为楼主了.
    检查一下你的SQL语句的空格."select * from bookinfo where" 这后面应该有空格.("title="+"'"+title+"'")这个title前后也应该有空格.我看你写的SQL里都没有注意空格.
    SELECT FORM WHERE AND 等等这些SQL关键字建议用大写,容易发现错误.
    出现乱码那就是你的页面编码有问题.先把所有的查询换成数值或英文,如果正常那就你的中文编码设置没有搞好.
      

  16.   

    1.先解决乱码
    2.然后输出sql
      

  17.   

    rs=stmt.executeQuery(sb.toString());
    printBookInfo(out,rs);
    out.close();在这段代码之前一行打印下你的sb.toString(),看看sql语句,
      

  18.   

    你把 sql 复制下来,在 数据库中运行一下看包什么错误
      

  19.   

    select * from bookinfo where 1=1 and title='JSP娣卞?ョ??绋?' and author='??????' and bookconcern='????????虹??绀?
    这是不是传说中的非法字符啊 我是不是还要写filter过滤呢?这个弄得焦头烂额的