http://community.csdn.net/Expert/topic/3691/3691688.xml?temp=.9523126

解决方案 »

  1.   

    select * from 表名 where 1 = 1然后用一个循环把用户输入的条件(加and)连接起来。
      

  2.   

    public String makesql(String staffname,String sax,String idcardno,String bthdate,String nation,String nativeplace){
        boolean ifand=false;    String sql="select staffname,sax,idcardno,bthdate,nation,nativeplace";
       
               sql+=" from bs_staffinfo where ";    if (!staffname.equals("")){
            sql+=" staffname='"+staffname+"' ";
            ifand=true;
        }
        if (!sax.equals("")){
            if (ifand)  sql+=" and ";
            sql+=" sax="+sax;
            ifand=true;
        }
        if (!idcardno.equals("")){
            if (ifand)  sql+=" and ";
            sql+=" idcardno ='"+ idcardno+"'";
            ifand=true;
        }if (!bthdate.equals("")){
            if (ifand)  sql+=" and ";
            sql+=" bthdate ='"+ bthdate+"'";
            ifand=true;
        }if (!nation.equals("")){
            if (ifand)  sql+=" and ";
            sql+=" nation ='"+ nation+"'";
            ifand=true;
        }if (!nativeplace.equals("")){
            if (ifand)  sql+=" and ";
            sql+=" nativeplace ='"+ nativeplace+"'";
            ifand=true;
        }
        return sql;
      }我最近刚写的一个例子,这个是一个方法在前面还经过判断,如果是NULL的就转换成“”;
    <%!public String handleNullVal(String str2handle){
    if (str2handle==null)
    return "";
    else
    return str2handle;
    }
    我实现了效果,希望你能看懂