我有3个变量,这些变量由用户插入。
我需要查询一个包含3列的表,如果变量上的一个为null则不执行查询。
如果没有if-else,我该怎么做?
String par1= request.getParameter("par1");
String par2= request.getParameter("par2");
String par3= request.getParameter("par3 ");
String query = "select * from table t " +
"where t.var1 = ? " +
"and t.var2" +
"and t.var3= ? ";
Connection conn = DataBase.getConnection();
PreparedStatement s = conn.prepareStatement(query);
s.setString(1, par1);
s.setString(2, par2);
s.setString(3, par3);
编辑 - 让我更清楚
我想查询搜索,用户可以在其中选择要填充的字段(其中一些可以保持为空)。
这样做的原因是什么?
答案 0 :(得分:1)
用户三元运算符http://en.wikipedia.org/wiki/%3F:
示例代码:
String x = null;
String y = x!=null ? "Its not null" : "It is null"
您的代码
String par1= request.getParameter("par1")==null? "" :
request.getParameter("par1");
答案 1 :(得分:1)
我看不到这里的条件限制。您需要使用StringBuilder
和if-else
块来动态构建SELECT
查询。
String par1 = "par1"; // intialized for testing
String par2 = null;
String par3 = "par3";
StringBuilder query = new StringBuilder("select * from table t");
if (par1 != null || par2 != null || par3 != null) {
query.append(" where ");
if (par1 != null) {
query.append("t.var1 = ?");
}
if (par2 != null) {
if (par1 != null) {
query.append(" AND ");
}
query.append("t.var2 = ?");
}
if (par3 != null) {
if (par1 != null || par2 != null) {
query.append(" AND ");
}
query.append("t.var3 = ?");
}
}
System.out.println(query);
输出:
select * from table t where t.var1 = ? AND t.var3 = ?
请注意,您还必须有条件地绑定占位符值。
Connection conn = DataBase.getConnection();
PreparedStatement s = conn.prepareStatement(query.toString());
if (par1 != null || par2 != null || par3 != null) {
int n = 1;
if (par1 != null) {
s.setString(n++, par1);
}
if (par2 != null) {
s.setString(n++, par2);
}
if (par3 != null) {
s.setString(n, par3);
}
}
答案 2 :(得分:0)
如果使用丑陋计为“不使用if-else”:
String[] fields = {"par1","par2","par3"};
String query = "SELECT * FROM TABLE t ";
String where = "";
String[] whereString = new String[]{" WHERE ", " AND "};
int whereAddress = 0;
List<String> valuesAsParameters = new ArrayList<String>();
for(String fieldName : fields) {
String value = request.getParameter(fieldName);
//do we need WHERE or AND?
where += whereString[whereAddress];
whereAddress=1; //change state
where += fieldName;
//should not EVER use exception for control flow... Outside of assignments, that is
try {
//throws NPE **on purpose**... Yuck!!!
valuesAsParameters.add(value.toString());
// to check emptiness of string, we could use value.charAt(0) for the same purpose
where += " = ? ";
} catch(NullPointerException e) {
// EEEEEEKKKK! No, please really, don't do stuff like this...
// I shall burn in hell for even
// thinking about
// **thinking about**
// ****thinking about****
// something this unexplainably, and universally bad...
where += " IS NULL ";
}
}
Connection conn = DataBase.getConnection();
PreparedStatement s = conn.prepareStatement(query);
int i=1;
for(String value : valuesAsParameters) {
s.setString(i++, value);
}
对不起,我现在要去呕吐......这段代码纯粹是邪恶的。 这里是龙不要在家里这样做。而 home 我指的是Universe的已知和未知部分。包括未知维度,不存在的地方,甚至梦想...... 我认为 我已经sta rted t o 看到的摇摆不定......
修改强>
好的,现在好多了。但是,上面的代码并没有像OP那样做,尽管很容易将其定制。这样做是使用IS NULL子句来表示空值,就像在SQL中一样,这是将事物与null
进行比较的方式。
答案 3 :(得分:0)
当参数为null时绑定通配符怎么样?这样可以有效地将它们从查询中排除,而无需使用StringBuilder
构建查询,然后跟踪您的绑定方式。
String query = "select * from table t " +
"where t.var1 like ? " +
"and t.var2 like ?" +
"and t.var3 like ? ";
Connection conn = DataBase.getConnection();
PreparedStatement s = conn.prepareStatement(query);
s.setString(1, par1 != null ? par1 : "%");
s.setString(2, par2 != null ? par2 : "%");
s.setString(3, par3 != null ? par3 : "%");