我有一个文本框,我需要在变量中输入。输入是数据库表的扩展列,数字被视为字符串。这是输入条件:
我已经为只有一个输入形成查询的条件,输入用连字符分隔,输入用逗号分别单独进行..现在根据我的需要我必须写条件,我必须格式化查询输入,如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);
}
}
请大家帮帮我.. 提前谢谢。
答案 0 :(得分:1)
进行以下更改:
1.用if (extension != "")
if (!extension.equals(""))
if (extension.contains(","))
之后的条件块String extn[] = extension.split(",");
再次拆分' - '上的每个字符串extn []并执行其他处理试试这个
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);
}
}