【发布时间】:2012-03-23 20:17:27
【问题描述】:
在我的 oracle 数据库中插入数据时出现以下错误。
java.sql.SQLException: ORA-01843: not a valid month
数据库中的日期为:dd-MMM-yy (06-MAR-12)
我正在通过以下方法将 06-03-2012 转换为 dd-MMM-yy:
String s="06-03-2012";
String finalexampledt = new SimpleDateFormat("dd-MMM-yy").format(new SimpleDateFormat("dd-MM-yyyy").parse(s));
所以我得到了 12 年 3 月 6 日,这与上述数据库日期格式相同,但我仍然收到错误消息。我插入为:
在 index.jsp 中
String todaydate="";
Calendar calendar1 = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
todaydate = dateFormat.format(calendar1.getTime());
<input type="text" name="datename" value="<%=todaydate%>"/>
在 servlet(doPost) 中
String s=request.getParameter("datename");
PreparedStatement ps=con.prepareStatement("insert into tablename(rest_dt, othercolname) values (to_date(?, 'dd-mm-yyyy'), ?)");
ps.setString(1, s);
ps.setString(2, otherstringdata);
int rs=ps.executeUpdate();
有什么建议
【问题讨论】:
-
什么类型的列有这个日期?日期类型?
-
oracle 中的日期类型列
-
如何将值插入数据库?
-
@student:你确定你输入的日期总是像 dd-MM-yyyy (英国/法国格式),而不是 MM-dd-yyyy (美国格式)吗?该输入中是否存在不是日期的内容?
-
你能给我们看看java代码吗?