首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

一个多条件模糊查询有关问题

2012-01-05 
一个多条件模糊查询问题?比如我现在有2个接收参数a,b如果a为空就过滤掉只查询b,如果b为空就过滤掉只查询a,

一个多条件模糊查询问题?
比如我现在有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(request.getParameter( "textfield2 "));

String sql = "select * from grubby ";


String where = "where 1=1 ";//相当于没有查询条件

ResultSet rs1 = null;

//当a为null 或a为空“”时则不加入条件,b同理
if ( bianma != null || !(bianma.tirm().equals( " ")))
where = "wuliaobianma like '% "+bianma+ "% ' ";

if ( pinming != null || !(pinming.tirm().equals( " ")))
where = where + "and pinming like '% "+pinming+ "% ' ";

sql = sql + where;

rs2=connBean.executeQuery(sql);

..........
..........

if(rs1 != null){
rs1.previous();
while(rs1.next()){ .....

[解决办法]
二楼回答的没问题!
[解决办法]
String strSQL= "select * from article where 1=1 ";
if(!a.equals( " ")) strSQL=strSQL+ " a的条件 ";
if(!b.equals( " ")) strSQL=strSQL+ " b的条件 ";

热点排行