Java JDBC:日期一致地持续两天 [英] Java JDBC: dates consistently two days off

查看:97
本文介绍了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 Server将向我显示正确的日期:

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.

有什么想法吗?

顽强(过去生活)

推荐答案

错误的JDBC驱动程序

事实证明,问题出在MS JDBC驱动程序.我尝试了日期类型和日期转换的所有可能组合,但没有任何效果.经过大量搜索(首先应该做的是!),我在

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.

-=强壮

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

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