简体   繁体   中英

Binding Nulls in Java HashMap

I am making a parameter input form to query an audit table. The inputs are not required.

The SQL query below, which is what I want the Java to resolve to, works perfectly. It returns the rows I want, based on the input.

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

However, when I try to make it work in Java, it returns no rows at all. I am thinking it has to do with the values in the map. Looking at them in the debugger, it show "" instead of 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());

Am I barking up the wrong tree here?

在查询中将`替换为'并尝试。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM