怎样实现模糊查询?请赐教!!!100分

star123qiu 2002-05-14 07:53:17
我怎样才能实现模糊查询?
比如说我现在有四个变量:
String id=request.getParameter("txtStu_id").trim();
String name=request.getParameter("txtName").trim();
String grade=request.getParameter("dwnGrade").trim();
String department=request.getParameter("dwnDepartment").trim();
byte[] tmpbyte=id.getBytes("ISO8859_1");
id=new String(tmpbyte);
tmpbyte=name.getBytes("ISO8859_1");
name=new String(tmpbyte);
tmpbyte=grade.getBytes("ISO8859_1");
grade=new String(tmpbyte);
tmpbyte=department.getBytes("ISO8859_1");
department=new String(tmpbyte);
要实现在数据库中的模糊查询,要求是 1,四个变量为空时,查出所有记录,2,随便一个变量有值,查出含有此内容的所有记录。
高手有礼了!


...全文
30 点赞 收藏 9
写回复
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
star123qiu 2002-05-17
谢谢各位仁兄,我已经成功了!
回复
mingkeming 2002-05-16
用like不就行了
回复
Andrawu 2002-05-15
String id=request.getParameter("txtStu_id")==null?"":(request.getParameter("txtStu_id").equals("null")?"":request.getParameter("txtStu_id").trim());
String name=request.getParameter("txtName")==null?"":(request.getParameter("txtName").equals("null")?"":request.getParameter("txtName").trim());
String grade=request.getParameter("dwnGrade")==null?"":(request.getParameter("dwnGrade").equals("null")?"":request.getParameter("dwnGrade").trim());
String department=request.getParameter("dwnDepartment")==null?"":(request.getParameter("dwnDepartment").equals("null")?"":request.getParameter("dwnDepartment").trim());
byte[] tmpbyte=id.getBytes("ISO8859_1");
id=new String(tmpbyte);
tmpbyte=name.getBytes("ISO8859_1");
name=new String(tmpbyte);
tmpbyte=grade.getBytes("ISO8859_1");
grade=new String(tmpbyte);
tmpbyte=department.getBytes("ISO8859_1");
department=new String(tmpbyte);

String sStr = "select * from tablename " ;
String sCondition = "" ;
if (!id.equals(""))
sCondition +=" and (id like '%" + id + "%')";
if (!name.equals(""))
sCondition += " and ( name like '%" + name + "%')";
if (!grade.equals(""))
sCondition += " and ( grade like '%" + grade + "%')";
if (!department.equals(""))
sCondition += " and ( department = '%" + department + "')";
if (!sCondition.equals(""))
sStr +="where "+ sCondition.substring(5);

试试

回复
chen_guo_quan 2002-05-15
String id=request.getParameter("txtStu_id");
String name=request.getParameter("txtName");
String grade=request.getParameter("dwnGrade");
String department=request.getParameter("dwnDepartment");
byte[] tmpbyte;
if(id!=null)
{
tmpbyte=id.trim().getBytes("ISO8859_1");
id=new String(tmpbyte);
}

if(name!=null)
{
tmpbyte=name.trim().getBytes("ISO8859_1");
name=new String(tmpbyte);
}

if(grade!=null)
{
tmpbyte=grade.trim().getBytes("ISO8859_1");
grade=new String(tmpbyte);
}

if(department!=null)
{
tmpbyte=department.trim().getBytes("ISO8859_1");
department=new String(tmpbyte);
}

String sStr = "select * from tablename " ;
String sCondition = "" ;
if (id!=null&&!id.equals(""))
sCondition +=" and ( id like '%" + id + "%')";
if (name!=null&&!name.equals(""))
sCondition += " and ( name like '%" + name + "%')";
if (grade!=null&&!grade.equals(""))
sCondition += " and ( grade like '%" + grade + "%')";
if (department!=null&&!department.equals(""))
sCondition += " and ( department = '%" + department + "')";
if (!sCondition.equals(""))
sStr +="where "+ sCondition.substring(5);

这样你先试一试,如果还不行的话,请你告诉我具体是什么情况。
回复
zhu_liping 2002-05-14
没理由呀,
四个与一个是一样的呀
String sql="select * from table"
if (id==null){
sql=sql+ "where id like '%"+id+"%'";}
回复
star123qiu 2002-05-14
还是不行
回复
Andrawu 2002-05-14
sorry:
有点问题。

String id=request.getParameter("txtStu_id").trim();
String name=request.getParameter("txtName").trim();
String grade=request.getParameter("dwnGrade").trim();
String department=request.getParameter("dwnDepartment").trim();
byte[] tmpbyte=id.getBytes("ISO8859_1");
id=new String(tmpbyte);
tmpbyte=name.getBytes("ISO8859_1");
name=new String(tmpbyte);
tmpbyte=grade.getBytes("ISO8859_1");
grade=new String(tmpbyte);
tmpbyte=department.getBytes("ISO8859_1");
department=new String(tmpbyte);

String sStr = "select * from tablename " ;
String sCondition = "" ;
if (!id.equals(""))
sCondition +=" and (id like '%" + id + "%')";
if (!name.equals(""))
sCondition += " and ( name like '%" + name + "%')";
if (!grade.equals(""))
sCondition += " and ( grade like '%" + grade + "%')";
if (!department.equals(""))
sCondition += " and ( department = '%" + department + "')";
if (!sCondition.equals(""))
sStr +="where "+ sCondition.substring(5);


回复
Andrawu 2002-05-14
String id=request.getParameter("txtStu_id").trim();
String name=request.getParameter("txtName").trim();
String grade=request.getParameter("dwnGrade").trim();
String department=request.getParameter("dwnDepartment").trim();
byte[] tmpbyte=id.getBytes("ISO8859_1");
id=new String(tmpbyte);
tmpbyte=name.getBytes("ISO8859_1");
name=new String(tmpbyte);
tmpbyte=grade.getBytes("ISO8859_1");
grade=new String(tmpbyte);
tmpbyte=department.getBytes("ISO8859_1");
department=new String(tmpbyte);

String sStr = "select * from tablename where" ;
if (!id.equals(""))
sStr +=" and (id like '%" + id + "%')";
if (!name.equals(""))
sStr += " and ( name like '%" + name + "%')";
if (!grade.equals(""))
sStr += " and ( grade like '%" + grade + "%')";
if (!department.equals(""))
sStr += " and ( department = '%" + department + "')";
if (!sStr.equals(""))
sStr = sStr.substring(5);
回复
saintKnight 2002-05-14
构造SQL语句时根据这四个参数是否为空来构造不行吗?
如:
String sql="select * from tablename ";
String temp="";
if(id!=nul && !id.equals("")) {
temp+=" id="+id;
}
if(name!=null && !name.equals("")) {
if(temp.length<1) {
temp+=" name='"+name+"'";
}else{
temp+=" and name='"+name+"'";
}
if(grade!=null && !grade.equals("")) {
if(temp.length<1) {
temp+=" grade='"+grade+"'";
}else{
temp+=" and grade='"+grade+"'";
}
if(department!=null && !department.equals("")) {
if(temp.length<1) {
temp+=" department='"+name+"'";
}else{
temp+=" and department='"+department+"'";
}
if(temp.length>1) {
sql+=" where "+temp;
}
回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告