/**
* User表中的id字段
*/
String ID = "id";
String AGE = "age";
/**
* 用户输入的值,根据是否为空字符串实现组合查询
*/
String id =
String strAge1 =
String strAge2 =
/**
* 条件判断
*/
int age1;
int age2;
if("".equals(id)){
ID = id = "1";
}
if("".equals(strAge1)){
age1= MIN; // MIN常量这里定义为0
}else{
age1 = Integer.parseInt(strAge1);
}
if("".equals(strAge2)){
age2 = MAX // MAX常量这里定义为150
}else{
age2 = Integer.parseInt(strAge2);
}
/**
* sql语句
*/
String sqlStr =
"select * from User where"+
ID + "= '"+id+"' and"+
AGE +">= "+ age1+" and"+
AGE +"<= "+ age2;
想当初为了实现组合查询,写了n条sql语句...