插入"daterange"通过JDBC将字段值放入PostgreSQL表中 [英] Insert "daterange" field value into PostgreSQL table through JDBC

查看:136
本文介绍了插入"daterange"通过JDBC将字段值放入PostgreSQL表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PostgreSQL(9.3)中有一个表,该表具有 daterange 字段类型.

I have a table in PostgreSQL(9.3) with daterange field type.

我可以像使用JDBC一样选择此字段,但是不能将其插入表中.

I can select this field like a String with JDBC, but I cannot Insert it in a table.

我尝试过的事情:

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驱动程序不支持日期范围... 也许还有第三种解决方案?

Does anyone have a solution for inserting daterange? What stm.setXXX should I use? Or maybe I cannot do that because JDBC Driver does not have daterange support... Maybe there is a third solution?

谢谢.

P.S.

我的PostgreSQL JDBC驱动程序:

My PostgreSQL JDBC Driver:

    <dependency>
      <groupId>postgresql</groupId>
      <artifactId>postgresql</artifactId>
      <version>8.4-701.jdbc4</version>
    </dependency>

推荐答案

使用:

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

这篇关于插入"daterange"通过JDBC将字段值放入PostgreSQL表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆