如何修复将 DATETIME 插入数据库时的错误
我正在尝试执行此操作:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我建议您使用PreparedStatement 的 setTimestamp(...) 和 setDate(...) 方法,然后将实际的 Date 对象传递给它们,而不是使用字符串。如果您仅限于使用“pr”类中的字符串,则将字符串转换为 derby doc 然后使用下面的代码
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
据我所知,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.
因此,您应该将“T”替换为空格或连字符。
So, you should replace 'T' with space or hyphen.
万一其他人遇到这个几个月前的问题(就像我一样)做类似但不完全相同的操作:仔细查看 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.
我遇到了同样的问题,然后我在这里找到了解决方案:
http://apache-database.10148.n7.nabble.com/Date-Timestamp-format-for-inserts-td99979.html
基本上你必须使用这种格式:
例如,这个插入对我有用:
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:
For instances, this insert works for me: