<html> 
<body> 
<center>查询书籍信息 
<hr size=5 color="445566">
 <form action="search.jsp"  method="post"> 
 <p><input type=submit value="查询所有"></p> 
</form> 
 <form action=result.jsp  method="post"> 
<p>请选择查询条件:<br> 
<select name="tiaojian" size=1> 
    <option  values="ID">ID</option> 
    <option    values="name">name</option> 
    <option   values="auther">auther</option> 
    <option   values="publisher">publisher</option> 
    <option   values="time">time</option> 
    <option    values="all">all</option> 
    </select></p> 
  <p>关键字<br> 
  <input type="text" name="keyword" size=20></p> 
   <p><input type=submit value=submit></p> 
</form> </center></body> </html>
 jsp的查询如下:
<%@ page contentType="text/html;charset=gb2312"%> 
<%@ page import="java.sql.*"%> 
<%@ include file="conn1.jsp"%>
 <%  String id=request.getParameter("ID");
              String name=request.getParameter("name");
String auther=request.getParameter("auther");
String publisher=request.getParameter("publisher");
String time1=request.getParameter("time");
String all=request.getParameter("all");
String keyword=request.getParameter("keyword");
String  id1="id";
 String  name1="name";
 String  auther1="auther";
 String  publisher1="publisher";
 String  time11=" time1";
 String  keyword1=" keyword";
 String  all1="all";
   if (id1==id)  {  String sql="select * from info where ID="+keyword;  }
     if(name1==name)  { String sql="select * from info where name="+keyword; }
     if(auther1==auther) { String sql="select * from info where auther="+keyword;  }
      if(publisher1==auther){ String sql="select * from info where  publisher="+keyword; }
     if(time11==time1) { String sql="select * from info where time="+keyword;}
     if(all1==all){  String sql="select * from info "; }
ResultSet rs=st.executeQuery(sql);
out.println("数据库操作成功,查询结果如下:"); 
out.println("<table  width=50% border=3> ");
out.println("<tr><td>"+"ID"+"</td><td>"+"name"+"</td><td>"+"auther"+"</td><td>"+"publisher"+"</td><td>"+"time"+"</td></tr>"); 
while(rs.next()) {
String col1 =rs.getString(1);
String col2 =rs.getString(2);
String col3 =rs.getString(3);
String col4 =rs.getString(4);
String col5 =rs.getString(5);
out.println("<tr><td>"+col1+"</td><td>"+col2+"</td><td>"+col3+"</td><td>"+col4+"</td><td>"+col5+"</td></tr>"); 
}
out.println("</table>");
{%>   <%}//这个是什么作用啊? 似乎很重要的语句
%> 
<% 
rs.close(); 
st.close(); 
con.close(); 
%>                   
<html>
<form  name="form" method="post" action="query.html">
      <input type="submit" name="summit" value="返回" >
</html>
 错误信息:
An error occurred at line: 5 in the jsp file: /result.jsp
Generated servlet error:
sql cannot be resolved感觉程序写的冗余代码比较多,希望大侠指点修正阿,拜谢!

解决方案 »

  1.   

    String sql = null;
     if (id1==id)  {  sql="select * from info where ID="+keyword;  }
         if(name1==name)  { sql="select * from info where name="+keyword; }
         if(auther1==auther) { sql="select * from info where auther="+keyword;  }
          if(publisher1==auther){ sql="select * from info where  publisher="+keyword; }
         if(time11==time1) { sql="select * from info where time="+keyword;}
         if(all1==all){  sql="select * from info "; }
      

  2.   

    楼上说得不错,你SQL定义得是局部变量,外面当然找不到了,要定义在外面,里面在赋值
      

  3.   

    改正后的错误是:
    javax.servlet.ServletException: Can not issue NULL query.
    我这个程序设计的好像有问题,请指教一下