我有下拉列表的列名称。当用户选择列名并输入serach值时,我填充页面,但我试图从表格中提取所有内容,如果它们不输入搜索值,但选择了下拉列名称之一。我试图这样做,如果搜索值为空不寻找where子句只是直接去其他人,但这不是为我工作..可以有人告诉我,我在做什么错在这里?谢谢Java搜索选项
sql.append(" SELECT id, name, dept, email");
if ((this.getSearch() != null)&& (this.getSelected().equals("1"))){
sql.append(" from table ");
sql.append(" where id = '");
sql.append(this.getSearch());
sql.append("'");
}else if ((this.getSearch() != null)&& (this.getSelected().equals("2"))){
sql.append(" from table ");
sql.append(" where name = '");
sql.append(this.getSearch());
sql.append("'");
}else{
sql.append(" from table ");
}
请不要使用StringBuilder建立您的查询。这是一个SQL注入漏洞。 –
@Mattb:怎么这样? – claymore1977
@ claymore1977 https://www.owasp.org/index.php/SQL_Injection使用准备好的语句来降低一些风险。 –