部分代码:它提示我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();
}
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();
}
那么你的sql语句变为
select * from bookinfo wheretitle=’title'
你说上面这个sql还错啊?
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 + "' ");
}
按你的代码最终where应该和下一个字符连在一起的。
你的:sb.append("title="+"'"+title+"'");
你可以把它改为
sb.append(" title="+"'"+title+"'");下面的author 这些条件依次类推
打印出来后 到数据库里面运行 你可以根据在数据库里面运行来判断你的字符串该如何组装 最好的就是想在数据库里面写sql得到你想要的结果 在进行组装
select * from bookinfo where 1=1 and title='JSP娣卞?ョ??绋?' and author='??????' and bookconcern='????????虹??绀? 是不是非法字符啊
检查一下你的SQL语句的空格."select * from bookinfo where" 这后面应该有空格.("title="+"'"+title+"'")这个title前后也应该有空格.我看你写的SQL里都没有注意空格.
SELECT FORM WHERE AND 等等这些SQL关键字建议用大写,容易发现错误.
出现乱码那就是你的页面编码有问题.先把所有的查询换成数值或英文,如果正常那就你的中文编码设置没有搞好.
2.然后输出sql
printBookInfo(out,rs);
out.close();在这段代码之前一行打印下你的sb.toString(),看看sql语句,
这是不是传说中的非法字符啊 我是不是还要写filter过滤呢?这个弄得焦头烂额的