或者这样:
sqlStr = "select * from table ";
if(key1!=null) {
  if(sqlStr.indexOf("where")>0) {
     sqlStr += " and key1=" + key1;
  } else {
     sqlStr += " where key1=" + key1;
  }
}
if(key2!=null) {
  if(sqlStr.indexOf("where")>0) {
     sqlStr += " and key2=" + key2;
  } else {
     sqlStr += " where key2=" + key2;
  }
}
if(key3!=null) {
  if(sqlStr.indexOf("where")>0) {
     sqlStr += " and key3=" + key3;
  } else {
     sqlStr += " where key3=" + key3;
  }
}
sqlStr += ";";

解决方案 »

  1.   

    但还是不够简单。
    而且没有办法使用prepareStatement,只能用executeQuery(sqlStr)来查询
      

  2.   

    你可以写到一起啊,在where后跟一个真值,如2>1,
    sqlStr = "select * from table where 2>1 and key1=" + key1 + " and key2=" + key2 + " and key3=" + key3 + ";";
    这样可以吧。如果害怕 key1 为null,你最好加一个判断。
    if(key1==null){
    key1="";
    }
      

  3.   

    sqlselct = "select * from table ";
    sqlStr = "";
    if(key1!=null) {
         sqlStr += " and key1=" + key1;
      }if(key2!=null) {
           sqlStr += " and key2=" + key2;
      }
    ....
    if len(sqlStr) > 0 {
       sqlStr = mid(sqlStr,5);
       sqlselct += " where " + sqlStr;
    }我不是学mysql的,大体就这个意思.len是求字符串长度的意思,mid()是从第几位去字符串到最后.
      

  4.   

    to chaozi(编程浪子) 如果key1==null的话
    sqlStr 不就是 select * from table where 2>1 and key1="" and key2=。显然语法错误to  double22822(大无忧-老实和尚(有事发消息)) :
    你的写法不是
    select * from table where and key1=......
    也是语法错误关键在于
    where后面的第一个条件没有and,而其它的条件后面有and
    但是哪一个才是第一个条件,判断起来很麻烦
      

  5.   

    和尚是对的,而且在代码后面附了详细说明了,楼主没看清楚,很简单的字符串处理啊:
    public foo(String key1, String key2, String key3) {
    ....
    //写查询语句,以它们三个为条件
    //如果这三个条件都有值,很简单
    //sqlStr = "select * from table where key1=" + key1 + " and key2=" + key2 + " and key3=" + key3 + ";";  sqlStr = "select * from table ";
      String sqlWhere = "";
      if(key1 != null){
        sqlWhere += " and key1=" + key1;
      }
      if(key2 != null){
        sqlWhere += " and key2=" + key2;
      }
      if(key3 != null){
        sqlWhere += " and key3=" + key3;
      }
      if(key4 != null){
        sqlWhere += " and key4=" + key4;
      }
      ...
      
      if(sqlWhere == ""){ //如果没有字符则说明所有条件都是null值,直接加";"
        sqlWhere = ";";
      }
      else{ //否则就肯定至少有一个条件非null,则将结果字符串前面的" and "去掉
        sqlWhere = "where" + sqlWhere.replace(/^and /g,"") + ";";
      }
      //拼装最后的结果
      sqlStr +=sqlWhere;
      ...
    }
      

  6.   

    很容易嘛,先写语句头:sqlStr = "select * from table where true ",
    后面再根据判断每个参数是否为空来决定该参数是否附加到条件语句中,
    if(key1 != null){
        sqlStr += " and key1=" + key1;
      }
    if(key2 != null){
        sqlStr += " and key2=" + key2;
      }
      if(key3 != null){
        sqlStr += " and key3=" + key3;
      }
    .....
      

  7.   

    if(key1 != null){
        sqlStr += " and key1=" + key1;
      }
    if(key2 != null){
        sqlStr += " and key2=" + key2;
      }
      if(key3 != null){
        sqlStr += " and key3=" + key3;
      }
    .....这个不错
      

  8.   

    to dddeee(dddeee) :
    你有没有看我的最后一个判断,我是在所有的前面都加了 and 然后判断 sqlstr 是不是为空,如果不为空,去掉前面五个字符,就是把 and 去掉,最后同where 组成sql语句,如果sql是空的话,那就没有where语句.
      

  9.   

    这个问题在编程中经常遇到,解决方法有很多。前面 chaozi(编程浪子)的方法是我平时最常用的,在MYSQL和SQL SERVER中都不会产生语法错误,不知道楼主用的是什么数据库?trampwind(随风)的方法也有异曲同工之处。 double22822(大无忧-老实和尚(有事发消息)) 的方法可行,不过这么写程序,是不是太罗嗦?  :)