3

I'm trying to insert date to mysql database from data-picker using javaFx. I am tired by submit this code.

@FXML
private DatePicker DPCurrentDate;//fx:id="DPCurrentDate"

// BrandAdded is a Method that i created for insert data into database

private void BrandAdded(){

    DBConnection newCon = new DBConnection();
    con = newCon.geConnection();

    try {
        pst = con.prepareStatement("insert into Brands values(?,?,?,?,?)");
        pst.setString(1, null);
        pst.setString(2, TFBrandName.getText());
        pst.setString(3, TABrandDecription.getText());
        pst.setString(4, lblUserName.getText());
        pst.setDate(5, DPCurrentDate.getValue());
        pst.executeUpdate();

    } catch (SQLException ex) {
        Logger.getLogger(AddBrandController.class.getName()).log(Level.SEVERE, null, ex);
    }
}

when i run my program it give me this error

error: incompatible types: LocalDate cannot be converted to Date
        pst.setDate(5, DPCurrentDate.getValue());

4 Answers 4

4

You need

java.util.Date date = 
    java.util.Date.from(dpCurrentDate.getValue().atStartOfDay(ZoneId.systemDefault()).toInstant());
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
pst.setDate(5, sqlDate);

(using a java.sql.Date).

Sign up to request clarification or add additional context in comments.

3 Comments

how i get the dpCurrentDate where i have DPCurrentDate as Datapicker.
I meant the same thing... I was just correcting your variable name so that it matched standard naming conventions :). I had a bunch of other errors in there though (don't know what I was thinking when I put that together...). They are now fixed.
I'm using this solution in year 2020 with JDK 8. It still works. Thank you so much.
0
import java.sql.Date;
Date  DPCurrentDate1 =  Date.valueOf(DPCurrentDate);
pst.setDate(5, DPCurrentDate1);

This would do your work

Comments

0

use this in your method save or whatever you named it.

dateTimePicker.valueProperty().get(),

Note. close it with ) if that is your last item to save.

Comments

0

You can try this:

pst.setDate(5, ((TextField)DPCurrentDate.getEditor()).getText());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.