Java搜索选项
问题描述:
我有下拉列表的列名称。当用户选择列名并输入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 ");
}
答
也许没有返回null
但别的东西像一个空字符串或包含空格字符的字符串。你已经清理过那个字符串了吗?
答
每当从一些代码返回,我没有写一个字符串处理,我通常检查NULL,然后再为一个零长度字符串:
String str = myClass.getMyString();
if (str == null || str.length() < 1) {
/* Failure case */
} else {
/* Success case */
}
上面的代码做这样的假设回报值方法说明零长度字符串是一种失败模式,所以记住这一点。
答
有一个称为调试器的工具。
请不要使用StringBuilder建立您的查询。这是一个SQL注入漏洞。 –
@Mattb:怎么这样? – claymore1977
@ claymore1977 https://www.owasp.org/index.php/SQL_Injection使用准备好的语句来降低一些风险。 –