关于select表单的 查询问题!

Pol 2008-01-06 04:19:27
如下面代码:


<table width="100%" border="0">
<tr>
<td height="20" valign="middle" bgcolor="#FFFFFF"><form id="form" name="form" method="post" action="query.jsp">
<p><span class="STYLE24">                  图书搜索</span>
<select name="select" size="1" id="select">
<option selected="selected">书名</option>
<option>作者</option>
<option >出版社</option>
<option>ISBN</option>
</select>
<input name="textinput" type="text" id="textinput" size="60" />
<span class="STYLE21"><a href="javascript:check()" ><img src="img/zongyemian/Button_sousuo.gif" width="72" height="22" /></a>   <a href="#"><strong>高级搜索</strong></a></span> </p>
</form> </td>
</tr>
</table>


JSP
<%@   page   contentType="text/html"   pageEncoding="GB2312"%> 
<%@ include file="datainit.jsp" %>
<html>
<body>

<%!
public String formatStr(String str){
return str==null?"":str;
}
%>

<%
String selected=formatStr(request.getParameter("select"));
String textinput=formatStr(request.getParameter("textinput"));
String strSql="";
if(textinput.equals("书名")){
strSql="select * from Book where BookName = '"+selected+"'";
}else if(textinput.equals("作者")){
strSql="select * from Book where Writer = '"+selected+"'";
}else if(textinput.equals("出版社")){
strSql="select * from Book where Publisher = '"+selected+"'";
}else{
strSql="select * from Book where BookID = '"+selected+"'";
}
%>

<%
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(strSql);
while(rs.next()){
out.print("<html><body>你查询的结果为:");
out.print(rs.getString(1));
out.print(rs.getString(2));
out.print("</body></html>");
}
out.print("没有您要找的书");
out.print(selected);
%>
</body></html>


为什么不管我查找什么都没有是执行没有找到? 还有返回的"selected"是乱码的/

应该怎么写了? 不会啊!
...全文
77 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
老紫竹 2008-01-06
  • 打赏
  • 举报
回复
<%@   page   contentType="text/html;charset=GBK"   pageEncoding="GBK"%> 
Pol 2008-01-06
  • 打赏
  • 举报
回复
谢谢您的回答.不过这样也不行的了.

提示错误:
cannot resolve symbol
symbol : variable select
location: class org.apache.jsp.query_jsp
if(select.equals("涔??")){


现在好了.是我自己粗心大意的把这里弄错了.
strSql="select * from Book where BookName = '"+selected+"'";
应该是:"
strSql="select * from Book where BookName = '"+textinput+"'";
这样才对了,谢谢您们了.
老紫竹 2008-01-06
  • 打赏
  • 举报
回复
呵呵!楼上正解,你把搜索的关键字给弄反了哦!
wanhao3g 2008-01-06
  • 打赏
  • 举报
回复
if(select.equals("书名")){
strSql="select * from Book where BookName = '"+textinput+"'";
}else if(select.equals("作者")){
strSql="select * from Book where Writer = '"+textinput+"'";
}else if(select.equals("出版社")){
strSql="select * from Book where Publisher = '"+textinput+"'";
}else{
strSql="select * from Book where BookID = '"+textinput+"'";
}
试试看
Pol 2008-01-06
  • 打赏
  • 举报
回复
谢谢您的回答 .照您说的加了一条代码,现在已经解决乱码的问题了.
不过我写的查询是不是哪里有问题? 不能实现在库中查询记录啊.
高手帮我看看应该怎么个修改了.?
谢谢了.
老紫竹 2008-01-06
  • 打赏
  • 举报
回复
<%@   page   contentType="text/html"   pageEncoding="GB2312"%> 
<%request.setCharacterEncoding("GBK");%>
<%@ include file="datainit.jsp" %>

81,116

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧