Insert date from Jdatepicker into MS Access

308 Views Asked by At

I have a GUI java interface that allows me to open a JDataPicker in order to insert the selected date into a ms-access database.

I tried inserting the date directly from the JDataPicker : Date date1 =(Date) jdate_picker.getModel().getSelectedValue(); However, the format retrieved returns : java.text.ParseException: Unparseable date: “Tue Apr 26 00:00:00 EDT 2016” (at offset 0).

I attempted several things but nothing seems to work. For instance, I tried retrieving the month, year and day separately through jdate_picker.getModel().getYear () (and getMonth() and getDay()). Then I concatenated the results to get a MM/DD/YYYY format. After that I used DateFormatter to convert the string to a date format but it still, I'm getting date format exceptions. My questions are as follow:

1- Is there a way that I haven't thought of which would allow me to insert a date from JDatePicker into an Access database?

2- If not, is there another tool that would allow me to select a date and send it to the Access database?

Thank you.

1

There are 1 best solutions below

0
On

You are using troublesome old date-time classes that have been supplanted by the java.time classes. Avoid using Date and Calendar.

For a date-only value without time zone and without time-of-day, use LocalDate.

LocalDate.of(
    jdate_picker.getModel().getYear() , 
    jdate_picker.getModel().getMonth(), 
    jdate_picker.getModel().getDay()
)

Exchange data with your database as objects rather than mere strings.

If your JDBC driver complies with JDBC 4.2 and later, you can directly use java.time objects.

myPreparedStatement.setObject( … , myLocalDate ) ; 

And to retrieve:

LocalDate myLocalDate = myResultSet.getObject( … , LocalDate.class ) ;

These issues have been covered many times already on Stack Overflow. Search for more info.