2

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();
}
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
Hassan Zaidi
  • 41
  • 1
  • 7

1 Answers1

1

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

pst.setDate(4, dateChooser.getDate());
Gustav
  • 53,498
  • 7
  • 29
  • 55