java.sql.SQLException: ORA-01843: not a valid mont

2019-09-20 08:38发布

问题:

I am getting date in html 5 input type = "date" format..in mm/dd/yyyy format. Then I am getting it String fDate = request.getParameter("fromDate");

room.setFromDate(fDate);

Then while trying to run query

insert into tbl_room_under_maintenance(room_id,room_number,from_date,to_date)
values(?,?,to_date(?,'MM/DD/YYYY'),to_date(?,'MM/DD/YYYY'))

PS1.setString(3,room.getFromDate().trim());
PS1.setString(4,room.getToDate().trim());

it shows error : java.sql.SQLException: ORA-01843: not a valid month

回答1:

Convert the date before insert:

String fDate = request.getParameter("fromDate");
SimpleDateFormat sm = new SimpleDateFormat("mm/dd/yyyy");
Date dat = sm.parse(fDate);
insert into tbl_room_under_maintenance(room_id,room_number,from_date,to_date)
values(?,?,?,?)
PS1.setDate(3,dat);