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

一个多条件模糊查询问题?
比如我现在有2个接收参数   a,b   如果a为空就过滤掉只查询b,如果b为空就过滤掉只查询a,2个都不是空就都查询该怎么做呀我搞了很久都搞不出
String   a=getStr(request.getParameter( "textfield "));
String   b=getStr(request.getParameter( "textfield2 "));
String   sql= " ";
String   sql2= " ";
ResultSet   rs1=null;
ResultSet   rs2=null;
if(bianma!=null&&pinming.equals( " ")){
  sql= "select   *   from   grubby   where   wuliaobianma   like '% "+bianma+ "% ' ";
    rs1=connBean.executeQuery(sql);}
  if(pinming!=null&&bianma.equals( " ")){
  sql2= "select   *   from   grubby   where   pinming   like '% "+pinming+ "% ' ";
                  rs2=connBean.executeQuery(sql);}
if(rs1.next()){   rs1.previous();
          while(rs1.next()){   .....
当rs1为空就报错   谁知道有什么好办法呀

------解决方案--------------------
public List getQylx(String id,String name){
DB db = null;
List queryList = new LinkedList();
ResultSet rs=null;
String strSql = "select * table_a where 1=1 ";

if (id.equals( " ") == false) { //单位名称
strSql = strSql + " and id = ' " + id+ " ' ";
}
if (name.equals( " ") == false) { //单位名称
strSql = strSql + " and name= ' " + name+ " ' ";
}

try {
db = DBFactory.getDBInstance();
rs = db.executeQuery(strSql);
while (rs.next()) {
//******
}
} catch (SQLException e) {
System.out.println(e.toString());
if(rs!=null)
try {
rs.close();
} catch (SQLException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
if(db!=null) db.close();
}finally{
if(rs!=null)
try{
rs.close();
}catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
if(db!=null) db.close();
}
return queryList;
}
------解决方案--------------------
String a= "11 ";
String b= "22 ";
ResultSet rs;
PreparedStatement pst;
if(a==null || a.equals( " ")){//如果A为空
if(b==null || b.equals( " ")){//如果B也为空
return;//不做查询
}else{//B不为空
pst=con.createStatement( "B有值的查询语句 ");
}
}else{//如果A有值
if(b==null || b.equals( " ")){//如果B为空
pst=con.createStatement( "A有值的查询语句 ");
}else{//B也有值
pst=con.createStatement( "A和B都有值的查询语句 ");
}
}
rs=pst.executeQuery();//下面做查询
------解决方案--------------------
有点不太明白你写的东西,既然a b为条件,可是程序里面并没有看到使用他们的地方。
我估计你是多条件参数组合查询的吧!

String bianma = getStr(request.getParameter( "textfield "));
String pinming = getStr(reques