Govinda Sakhare
Govinda Sakhare

Reputation: 5739

inserting html5 datepicker into oracle getting exceptions

i am trying to insert html5 datepicker's date into Oracle table having datatype date,i've tried so many thing but i am still facing problem,plz help me to solve the issue.

static int addNotification(String date,String message)
{
    int i=0;
    try {
        ps=con.prepareStatement("INSERT into ev values(?,to_date(?,'DD-MON-YYYY'))");           
        ps.setString(1, message);

         DateFormat originalFormat = new SimpleDateFormat("yyyyMMdd");
          DateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          java.util.Date dt = originalFormat.parse(date);
            String formattedDate = targetFormat.format(dt); 
            System.out.println(formattedDate);
            ps.setString(2, formattedDate);
    //  ps.setDate(2,formattedDate);

        i=ps.executeUpdate();
    }

everytime i am making changes , i am getting new exception for ex.

java.sql.SQLException: ORA-01861: literal does not match format string

java.text.ParseException: Unparseable date('YYYY-MM-DD')

plz help me to solve this problem.

Upvotes: 0

Views: 293

Answers (2)

user4436160
user4436160

Reputation:

if you want to convert the date then use,

 DateFormat originalFormat = new SimpleDateFormat("yyyyMMdd");

if not would suggest you not capture html 5 datepickers data directly ,I would rather convert it using javascript and send it to response.

go through date class :-

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Upvotes: 0

seenukarthi
seenukarthi

Reputation: 8664

You can use PreparedStatement.setDate(int parameterIndex,Date x) as follows.

try {
    ps=con.prepareStatement("INSERT into ev values(?,?)");           
    ps.setString(1, message);

    DateFormat originalFormat = new SimpleDateFormat("yyyyMMdd");
    java.util.Date dt = originalFormat.parse(date);
    ps.setDate(2,dt);

    i=ps.executeUpdate();
}

Upvotes: 1

Related Questions