1

我有 3 个变量,它们的值由用户插入。

我需要查询一个包含 3 列的表,如果变量中的一个为空,则不执行查询。

如果没有 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);

编辑 - 让我更清楚

不

我想查询一个搜索,用户可以在其中选择要填写的字段(其中一些可以保持空白)。

这样做的正确方法是什么?

4

4 回答 4

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");
于 2013-10-03T13:53:00.100 回答
1

我在这里没有看到绕过条件的方法。您需要使用 aStringBuilderif-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);
    }
}
于 2013-10-03T13:55:39.583 回答
0

如果使用ugliness算作“没有 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);
}

打扰一下,我现在得去吐了……这代码纯属邪恶。这里是龙不要在家里这样做。我所说的是指宇宙中已知未知的部分。包括未知的维度,不存在的地方,甚至梦想……我 已经开始看到摇摆不定的东西……

编辑

好的,现在好多了。然而,上面的代码并没有做 OP 想要的,虽然很容易定制它。这样做是对空值使用 IS NULL 子句,就像在 SQL 中一样,这是将某些东西与null.

于 2013-10-03T13:55:36.193 回答
0

当参数为空时如何绑定通配符?这将有效地将它们从查询中排除,而无需使用 a 构建查询,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 : "%");
于 2013-10-03T15:20:14.990 回答