SQL Server 2008 datetime列JDBC插入问题 [英] SQL Server 2008 datetime column JDBC insert problem

查看:51
本文介绍了SQL Server 2008 datetime列JDBC插入问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,该表的列是SQL Server 2008数据库中的 DATETIME .从在插入时使用NOW可以接受的意义上说,插入的日期不是时间戳.日期是在插入之前生成的,我想保留此值.

I have a table with a column that is a DATETIME in a SQL Server 2008 database. The dates that are being inserted are not timestamps in the sense that using NOW on insert would be acceptable. The date is generated a bit before the insert and I would like to preserve this value.

Datetimes被序列化为字符串,格式为 yyyy-mm-dd hh:mm:ss.[fff ...] ,然后在插入之前将其转换为JDBC Timestamp对象.

Datetimes are being serialized as strings in the format yyyy-mm-dd hh:mm:ss.[fff...] and then converted to a JDBC Timestamp object before being inserted.

我在使用SQL Server时遇到的一个问题是,它将在插入之前将值转换为TIMESTAMP,然后抱怨如下:

A problem I'm running into with SQL Server is that it converts the value to TIMESTAMP before being inserted and then complains with this:

com.microsoft.sqlserver.jdbc.SQLServerException: 
Cannot insert an explicit value into a timestamp column. 
Use INSERT with a column list to exclude the timestamp column, 
or insert a DEFAULT into the timestamp column.

如何在不进行转换的情况下直接插入DATETIME列?

How can I just insert into a DATETIME column without the conversion?

谢谢.

推荐答案

SQL Server TIMESTAMP数据类型不是类似于日期的数据类型.它是服务器生成的值,用于帮助实现数据一致性.如果要存储日期时间数据,请避免使用TIMESTAMP数据类型.

A SQL Server TIMESTAMP data type is not a date-like data type. It is a server-generated value used to help with data consistency. If you are storing datetime data, then avoid the TIMESTAMP data type.

因此,请勿转换为JDBC Timestamp对象.数据可以作为字符串值插入.请注意,您指定的格式是可以的,但是 fff ... 必须为3位数字,对于SQL Server DATETIME数据类型,不能超过3位.

So, don't convert to a JDBC Timestamp object. Data can be inserted as a string value. Note that the format you indicate is okay, but the fff... must be 3 digits, not more than 3 digits for a SQL Server DATETIME data type.

这篇关于SQL Server 2008 datetime列JDBC插入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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