日期:2014-05-18  浏览次数:20745 次

查询条件写得不对吗?
该代码段能正常显示所有记录,但按姓名查或按职称(下拉式列表)或按部门(下拉式列表)时均查不到记录,请问代码该如何修改呢?另外”修改“两个字怎么不是超链接的样式?
Java code

      String sqlString=" ";
      String teacherName=request.getParameter("teacherName");
      String technicalPosition=request.getParameter("technicalPositionSelect");
      String department=request.getParameter("departmentSelect");
                         
                    
      if(request.getParameter("teacherName")!=null && request.getParameter("teacherName")!="") 
          {     
               sqlString=sqlString+ "and T_name='teacherName'";
          }
      if(request.getParameter("technicalPositionSelect")!=null&&request.getParameter("technicalPositionSelect")!="")
          {    
              sqlString=sqlString + " and T_technicalposition='technicalPosition'";
          }
      if(request.getParameter("departmentSelect")!=null&&request.getParameter("departmentSelect")!="")
          {    
              sqlString=sqlString + " and T_department='department'";
          }
              
    ResultSet rs=db.executeQuery("select * from teacher "
                                      +" where 1=1 "
                                      +sqlString+" order by T_name desc "
        String teacherId="";
    while(rs.next())
    {
                
            teacherId=rs.getString("Id");
        out.println("<tr height=22>");
        out.println("<td align=center>"+rs.getString("T_name")+"</td>");
        out.println("<td align=center>"+rs.getString("T_technicalposition")+"</td>");
        out.println("<td align=center>"+rs.getString("T_department")+"</td>");
        %>
        <td align="center"<a href="teacherModify.jsp?teacherId=<%=teacherId%>">
修改</a>&nbsp;&nbsp;&nbsp;<a href="javascript:del('teacherManage.jsp',<%=teacherId%>)">删除</a></td></tr>
        }



------解决方案--------------------
<td align="center"<a href="teacherModify.jsp?teacherId=<%=teacherId%>">
错了
<td align="center"><a href="teacherModify.jsp?teacherId=<%=teacherId%>">修改</a>

查不出来的原因是因为你这个sqlString初值是不是有个空格?所以最后的sql不正确了。
你在执行前把这句SQL打出来看看,可能拼错了。dubug一下吧。

还有楼主的头像挺不错,我喜欢,呵呵。