0

I want to insert a product into table. For that I have a Date field for which I use Date Chooser Combo. and for adding date for that product I wrote the following code,

String sql = "INSERT INTO ProductInfo(Name,Price,Date,Image,Category) values(?,?,?,?,?) ";
            pst=conn.prepareStatement(sql);
            pst.execute();

            pst.setString(1, txt_name.getText());
            pst.setString(2, txt_price.getText());

            SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
            String addDate = dateFormat.format(txt_AddDate.getDateFormat());
            pst.setString(3, addDate);

But this is giving me "Cannot format given object as a date" error. I've tried a lot but can't solve. Anyone help me please?

Rifa Tabassum
  • 15
  • 2
  • 6

1 Answers1

0

in

pst.setString(3, addDate);

you are telling that you want to use a String in a Date property.

you should change it to

pst.setDate(3, addDate);

And also why not use the date that you have? why not?

pst.setDate(3, txt_AddDate.getDateFormat());

If this don't work because a format or simple because it doesn't return a date and you still want to use the SimpleDateFormat you could use something like:

pst.setDate(3, java.sql.Date.valueOf(addDate));
liponcio
  • 264
  • 1
  • 9