【问题标题】:How to check NULL value of Date/Time while using UCanAccess JDBC Driver?使用 UCanAccess JDBC 驱动程序时如何检查日期/时间的 NULL 值?
【发布时间】:2015-12-13 16:44:23
【问题描述】:

在使用 UCanAccess JDBC 驱动程序访问 MS Access DB 时,我在处理日期时遇到了太多问题。我想检查日期/时间字段是否为空。如果有 Null 则想更新,但我不能这样做。 MSAccess 数据库中的“logout_time”字段是“日期/时间”数据类型。

String sqlString = "update user_Log set logout_time = #" + 
                                    strEndDate + "# where (user_Id ="+ 
                                    userId +") & (logout_time = NULL)";


                    int l=stmt.executeUpdate(sqlString);

这将发送 sqlString

update user_Log set logout_time = #12/13/2015 10:33:28# where (user_Id =3) & (logout_time = NULL);

此 SQL 语句在 MSAccess Query 中执行良好。但它不能从 UCanAccess JDBC 驱动程序中执行。它会产生错误:

    net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.3 incompatible data types in combination 
 This exception may  happen if you add integers representing units of time directly to datetime values using the arithmetic plus operator but without specifying the unit of date. 
   In this specific case you have to use, for example, <dateColumn> + 1 DAY.
    at net.ucanaccess.jdbc.UcanaccessStatement.executeUpdate(UcanaccessStatement.java:222)

【问题讨论】:

    标签: ms-access jdbc ucanaccess


    【解决方案1】:

    此 SQL 语句在 MSAccess Query 中执行良好。

    不是真的。它可能会执行,但也可能不会执行您想要的操作。你的代码...

    WHERE ... (logout_time = NULL)
    

    ... 是非标准 SQL,可能会产生意想不到的结果(详情 here)。你想要的

    WHERE ... (logout_time IS NULL)
    

    您还应该停止使用动态 SQL,而是使用 PreparedStatement参数化查询 来执行更新。

    【讨论】:

    • 问题解决了!将 WHERE (logout_time = NULL) & (user_Id = 4) 更改为 WHERE (logout_time IS NULL) AND (user_Id = 4) 后,它可以工作了。谢谢大家的快速回复。
    猜你喜欢
    • 2015-01-12
    • 2014-01-14
    • 1970-01-01
    • 1970-01-01
    • 2021-08-28
    • 1970-01-01
    • 2015-06-23
    • 2023-03-17
    • 2016-06-19
    相关资源
    最近更新 更多