【问题标题】:How to insert TIMESTAMP WITH TIME ZONE into HSQLDB using JOOQ如何使用 JOOQ 将 TIMESTAMP WITH TIME ZONE 插入 HSQLDB
【发布时间】:2021-03-23 21:46:52
【问题描述】:

我正在使用HSQLDB 2.4.0jOOQ 3.7.1Scala 2.11.7Java 8

我正在尝试将java.time.OffsetDateTime 插入HSQLDB 表中的TIMESTAMP WITH TIME ZONE 列:

sql.insertInto(MY_TABLE)
    .columns(MY_TABLE.INSERTED_ON)
    .values(java.time.OffsetDateTime.now())
    .execute()

我收到以下异常:

org.hsqldb.HsqlException: data exception: invalid datetime format

我做错了什么?

【问题讨论】:

  • 有趣!我在这里没有发现任何问题。从HSQLDB 2.4.0 开始支持TIMESTAMP WITH TIME ZONE,对应的Java 类型确实是java.time.OffsetDateTime。但是,我不知道jOOQ。我也想知道这个问题怎么会是近距离投票和否决票。
  • 我已经验证如果我将列类型更改为varchar...
  • 应该是找到原因了。检查github.com/jOOQ/jOOQ/issues/9174,上面写着:For backwards compatibility reasons, we're still binding JSR 310 types as strings in most dialects, as JDBC drivers haven't adopted them immediately. There's a pending feature request to pass the JSR 310 type instead of the string:#9902
  • 我不确定他们建议做什么。我必须升级到 jOOQ 3.14 吗?
  • 恕我直言,升级到最新版本的jOOQ 是最好的选择。

标签: java scala datetime hsqldb jooq


【解决方案1】:

正如https://github.com/jOOQ/jOOQ/issues/9174所提到的那样

出于向后兼容性的原因,我们仍在绑定 JSR 310 类型 在大多数方言中作为字符串,因为 JDBC 驱动程序没有采用它们 立即地。有一个通过 JSR 310 的待处理功能请求 键入而不是字符串:#9902

恕我直言,升级到最新版本的 jOOQ 是最好的选择。

那里也提到了

jOOQ 仍然将值转换为字符串表达式,如“timestamp 时区 '2019-09-05 15:02:14.5015411+02:00'" 而不是仅仅 将其传递给 JDBC 驱动程序。

您可以尝试将OffsetDateTime格式化成符合指定格式的字符串,看看是否有效。

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.nXXX");
String formatted = OffsetDateTime.now().format(formatter);
sql.insertInto(MY_TABLE)
    .columns(MY_TABLE.INSERTED_ON)
    .values(formatted)
    .execute()

【讨论】:

    【解决方案2】:

    HSQLDB 只有在按照 SQL 标准格式化时才能将字符串转换为 TIMESTAMP WITH TIME ZONE。接受诸如“2020-12-12 21:34:10.769000+2:00”之类的字符串。 OffsetDateTime 的 toString 方法的输出略有不同,失败。

    您应该能够插入格式正确的字符串。

    【讨论】:

      猜你喜欢
      • 2023-01-10
      • 2012-04-04
      • 2020-09-20
      • 2011-03-14
      • 1970-01-01
      • 1970-01-01
      • 2013-02-20
      • 2011-05-03
      • 1970-01-01
      相关资源
      最近更新 更多