gpt4 book ai didi

java - mssql 2005 日期时间和 jdbc

转载 作者:行者123 更新时间:2023-11-30 05:50:11 25 4
gpt4 key购买 nike

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

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

最佳答案

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

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

From the Javadocs :

To conform with the definition of SQL DATE, the millisecond values wrapped by a java.sql.Date instance must be 'normalized' by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated.

当然,您还需要使用 setTimestamp() 而不是 setDate()

关于java - mssql 2005 日期时间和 jdbc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14460907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com