如何修复将 DATETIME 插入数据库时​​的错误

发布于 2024-10-16 11:26:51 字数 907 浏览 2 评论 0原文

我正在尝试执行此操作:

pr.setStartdate("2006-09-10T00:00:00");

我收到此错误:

java.sql.SQLDataException: The syntax of the string representation of a datetime value is incorrect.

任何有关如何成功插入的想法都会很棒。

这里还有一些代码。 现在我需要设置日期吗?或者 setString 对开始时间、结束时间和日期有效吗?它们都是 DATETIME 对象:

PreparedStatement pstmt = conn.prepareStatement("UPDATE Event SET date=?, begintime=?, endtime=?, status=?, productionid=?, conceptual_packageid=? WHERE id=?");
        pstmt.setString(1, pkg.getDate());
        pstmt.setString(2, pkg.getBeginTime());
        pstmt.setString(3, pkg.getEndTime());
        pstmt.setString(4, pkg.getStatus());
        pstmt.setString(5, pkg.getProductionID());
        pstmt.setString(6, pkg.getConceptual_PackageID());
        pstmt.setString(7, pkg.getId());

        pstmt.executeUpdate();
        pstmt.close();

I am trying to to do this:

pr.setStartdate("2006-09-10T00:00:00");

I am getting this error:

java.sql.SQLDataException: The syntax of the string representation of a datetime value is incorrect.

any ideas on how to successfully insert would be great.

here is a little more code.
now do i need to setDate? or does setString work for begintime, endtime, and date? they are all DATETIME objects:

PreparedStatement pstmt = conn.prepareStatement("UPDATE Event SET date=?, begintime=?, endtime=?, status=?, productionid=?, conceptual_packageid=? WHERE id=?");
        pstmt.setString(1, pkg.getDate());
        pstmt.setString(2, pkg.getBeginTime());
        pstmt.setString(3, pkg.getEndTime());
        pstmt.setString(4, pkg.getStatus());
        pstmt.setString(5, pkg.getProductionID());
        pstmt.setString(6, pkg.getConceptual_PackageID());
        pstmt.setString(7, pkg.getId());

        pstmt.executeUpdate();
        pstmt.close();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

两仪 2024-10-23 11:26:51

我建议您使用PreparedStatement 的 setTimestamp(...) 和 setDate(...) 方法,然后将实际的 Date 对象传递给它们,而不是使用字符串。如果您仅限于使用“pr”类中的字符串,则将字符串转换为 derby doc 然后使用下面的代码

java.sql.Timestamp.valueOf("time/date converted");

I'd suggest you use setTimestamp(...) and setDate(...) methods of PreparedStatement and then pass in the actual Date objects to them rather than working with strings. If you are limited to working with Strings in the "pr" class then convert the Strings to the format specified in the derby doc and then use the below code

java.sql.Timestamp.valueOf("time/date converted");
梦年海沫深 2024-10-23 11:26:51

据我所知,derby 没有“DATETIME”数据类型,它们支持 DATE、TIME 和 TIMESTAMP。

http://db.apache.org/derby/docs/10.10/ref /crefsqlj31068.html

因此,如果您想在该字段中保留时间和日期值,我会确保您将“日期”列声明为 TIMESTAMP。

其次,我将单引号列名“日期”,因为 DATE 是保留数据类型。我不知道这是否是这里的问题,但可能想尝试一下。

第三,当设置/获取“日期”列时,我会尝试使用 set/getTimestamp 并在适用的情况下传递/分配 java.sql.Timestamp 对象。

希望能提供一些帮助。

From what I can tell, derby has no "DATETIME" data type, they support DATE, TIME, and TIMESTAMP.

http://db.apache.org/derby/docs/10.10/ref/crefsqlj31068.html

As such I would make sure you're declaring your 'date' column as a TIMESTAMP if you are wanting to keep both the time and date value in that field.

Second I would single quote the column name 'date' as DATE is a reserved data type. I don't know if that would be the problem here but might want to try it.

Thirdly, when setting/getting the 'date' column, I would try to use set/getTimestamp and pass/assign a java.sql.Timestamp object where applicable.

Hope that provides some help.

只是我以为 2024-10-23 11:26:51

日期、时间和时间戳不能
表情中相互混合。
Derby 支持以下格式
对于时间戳:

年-月-日 时:分:秒[.nnnnnn]
yyyy-mm-dd-hh.mm.ss[.nnnnnn]

年份必须始终有四位数字。
月、日、小时可能有一个
或两位数。分钟和秒
必须有两位数。纳秒,如果
目前,可能有 1 到 6 个
数字。

因此,您应该将“T”替换为空格或连字符。

Dates, times, and timestamps cannot be
mixed with one another in expressions.
Derby supports the following formats
for TIMESTAMP:

yyyy-mm-dd hh:mm:ss[.nnnnnn]
yyyy-mm-dd-hh.mm.ss[.nnnnnn]

The year must always have four digits.
Months, days, and hours may have one
or two digits. Minutes and seconds
must have two digits. Nanoseconds, if
present, may have between one and six
digits.

So, you should replace 'T' with space or hyphen.

最终幸福 2024-10-23 11:26:51

万一其他人遇到这个几个月前的问题(就像我一样)做类似但不完全相同的操作:仔细查看 Lev Khomich 的答案中的两种格式:天/小时之间有一个空格,小时/分钟之间有冒号,以及日/小时之间的破折号以及小时/分钟之间的句点。我刚刚遇到一个案例,使用一个 SQL 语句,其中的句点位于小时/分钟之间,其中天/小时之间的空格导致错误,但破折号却没有。

我习惯了 DB2,当使用小时/分钟和分钟/秒之间的句点时,它接受破折号或空格,这使得它更容易被忽视。我只是假设一天/小时之间允许有空格或破折号。

In case someone else runs across this months-old question (as I did) doing a similar but not identical operation: Look carefully at the two formats in Lev Khomich's answer: there is a space between day/hour with colons between the hour/min, and a dash between day/hour with periods between hour/min. I just had a case, using a SQL statement with periods between hour/min, where a space between day/hour caused an error but a dash did not.

I'm used to DB2, which accepts either a dash or a space when using periods between hour/min and min/sec, making it even easier to overlook. I was just assuming either space or dash was allowed between day/hour.

月光色 2024-10-23 11:26:51

我遇到了同样的问题,然后我在这里找到了解决方案:

http://apache-database.10148.n7.nabble.com/Date-Timestamp-format-for-inserts-td99979.html

基本上你必须使用这种格式:

yyyy-mm-dd-hh.mm.ss[.nnnnnn]

例如,这个插入对我有用:

statement.executeUpdate("INSERT INTO animal VALUES (1, 1, 'Elsa', '2006-09-10-00.00.00')");

I had the same issue, then I found the solution here:

http://apache-database.10148.n7.nabble.com/Date-Timestamp-format-for-inserts-td99979.html

Basically you have to use this format:

yyyy-mm-dd-hh.mm.ss[.nnnnnn]

For instances, this insert works for me:

statement.executeUpdate("INSERT INTO animal VALUES (1, 1, 'Elsa', '2006-09-10-00.00.00')");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文