在JDBC中,当调用PreparedStatement.setArray()时,如何为时间戳数组指定时区?

发布于 2024-12-12 02:59:00 字数 441 浏览 0 评论 0原文

UTC 时间戳,我会这样做:

PreparedStatement pst = connection.prepareStatement(sql);
pst.setTimestamp(1, timestampValue, Calendar.getInstance(TimeZone.getTimeZone("UTC"));

但现在我想做同样的事情,但对于时间戳数组:

pst.setArray(1, timestampValues);

目前,使用 JDBC,如果我想将数据库列设置为时间戳值并使数据库将其解释为 timestampValues 是一个时间戳数组(数据库列的类型为“timestamp[]”,例如在 Postgresql 中)。我不知道在哪里可以向 JDBC 驱动程序指定数组中的每个时间戳值都必须被视为 世界标准时间?

Currently, using JDBC, if I want to set a database column to a timestamp value and have the database interpret it as a UTC timestamp, I would do this:

PreparedStatement pst = connection.prepareStatement(sql);
pst.setTimestamp(1, timestampValue, Calendar.getInstance(TimeZone.getTimeZone("UTC"));

but now I want to do the same thing but for an array of Timestamps:

pst.setArray(1, timestampValues);

where timestampValues is an array of Timestamps (and the database column is of type "timestamp[]" e.g in Postgresql. I don't see where I can specify to the JDBC driver that each Timestamp value in the array has to be treated as UTC?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恋竹姑娘 2024-12-19 02:59:00

我相信您必须立即将它们全部设置为 UTC。另外,下面的代码会覆盖 JVM 使用的时区,使其对所有内容都使用 UTC。然后,如果您需要 UTC 以外的其他时间,您可以使用 SimpleDateFormat 来更改它(例如用于日志记录)。

我参考了以下帖子获取时区信息。

TimeZone utcTimeZone = TimeZone.getTimeZone("UTC");
TimeZone.setDefault(utcTimeZone);
Calendar calendar = Calender.getInstance(utcTimeZone);

....//set the calendar values here.
//i'll just give you an example with a loop, you'll have to customize.
java.sql.Timestamp [] array = new java.sql.Timestamp [5];
for(int i=0; i < array.lenght; i++) {
  array[i] = new java.sql.Timestamp(calendar.getTimeInMillis());
}

PreparedStatement pst = connection.prepareStatement(sql);
pst.setArray(1, array);

I believe you will have to set them all to UTC right away. Plus the code below overrides the TimeZone used by your JVM to make it use UTC for everything. Then if you need other than UTC you can use SimpleDateFormat to change it (say for logging).

I referred to the following post for the timezone information.

TimeZone utcTimeZone = TimeZone.getTimeZone("UTC");
TimeZone.setDefault(utcTimeZone);
Calendar calendar = Calender.getInstance(utcTimeZone);

....//set the calendar values here.
//i'll just give you an example with a loop, you'll have to customize.
java.sql.Timestamp [] array = new java.sql.Timestamp [5];
for(int i=0; i < array.lenght; i++) {
  array[i] = new java.sql.Timestamp(calendar.getTimeInMillis());
}

PreparedStatement pst = connection.prepareStatement(sql);
pst.setArray(1, array);
孤寂小茶 2024-12-19 02:59:00

用户 connection.createArrayOf(..) 如此处所述 http://docs。 oracle.com/javase/tutorial/jdbc/basics/array.html

User connection.createArrayOf(..) as described here http://docs.oracle.com/javase/tutorial/jdbc/basics/array.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文