String sqlstr = "select * from Table where ";
int a = Integer.parseInt(request.getParameter("a"));
int b = Integer.parseInt(request.getParameter("b"));
int c = Integer.parseInt(request.getParameter("c"));
int d = Integer.parseInt(request.getParameter("d"));if (a!= -1)
sqlstr = sqlstr + " a=" + a;
if (b != -1)
sqlstr = sqlstr + " and b=" + b;
if (c != -1)
sqlstr = sqlstr + " and c=" + c;
if (d!= -1)
sqlstr = sqlstr + " and d=" + d;如果第一个字段a为空的话,会变成select * from Table where and b=... 这样会报错,以前一直用where1=1来处理的,现在因为这张表数据量比较大,会影响性能。so想问一下大家还有没有其他方法?
String temp = "";
if (a!= -1)
temp = temp + " a=" + a;
if (b != -1) {
if (temp.length > 0) {temp = temp + " and";}
temp = temp + " b=" + b;
}
if (c != -1) {
if (temp.length > 0) {temp = temp + " and";}
temp = temp + " c=" + c;
}
if (d!= -1) {
if (temp.length > 0) {temp = temp + " and";}
temp = temp + " d=" + d;
}
sqlstr = sqlstr + temp