通过JDBC将“daterange”字段值插入到PostgreSQL表中

6

我在PostgreSQL(9.3)中有一个包含daterange字段类型的表。

我可以使用JDBC像字符串一样选择此字段,但我无法将其插入到表中。

我尝试过以下方法:

PreparedStatement stm = conn.prepareStatement("insert into mytable (my_daterange_field) values (?)"); 
stm.setString(1, "[2014-01-02,2014-01-04]");
int i = stm.executeUpdate();

我得到了:

Exception in thread "main" org.postgresql.util.PSQLException: ERROR: column "my_daterange_field" is of type daterange but expression is of type character varying
  Hint: You will need to rewrite or cast the expression.
  Position: 168

有没有人有插入日期范围的解决方案?我应该使用什么stm.setXXX?或者可能是因为JDBC驱动程序不支持日期范围而无法这样做...也许有第三种解决方案吗?

谢谢。

P.S.

我的PostgreSQL JDBC驱动程序:

    <dependency>
      <groupId>postgresql</groupId>
      <artifactId>postgresql</artifactId>
      <version>8.4-701.jdbc4</version>
    </dependency>
1个回答

11

使用:

insert into mytable (my_daterange_field) values (?::daterange)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接