0

我有一个文本框,我需要在其中输入变体。输入用于数据库表的扩展列,数字被视为字符串。输入条件如下:

  1. 只有一个输入,如 5028
  2. 输入由连字符 (-) 分隔,如 5028-5090。
  3. 输入以逗号 (,) 分隔,例如 5028,5029。
  4. 包含单个输入,连字符和逗号的输入,例如 5029,1234-4567,9876。

我已经编写了仅针对一个输入的查询形成条件,输入由连字符分隔,输入由逗号分隔..现在根据我的需要,我必须编写条件,我必须为输入格式化查询,例如 5029,1234-4567, 9876 但我没有得到确切的逻辑..

这是我的代码..

if (extension != "") {
    if (extension.contains(",")) {
        query = query.concat(" and (extension='");
        String extn[] = extension.split(",");
        for (int i = 0; i < extn.length; i++) {
            System.out.println(extn[i]);
            query = query.concat(extn[i]).concat("'").concat(" or extension='");
            System.out.println(query);
        }
        query = query.substring(0, query.length() - 15);
        System.out.println(query);
        query = query.concat(")");
        System.out.println(query);
    } else if (extension.contains("-")) {
        query = query.concat(" and cast(extension as signed) >=");
        String extn[] = extension.split("-");
        for (int i = 0; i < extn.length; i++) {
            System.out.println(extn[i]);
            query = query.concat(extn[i]).concat(" And cast(extension as signed) <=");

        }
        query = query.substring(0, query.length() - 33);
        System.out.println(query);

    } else {
        query = query.concat(" and extension='" + extension).concat("'");
        System.out.println(query);
    }
}

请大家帮帮我..提前谢谢。

4

1 回答 1

1

进行以下更改:1.替换if (extension != "")if (!extension.equals(""))

  1. 在第一个 if 条件块if (extension.contains(","))String extn[] = extension.split(",");再次拆分每个 String extn[] on '-' 并执行其他过程

试试这个

if (!extension.equals("")) {
if (extension.contains(",")) {
    query = query.concat(" and (extension='");
    String extn[] = extension.split(",");
    for(int k=0; k <extn.length; k++){
    if(extn[k].contains("-")){
        String subExtn[] = extn[k].split("-");
        for (int i = 0; i < subExtn.length; i++) {
            //System.out.println(subExtn[i]);
            query = query.concat(subExtn[i]).concat("'").concat(" or extension='");

        }

    }else{
        //for (int i = 0; i < extn.length; i++) {
           // System.out.println(extn[i]);
            query = query.concat(extn[k]).concat("'").concat(" or extension='");
          //  System.out.println(query);
        //}
    }
    //System.out.println(query);
    }
    query = query.substring(0, query.length() - 15);
   // System.out.println(query);
    query = query.concat(")");
    System.out.println(query);
} else if (extension.contains("-")) {
    query = query.concat(" and cast(extension as signed) >=");
    String extn[] = extension.split("-");
    for (int i = 0; i < extn.length; i++) {

        query = query.concat(extn[i]).concat(" And cast(extension as signed) <=");

    }
    query = query.substring(0, query.length() - 33);
    System.out.println(query);

} else {
    query = query.concat(" and extension='" + extension).concat("'");
    System.out.println(query);
}
}
于 2013-10-27T07:24:27.940 回答