当前位置: 技术问答>java相关
怎样实现模糊查询?请赐教!!!100分
来源: 互联网 发布时间:2015-05-31
本文导语: 我怎样才能实现模糊查询? 比如说我现在有四个变量: String id=request.getParameter("txtStu_id").trim(); String name=request.getParameter("txtName").trim(); String grade=request.getParameter("dwnGrade").trim(); String department=request...
我怎样才能实现模糊查询?
比如说我现在有四个变量:
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,随便一个变量有值,查出含有此内容的所有记录。
高手有礼了!
比如说我现在有四个变量:
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,随便一个变量有值,查出含有此内容的所有记录。
高手有礼了!
|
没理由呀,
四个与一个是一样的呀
String sql="select * from table"
if (id==null){
sql=sql+ "where id like '%"+id+"%'";}
四个与一个是一样的呀
String sql="select * from table"
if (id==null){
sql=sql+ "where id like '%"+id+"%'";}
|
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);
这样你先试一试,如果还不行的话,请你告诉我具体是什么情况。
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);
这样你先试一试,如果还不行的话,请你告诉我具体是什么情况。
|
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);
试试
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);
试试
|
构造SQL语句时根据这四个参数是否为空来构造不行吗?
如:
String sql="select * from tablename ";
String temp="";
if(id!=nul && !id.equals("")) {
temp+=" id="+id;
}
if(name!=null && !name.equals("")) {
if(temp.length
如:
String sql="select * from tablename ";
String temp="";
if(id!=nul && !id.equals("")) {
temp+=" id="+id;
}
if(name!=null && !name.equals("")) {
if(temp.length