简体   繁体   English

在数据库中存储当前日期

[英]Storing Current date in database

I am having a column in my table of type TIMESTAMP . 我的表中有TIMESTAMP类型的列。 In my servlet am writing the code to insert current date and time in this column like this : 在我的servlet中,正在编写代码以在此列中插入当前日期和时间,如下所示:

 DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 Date date = new Date();
 String sendDate = dateFormat.format(date);
 ps.setString(4,sendDate);

But it throws an Exception that: 但是它抛出一个Exception

 java.sql.SQLException: [Oracle][ODBC][Ora]ORA-01843: not a valid month

What can be the reason? 可能是什么原因?

Please help. 请帮忙。

Why do you use ps.setString ? 为什么使用ps.setString You should use ps.setDate . 您应该使用ps.setDate

EDIT 编辑

Date dateNow = new java.sql.Date(System.CurrentTimeMillis())
ps.setDate(dateNow);

Better to use timestamp tough: 最好使用强悍的时间戳:

ps.setTimestamp(1, new Timestamp(System.currentTimeMillis()); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM