Java JDBC:连续两天休息 [英] Java JDBC: dates consistently two days off

查看:23
本文介绍了Java JDBC:连续两天休息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Java JDBC 将日期写入 SQL Server 2008,然后将其读回.
回读的日期始终比实际写入的日期早两天.

I am using Java JDBC to write a date to SQL server 2008 and then read it back.
The date that is read back is consistently two days earlier than the date that was actually written.

我正在使用准备好的语句插入包含日期字段的行.日期值由以下提供:

I am inserting the row containing the Date field with a prepared statement. The date value is provided by:

java.sql.Date todaysDate = new java.sql.Date(System.currentTimeMillis()) ;
System.out.println(todaysDate.toString()) // -> 2012-07-02
ps.setDate(8, todaysDate);

将日期写入数据库后,如果我运行,SQL 服务器会显示正确的日期:

After writing the date to the DB, SQL server shows me the correct date if I run:

select date from table_name where date!=null // ->2012-07-02

如果我通过 JDBC 运行相同的查询,则使用

If I run the same query via JDBC then retrieve the date value from the resultset using

java.sql.Date sqlDate = rs.getDate("date") ;
sqlDate.toString() // ->2012-06-30

插入的行是表中唯一具有非空日期的行,因此这似乎不是读取错误记录的情况.

The inserted row is the only row in the table with a non-null date so this does not appear to be a case of reading the wrong record.

我认为这将是一个众所周知的问题,但我通过 Google 搜索找到的关于两天假期"问题的唯一参考没有明确的答案.

I thought this would be a well known problem but the only reference I could find by a Google search for a "two days off" issue had no definitive answer.

有什么想法吗?

beeky(活在过去)

beeky (living in the past)

推荐答案

JDBC 驱动程序错误

事实证明问题出在 MS JDBC 驱动程序上.我尝试了所有可能的日期类型和日期转换组合,但没有任何效果.经过大量搜索(应该先完成!)我看到了一个评论 一个较旧的 SO 条目 暗示问题是来自 Microsoft 的第 3 版 JDBC 驱动程序.我得到了最新的驱动程序,版本 4.something,问题就消失了.

Faulty JDBC Driver

It turns out the problem was the MS JDBC driver. I tried every possible combination of date types and date conversions and nothing worked. After a great deal of searching (should have done that first!) I saw a comment on an older SO entry that implied the problem was the version 3 JDBC driver from Microsoft. I got the latest driver, version 4.something, and the problem went away.

感谢所有试图提供帮助的人.特别感谢 Mike 抽出时间发布解决方案.

Thanks to all that tried to help. Special thanks to you Mike for taking the time to post a solution.

-=beeky

这篇关于Java JDBC:连续两天休息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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