急,一个关于模糊查询的问题,求高手指教啊.(在线等待)

maihill 2006-04-05 12:42:14
正在编写一个高级查询时,遇到不能实现模糊查询的问题.这个Jsp代码如下:

<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="gb2312" import="com.jspdev.bean.*,java.util.*,java.io.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Insert title here</title>
</head>
<jsp:useBean id="treatise" class="com.jspdev.bean.TreatiseBean" scope="page"></jsp:useBean>
<body>
<%!
public String trans(String chi)
{
String result = "";
byte temp[];
try
{
temp=chi.getBytes("iso-8859-1");
result = new String(temp);
}
catch(UnsupportedEncodingException e)
{
System.out.println (e.toString());
}
return result;
}

public String codeStringRandom(String s)
{
String result="%"+s+"%";
return result;
}
%>
<center>
<% //KeywordId是关键字。
String KeywordId=request.getParameter("keywordId");
//Rule是查询字段。
String Rule=request.getParameter("selection");
//TypeId判断查询的类型。(用模糊查询还是精确查询)
String TypeId=request.getParameter("typeid");
String Sql=null;
if(KeywordId.equals(""))
{
out.println("<tr>");
out.println("<td bgcolor=#0033FF scope=col>"+"<div aligncenter>"+"</div>"+"</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td bordercolor=#000000>"+"<div align=center>");
out.println("<p>"+"<br>");
out.println("查询错误!用户没有提供查询的数据或数据库出错。"+"<br>");
out.println("<br>");
out.println("</p>");
out.println("</div>"+"</td>");
out.println("</tr>");
out.println("</table>");
}
else
{
if(Rule.equals("TeacherName"))
{
Rule="Teacher_no";
}
else
{
if(Rule.equals("Stu"))
{
Rule="Stu_no";
out.println("<h1>"+"按学生名查询得"+"</h1>");
}
else
{
if(Rule.equals("Num"))
{
Rule="Number";
out.println("<h1>"+"按项目名查询得"+"</h1>");
}
}
}
out.println("<table width=80% border=1 bgcolor=#0099CC>");
out.println("<tr bgcolor=#009966 bordercolor=#990066>");
out.println("<td>"+"Number"+"</td>"+"<td>"+"Subject"+"</td>"+"<td>"+"Putin"+"</td>"+"<td>"+"Guidance"+"</td>"+"<td>"+"StudentId"+"</td>"+"<td>"+"TeacherId"+"</td>");
out.println("</tr>");
if(TypeId.equals("Random"))
{
KeywordId=codeStringRandom(KeywordId);
}
KeywrodId=trans(KeywordId);
Sql=treatise.getSelection(Rule,KeywordId);
Collection treatises=treatise.getTreatiseforPro(Sql);
if(treatises.size()==0)
{
out.println("</table>");
out.println("没有数据"+"<br>");
}
else
{
Iterator it=treatises.iterator();
while(it.hasNext())
{
Treatise temp=(Treatise)it.next();
out.println("<tr bordercolor=#990066>");
out.println("<td>"+temp.getNumber()+"</td>");
out.println("<td>"+temp.getSubject()+"</td>");
out.println("<td>"+temp.getPutin()+"</td>");
out.println("<td>"+temp.getGuidance()+"</td>");
out.println("<td>"+temp.getStudentId()+"</td>");
out.println("<td>"+temp.getTeacherId()+"</td>");
out.println("</tr>");
}
out.println("</table>");
}
}
%>
<a href="index.html">Back</a>
</center>
</body>
</html>

它实现的JavaBean部分代码为:
public Collection getTreatiseforPro(String SQL)
{
Collection ret=null;
String condition=SQL;
try
{
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(condition);
ret=new ArrayList();
while(rs.next())
{
Treatise temp=new Treatise();
temp.setNumber(rs.getString("Number"));
temp.setSubject(rs.getString("Subject"));
temp.setPutin(rs.getString("IsPutin"));
temp.setGuidance(rs.getString("IsGuidance"));
temp.setStudentId(rs.getString("Stu_no"));
temp.setTeacherId(rs.getString("Teacher_no"));
ret.add(temp);
}
con.close();
}
catch(Exception e){
e.getMessage();
}
return ret;
}

public String getSelection(String Rule,String KeyWord)
{
String rule=Rule;
String keyword=KeyWord;
String sql=null;
if(rule.trim().equals("Teacher_no"))
{
sql="select * from Treatise where "+rule+" = (select "+rule+" from Teachers where Name like '"+keyword+"')";
}
else
{
if(rule.trim().equals("Stu_no"))
{
sql="select * from Treatise where "+rule+"=(select "+rule+" from Students where Name like '"+keyword+"')";
}
else
{
sql="select * from Treatise where "+rule+"=(select "+rule+" from Treatise where Subject like '"+keyword+"')";
}
}
return sql;
}
...全文
231 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
maihill 2006-04-07
  • 打赏
  • 举报
回复
各位高手,请教教小弟吧
gefengxztg 2006-04-07
  • 打赏
  • 举报
回复
为什么喜欢用else{if(){ }}这种句子啊
可能是我没读懂你的程序,是不是就是else if(){}啊
peijihui 2006-04-07
  • 打赏
  • 举报
回复
强,这么长,怎么看啊
xiongbing528 2006-04-07
  • 打赏
  • 举报
回复
看我的眼花啊。。
leojay1 2006-04-07
  • 打赏
  • 举报
回复
是不是没有用通配符哟?
yyfhz 2006-04-06
  • 打赏
  • 举报
回复
那你在JSP里把getSelection的返回值贴出来看看啊
maihill 2006-04-06
  • 打赏
  • 举报
回复
输出的SQL语句就如:select * from Treatise where Teacher_no = (select Teacher_no from Teachers where Name like '木')
没有执行了这个语句
if(TypeId.equals("Random"))
{
KeywordId=codeStringRandom(KeywordId);
}
zh029t 2006-04-06
  • 打赏
  • 举报
回复
是什么错
li_d_s 2006-04-06
  • 打赏
  • 举报
回复
看得好累,最后把sql print出来看看对不对啊
maihill 2006-04-05
  • 打赏
  • 举报
回复
就系不能实现查询,显示没有数据
langya333 2006-04-05
  • 打赏
  • 举报
回复
有没有错误信息啊。这样看代码太累了

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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