[英]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.