JDBC DATE 格式错误

发布于 2024-11-08 15:33:09 字数 352 浏览 0 评论 0原文

我试图从数据库中读取多个日期,但在某些情况下我收到“java.sql.SQLException Bad format for DATE”。这是我的代码:

Date entryDateD = res.getDate("entryDate");

在调试模式下,我看到entryDateD'1996-9-15'的内容与我的数据库中一样。 尽管我不得不提一下,我也从数据库中读取了其他日期,我注意到这些日期的格式为“xxxx-0y-zz”。我想说的是,如果一个月小于 10,则在其前面添加一个零,在这种情况下不添加零。我怀疑这可能与此有关。 (这个零不会出现在数据库本身中,不仅出现在这个日期,而且出现在任何日期) 提前谢谢:)

I am trying to read several dates from my database but under certain circumstances I get a ' java.sql.SQLException Bad format for DATE'. Here is my code :

Date entryDateD = res.getDate("entryDate");

In debug mode I see that the content of entryDateD '1996-9-15' as is in my database..
Although I would have to mention that I read other dates too from my database which I notice are of the format 'xxxx-0y-zz'. What I want to say is that in case of a month being less than 10 there is a zero added in front of it which in this case is not added. I suspect that this might have something to do with it.
(this zero does not appear in the database itself though not only in this date but in any date)
thanx in advance :)

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

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

发布评论

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

评论(4

御弟哥哥 2024-11-15 15:33:09

只是猜测,但是你的entryDate 有什么MySQL 列类型?

默认情况下,MySQL 中的日期类型会生成 yyyy-mm--dd 格式;缺少零让我相信列类型可能是 varchar 或数据库级别的其他非日期类型。

这可能是 Java 级别出现问题的原因......

Just a guess, but what MySQL column type do you have for entryDate??

By default a date type in MySQL will generate an yyyy-mm--dd format; the missing zero leads me to believe that the column type may be varchar or other non-date type at DB level.

This could be the cause of your problems at Java level...

撩心不撩汉 2024-11-15 15:33:09

如果将其表示为 java.util.Date,则具有允许 JDBC 驱动程序担心处理数据库的任何格式问题的优点。

至于如何在显示器中呈现它,这取决于您和您对 java.text.DateFormat 类的使用。

您在应用程序中将日期表示为 java.util.Date 是正确的做法,但您需要了解格式设置是与类型不同的问题。

If you represent it as java.util.Date, you have the advantage of allowing the JDBC driver to worry about handling any format issues with the database.

As for how it's rendered in your display, that's up to you and your use of the java.text.DateFormat class.

You're doing the right thing by representing a date as java.util.Date in your app, but you need to understand that formatting is a separate issue from the type.

萝莉病 2024-11-15 15:33:09

Java 有一个 java.util.Date 的子类,名为 java.sql.Date

创建 Java SQL 日期的最简单方法是使用实​​用程序日期调用构造函数。

java.util.Date uDate = res.getDate("entryDate");
java.sql.Date sDate  = new java.sql.Date(uDate.getTime());

Java has a child class of java.util.Date, called java.sql.Date

The easiest way to create a Java SQL Date is by calling the constructor with a Utility Date.

java.util.Date uDate = res.getDate("entryDate");
java.sql.Date sDate  = new java.sql.Date(uDate.getTime());
灯角 2024-11-15 15:33:09

如果您想将 res.getDate("entryDate") 获取为 YYYY-MM-DD 那么您需要在查询中将其转换为日期,如下所示:

SELECT
...
CAST(entryDate as DATE)as entryDate,
...
FROM your_table;

如果您有这样的感觉有帮助,然后进行投票以使其对其他人有用。

If you want to get res.getDate("entryDate") as YYYY-MM-DD then you need to cast as date in your query like this:

SELECT
...
CAST(entryDate as DATE)as entryDate,
...
FROM your_table;

If you feel this is helpful then do upVote to make it useful for others.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文