PreparedStatement:Java8 OffsetDateTime到Postgresql带时区的时间戳

时间:2017-04-12 07:31:25

标签: java postgresql maven jdbc java-8

是Java 8'的直接映射。 OffsetDateTime类型为PostgreSQL的Java PreparedStatement中带时区的时间戳可能吗?

https://jdbc.postgresql.org/documentation/head/8-date-time.html说必须使用setObject完成,但它对我没用。

My Prepared语句是PostgreSQL函数调用。函数的参数是带时区的时间戳。 我尝试使用prepareCallprepareStatement。两个都不工作。

我的pom.xml有最新的postgresql驱动程序:

<dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.0.0</version>
</dependency>

我也试过这个:

<dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.4.1208-jdbc42-atlassian-hosted</version>
</dependency>

OffsetDateTime offsetDateTime = FileUtil.getCreationDateTime(absolutePath)
PreparedStatement stmt = connection.prepareStatement("select import_file(?) ;");
stmt.setObject(1, offsetDateTime);
stmt.executeUpdate(); --> error "column "x" is of type timestamp with time zone but expression is of type text". Hint:  You will need to rewrite or cast the expression.

函数的参数列表需要一列&#34; x&#34;带有时区的时间戳类型。

 CREATE OR REPLACE FUNCTION public.import_file(    
    x timestamp with time zone)
  RETURNS bigint AS
$BODY$BEGIN
    INSERT INTO datei VALUES (nextval('datei_pk_datei_id_seq'::regclass), x);       
    RETURN currval('datei_pk_datei_id_seq'::regclass);
END;$BODY$

0 个答案:

没有答案