2013-07-17 49 views
0

我正在製作一個參數輸入窗體來查詢審計表。輸入不是必需的。在Java HashMap中綁定空值

下面的SQL查詢,這是我想要的Java解決,完美的作品。它根據輸入返回我想要的行。

SELECT * 
FROM _audit 
WHERE DATEDIFF(NOW(), `_audit_timestamp`) < 30 
     AND (('smith' <> '' AND _action__user = 'smith') 
      OR (_action__user = _action__user AND 'smith' IS NULL)) 
     AND ((NULL IS NOT NULL AND _action_searchkey IS NULL) 
      OR (_action_searchkey = _action_searchkey OR _action_searchkey IS NULL)) 
     AND ((NULL IS NOT NULL AND _action_customer IS NULL) 
      OR (_action_customer = _action_customer AND _action_customer IS NULL)) 
ORDER BY _audit_timestamp DESC 

但是,當我嘗試使它在Java中工作時,它根本沒有返回任何行。我認爲這與地圖中的值有關。在調試器中看到它們,它顯示「」而不是NULL

String sqlText = "SELECT * " 
      + "  FROM _audit " 
      + "  WHERE DATEDIFF(NOW(), `_audit_timestamp`) < :days " 
      + "  AND ((:Name IS NOT NULL AND _action__user = :Name) " 
      + "   OR (_action__user = _action__user AND :Name IS NULL)) " 
      + "  AND ((:SearchKey IS NOT NULL AND _action_searchkey = :SearchKey) " 
      + "    OR (_action_searchkey = _action_searchkey AND :SearchKey IS NULL)) " 
      + "  AND ((:Customer IS NOT NULL AND _action_customer = :Customer) " 
      + "   OR (_action_customer = _action_customer AND :Customer IS NULL)) " 
      + "  ORDER BY _audit_timestamp DESC"; 

    Map<String, String> parameters = new HashMap<String, String>(); 
    parameters.put("days", aqForm.getDays().toString()); 
    parameters.put("Name", aqForm.getName()); 
    parameters.put("SearchKey", aqForm.getSearchKey()); 
    parameters.put("Customer", aqForm.getCustomer()); 

我在這裏吠叫錯誤的樹嗎?

+0

建議:不要在參數名稱中的大寫和小寫字符之間交替。一致性是關鍵。 –

回答

0

在您的查詢中替換'with'並嘗試。