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());
K M Rifat ul alom
  • 525
  • 12
  • 25

4 Answers4

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).

James_D
  • 201,275
  • 16
  • 291
  • 322
  • how i get the `dpCurrentDate` where i have `DPCurrentDate` as Datapicker. – K M Rifat ul alom May 16 '15 at 19:03
  • 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. – James_D May 16 '15 at 20:28
  • I'm using this solution in year 2020 with JDK 8. It still works. Thank you so much. – Tanzeel Feb 25 '20 at 18:42
0
import java.sql.Date;
Date  DPCurrentDate1 =  Date.valueOf(DPCurrentDate);
pst.setDate(5, DPCurrentDate1);

This would do your work

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
Sreekanth
  • 31
  • 1
  • 11
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.

K.Dᴀᴠɪs
  • 9,945
  • 11
  • 33
  • 43
0

You can try this:

pst.setDate(5, ((TextField)DPCurrentDate.getEditor()).getText());
shafik
  • 6,098
  • 5
  • 32
  • 50
Smmy Sa
  • 491
  • 1
  • 5
  • 5