mssql 2005 datetime和jdbc

时间:2013-01-22 14:31:25

标签: java sql-server-2005 datetime jdbc

如何使用JDBC在MS SQL 2005中插入日期时间?我使用存储过程插入DB:

ALTER proc [dbo].[sp_insertid_report]
@stream_time_gmt as datetime,
@stream_time_local as datetime,
@start_time_gmt as datetime,
@end_time_gmt as datetime,
@start_time_local as datetime,
@end_time_local as datetime,
@note_id as int,
@reported_by as varchar(100),
@date_reported as datetime,
@date_created as datetime,
@date_updated as datetime,
@stream_id as int,
@Fp_file_path as varchar(300),
@Fp_name as varchar(200), 
@Is_Allowed as varchar(2)

as

begin  
insert into id_reports(stream_time_gmt,stream_time_local,start_time_gmt,end_time_gmt,start_time_local,end_time_local,
note_id,reported_by,date_reported,date_created,date_updated,stream_id,Fp_file_path,Fp_name,Is_Allowed)
values(@stream_time_gmt,@stream_time_local,@start_time_gmt,@end_time_gmt,@start_time_local,
@end_time_local,@note_id,@reported_by,@date_reported,@date_created,@date_updated,@stream_id,@Fp_file_path,@Fp_name,@Is_Allowed)
end 

我的JDBC代码是:

    callableStatement = connection.prepareCall("{ call dbo.sp_insertid_report(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) }");
        callableStatement.setDate(1, new Date(cvtToGmt(new java.util.Date(modifiedTime.toMillis())).getTime()));
        callableStatement.setDate(2, new Date(modifiedTime.toMillis()));
        callableStatement.setDate(3, new Date(cvtToGmt(startTime).getTime()));
        callableStatement.setDate(4, new Date(cvtToGmt(endTime).getTime()));
        callableStatement.setDate(5, new Date(endTime.getTime()));
        callableStatement.setDate(6, new Date(endTime.getTime()));
        callableStatement.setInt(7, songID);
        callableStatement.setString(8, AudioMatcherService.hostName);
        java.util.Date date = new java.util.Date();
        callableStatement.setDate(9, new Date(date.getTime()));
        callableStatement.setDate(10, new Date(date.getTime()));
        callableStatement.setDate(11, new Date(date.getTime()));
        callableStatement.setInt(12, channel.getAssignmentID());
        callableStatement.setString(13, "no");
        callableStatement.setString(14, "no");
        callableStatement.setString(15, "Y");
        callableStatement.execute();

但这不是在DB中插入小时,分钟,秒,毫秒?如何从JDBC插入此信息?提前致谢。我发现很难理解日期和时间的事情......到处都有很多变化..

修改

表结构是:

enter image description here

修改

方法cvtToGmt()来自How to convert a local date to GMT

2 个答案:

答案 0 :(得分:4)

您需要使用java.sql.Timestamp而不是java.sql.Date

java.util.Date仅适用于“真实”日期列,并会删除时间部分。如果您需要存储日期时间,则必须使用java.sql.Timestamp

From the Javadocs

  

为了符合SQL DATE的定义,java.sql.Date实例包含的毫秒值必须通过在特定时区中将小时,分钟,秒和毫秒设置为零来“标准化”与实例关联的。

当然,您还需要使用setTimestamp()代替setDate()

答案 1 :(得分:2)

<强> EDIT2

使用callableStatement.setTimestamp() - 这将被视为时间戳。 javax.sql.Timestamp扩展java.util.Date,因此您无需转换。

<强>上

也许cvtToGmt()函数会削减分数。

您的表格中可能包含错误的列类型,但不会仅存储Date

Date列类型:YYYY-MM-DD http://msdn.microsoft.com/en-ie/library/bb630352.aspx

Datetime列类型:YYYY-MM-DD hh.mm.ss.nnn http://msdn.microsoft.com/en-ie/library/ms187819.aspx