加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

postgresql – 如何在预准备语句中传递带有”(timestamp)的字符

发布时间:2020-12-13 16:05:36 所属栏目:百科 来源:网络整理
导读:我正在尝试执行以下查询 INSERT INTO hotspot(timestamp) VALUES (timestamp with time zone '2012-10-25 14:00:00 +05:00' at time zone 'EET'); 我想将时间戳作为变量传递. 我的时间戳列是带时区的时间戳类型. 你知道如何做到这一点吗? 当我做…(Java,Pos
我正在尝试执行以下查询

INSERT INTO hotspot(timestamp) VALUES 
(timestamp with time zone '2012-10-25 14:00:00 +05:00' at time zone 'EET');

我想将时间戳作为变量传递.

我的时间戳列是带时区的时间戳类型.

你知道如何做到这一点吗?

当我做…(Java,Postgresql)

String stm= "INSERT INTO hotspot(timestamp) VALUES(timestamp with time zone ? at time zone 'EET')";
    pst = con.prepareStatement(stm);
    pst.setString(1,"2012-08-24 14:00:00 +05:00");
    pst.executeUpdate();

我在“$1”或附近收到语法错误

无论如何我能克服这个错误吗?
先感谢您!!

更新:我尝试通过以下方式使用setTimestamp …

Calendar c=Calendar.getInstance(TimeZone.getTimeZone("GMT+05:00"));
String stm= "INSERT INTO hotspot(timestamp) VALUES(?)";
pst = con.prepareStatement(stm);
pst.setTimestamp(1,Timestamp.valueOf("2012-01-05 14:00:00"),c );
pst.executeUpdate();

我想数据库中的正确值应该是(关于我的本地时区是EET(02))

2012-01-05 11:00:00 02

但使用pgadmin我检查值,我得到

2012-01-05 14:00:00 02

有什么建议?

解决方法

考虑使用setTimestamp()方法而不是setString()方法.检查 this link以了解如何使用PreparedStatement.

编辑:正如我在评论中解释的那样,使用三个参数检查API reference for setTimestamp():

Sets the designated parameter to the given java.sql.Timestamp value,using the given Calendar object. The driver uses the Calendar object to construct an SQL TIMESTAMP value,which the driver then sends to the database. With a Calendar object,the driver can calculate the timestamp taking into account a custom timezone. If no Calendar object is specified,the driver uses the default timezone,which is that of the virtual machine running the application.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读