eclipsems-accessjdbcucanaccessjdatechooser

When I try enter date chosen by Jdatechooser it I get the Error "data exception: invalid datetime format"


I want to input into my MS Access table the date that the user inputs using the Jdatechooser.

In Access I have set the Date/time column to 'short date', but any format I try won't work. Help!

try 
{
    Connection conn=DriverManager.getConnection("jdbc:ucanaccess://E:\\testing.accdb");
    String sql="insert into Homework (Description,Subject_ID,Name,Due_Date) values (?,?,?,?) ";
    PreparedStatement pst=conn.prepareStatement(sql);
    pst.setString(1, textFieldDes.getText());
    pst.setString(2, textFieldID.getText());
    pst.setString(3, textFieldName.getText());
    SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
    String date= sdf.format(dateChooser.getDate());
    pst.setString(4,date);
    pst.executeUpdate();
    JOptionPane.showMessageDialog(null, "Data Saved");
    pst.close();
} 
catch (Exception e) 
{
    e.printStackTrace();
}

Solution

  • A date value carries no format. Try inserting the true date value as is:

    pst.setDate(4, dateChooser.getDate());