使用 Java 在 Postgresql 中输入日期值

发布于 2024-12-08 10:56:51 字数 434 浏览 1 评论 0原文

我正在尝试使用 java & 编写数据库应用程序PostgreSQL。我有一些带有日期数据类型的行。但我无法使用此代码向数据库添加任何条目:

Date aDate = null;
aDate.setYear(1990);
aDate.setDate(01);
aDate.setMonth(05);

preparedStatement prep = connection.prepareStatement("insert 
into exampletable values (?,?);");
prep.setDate(1, (java.sql.Date) aDate);
prep.setDate(2, (java.sql.Date) aDate);

How can I add a date in a postgreSQL row with query in java?

I am trying to program a database application with java & PostgreSQL. I have some rows with date data type. But i cant add any entries to the database with this code :

Date aDate = null;
aDate.setYear(1990);
aDate.setDate(01);
aDate.setMonth(05);

preparedStatement prep = connection.prepareStatement("insert 
into exampletable values (?,?);");
prep.setDate(1, (java.sql.Date) aDate);
prep.setDate(2, (java.sql.Date) aDate);

How can i add a date in a postgreSQL row with queries in java?

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

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

发布评论

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

评论(1

寒江雪… 2024-12-15 10:56:51

目前尚不清楚这是否是您唯一的问题,但这段代码几乎肯定不是您想要的:

Date aDate = null;
aDate.setYear(1990);
aDate.setDate(01);
aDate.setMonth(05);
  • 它将抛出 NullPointerException 因为您试图取消引用 null
  • 然后,您尝试将年份设置为 3890AD(java.util.Date 的年份是基于 1900 年),
  • 然后将月份设置为 6 月。如果您认为将月份设置为五月,请再想一想 - Date 是从 0 开始的月份
  • 敲响警钟
  • 您使用的所有方法均已弃用 - 这应该给您 然后尝试将 aDate 转换为 java.sql.Date 但没有迹象表明它 java.sql.Date

我建议:

  • 要么使用 Joda Time 作为更好的日期/时间 API,或 java.util.Calendar
  • 确保您在设置值之前实际创建了一个实例
  • 稍后可能会创建一个新的 java.sql.Date 。

例如:

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 1990);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.MONTH, 4); // Assuming you wanted May 1st

java.sql.Date date = new java.sql.Date(calendar.getTime().getTime());

// Ideally specify the columns here as well...
PreparedStatement prep = connection.prepareStatement(
    "insert into exampletable values (?,?)");
prep.setDate(1, date);
prep.setDate(2, date);

It's not clear whether or not this is your only problem, but this code is almost certainly not what you want:

Date aDate = null;
aDate.setYear(1990);
aDate.setDate(01);
aDate.setMonth(05);
  • It will throw a NullPointerException because you're trying to dereference null
  • You're then trying to set the year to 3890AD (java.util.Date is 1900-based for years)
  • You're then setting the month to June. If you thought you were setting the month to May, think again - Date is 0-based for months
  • All the methods you're using are deprecated - that should raise a big warning light for you
  • You're then trying to cast aDate to java.sql.Date but there's no sign that it is a java.sql.Date

I would suggest:

  • Either use Joda Time as a far better date/time API, or java.util.Calendar
  • Make sure you actually create an instance before you set values
  • Probably create a new java.sql.Date later on.

For example:

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 1990);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.MONTH, 4); // Assuming you wanted May 1st

java.sql.Date date = new java.sql.Date(calendar.getTime().getTime());

// Ideally specify the columns here as well...
PreparedStatement prep = connection.prepareStatement(
    "insert into exampletable values (?,?)");
prep.setDate(1, date);
prep.setDate(2, date);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文