遇到的问题:
以前在使用sqlserver的时候一般日期类型我会选择使用datetime类型,最近有个项目中使用了date类型,我在数据库插入的日期是1994-07-08结果检索出来的结果是1994-07-06,硬生生是少了两天。
先给出解决方案:
方案一:将数据库中date类型的字段改为datetime,就会正常显示时间;
方案二:自己下载JDBC Driver 4.0安装,该方案解释如下:
考虑下,你在java中使用jdbc方式访问数据库,是不是根本就没用引用过jdcb.jar包,也可以访问,一直以来我也没有深究这个问题,知道今天遇到这个问题,我就深究了一下。我的想法是在没有jdbc.jar包的情况下也可以访问数据库那么系统库中肯定有集成过jdbc,果然在JRS System library下的rt.jar中我就找到了集成的jdbc,如下图:
java.sql包下:
这就是为什么不自己导入jdbc.jar包也可以访问数据库的原因;
而微软官网是这样解释的:
Dear SQL Server developers and users:
Thank you for your ongoing support of the Microsoft JDBC Driver for SQL Server. Some of you reported problems with dates when using JRE 1.7.
The Microsoft JDBC Driver for SQL Server currently supports JRE 1.5 & JRE 1.6, however we have made modifications to return correct date values regardless of the JRE version.This modification is available in the production version of the Microsoft JDBC Driver 4.0 for SQL Server. The Microsoft JDBC Driver 4.0 for SQL Server is supported on JRE 1.5 and JRE 1.6, and support for JRE 1.7 is on our roadmap.
In addition, the driver modification is also available for SQL Server 2000 customers as a hotfix in the 3.0 version of our driver. Please review the hotfix article to determine if the fix is applicable to you and if so, click the link to download the hotfix (applicable for all platforms).
As always we are interested in your feedback. You may report bugs and issues via our forums. If you encounter Java bugs you may report those to Java.
Thank you,
Shamitha Reddy
Program Manager Microsoft JDBC Driver for SQL Server
核心解释我放大加红了,所以原因一目了然,说白的就是jdbc驱动(jre library中集成的也是)目前只支持JRE1.5和JRE1.6,目前微软已经提出的解决解决方案,就是下载JDBC Driver4.0 安装既可以解决这个问题。O(∩_∩)O哈哈~