K M Rifat ul alom
K M Rifat ul alom

Reputation: 525

Insert date into MySql Database using JavaFX Datepicker

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());

Upvotes: 3

Views: 10126

Answers (4)

Smmy Sa
Smmy Sa

Reputation: 571

You can try this:

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

Upvotes: 0

Jonathan Tetteh
Jonathan Tetteh

Reputation: 11

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.

Upvotes: 0

Sreekanth
Sreekanth

Reputation: 31

import java.sql.Date;
Date  DPCurrentDate1 =  Date.valueOf(DPCurrentDate);
pst.setDate(5, DPCurrentDate1);

This would do your work

Upvotes: 0

James_D
James_D

Reputation: 209340

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

Upvotes: 4

Related Questions