0

I'm trying to add data from a datepicker into my db, but after submitting the query, in the db, no date in the Date column has been added, I'm pretty sure the way that I'm getting the date from the datepicker in my code is awfully wrong, but I can't seem to find a workaround..

package LicentaApp;

import javafx.scene.control.DatePicker;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.sqlite.SQLiteErrorCode;
import org.sqlite.SQLiteException;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import java.sql.Date;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextField;

import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class AddProgramareController implements Initializable {
ObservableList Timestamp=FXCollections.observableArrayList();

@FXML
private TextField Nume;

@FXML
private TextField Prenume;

@FXML
private TextField Ora;

@FXML
private DatePicker Data; Data is date in my language btw

@FXML
private TextField Departament;

@FXML
private TextField Doctor;

@FXML
private TextField Nr_telefon;

public void initialize(URL location, ResourceBundle resources) {
} 

@FXML 
private void AddProgramare(ActionEvent event) throws SQLException,         IOException  {


    String Interogare1= "INSERT INTO programaritest(Nume,Prenume,Data,Ora,Departament,Doctor,Nr_telefon) VALUES(?,?,?,?,?,?,?)";

    String nume=Nume.getText();
    String prenume=Prenume.getText();
    String data=Data.getPromptText(); *Date
    String ora=Ora.getText();
    String departament=Departament.getText();
    String doctor=Doctor.getText();
    String nr_telefon=Nr_telefon.getText();


    try {
         ConectaredB ConectaredB=new ConectaredB();
         Connection conexiune=ConectaredB.logareDB();
        PreparedStatement MG = conexiune.prepareStatement(Interogare1);


        MG.setString(1, nume);
        MG.setString(2, prenume);
        MG.setString(3, data); *Date
        MG.setString(4, ora);
        MG.setString(5, departament);
        MG.setString(6, doctor);
        MG.setString(7, nr_telefon);


        MG.executeUpdate();

         ((Node)event.getSource()).getScene().getWindow().hide();
            Stage PS= new Stage();
            FXMLLoader incarcator= new FXMLLoader();
            Pane parinte = incarcator.load(getClass().getResource("/LicentaApp/Departamente.fxml").openStream());
            Scene scena = new Scene(parinte);
            scena.getStylesheets().add(getClass().getResource("Style1212.css").toExternalForm());
            PS.setScene(scena);
            PS.show();
            System.out.println("Programare adaugata muchacho");



    } catch (SQLException exceptie1) {
        exceptie1.printStackTrace(); // 
    }

}
}

No error message, no nothing, it just doesn't work.

1 Answer 1

1

You use getValue() to get the value the user has chosen from a DatePicker. Since this is a date, represent it as a date in the database and use setDate(...) on the PreparedStatement. So:

@FXML 
private void AddProgramare(ActionEvent event) throws SQLException, IOException  {


    String Interogare1= "INSERT INTO programaritest(Nume,Prenume,Data,Ora,Departament,Doctor,Nr_telefon) VALUES(?,?,?,?,?,?,?)";

    String nume=Nume.getText();
    String prenume=Prenume.getText();

    LocalDate data=Data.getValue(); 

    String ora=Ora.getText();
    String departament=Departament.getText();
    String doctor=Doctor.getText();
    String nr_telefon=Nr_telefon.getText();


    try {
        ConectaredB ConectaredB=new ConectaredB();
        Connection conexiune=ConectaredB.logareDB();
        PreparedStatement MG = conexiune.prepareStatement(Interogare1);


        MG.setString(1, nume);
        MG.setString(2, prenume);
        MG.setDate(3, Date.valueOf(data)); 
        MG.setString(4, ora);
        MG.setString(5, departament);
        MG.setString(6, doctor);
        MG.setString(7, nr_telefon);


        MG.executeUpdate();

        // ...
   } catch (SQLException exceptie1) {
       exceptie1.printStackTrace(); /
   }


}

The imports you need here are java.time.LocalDate and java.sql.Date.

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

1 Comment

Thanks for ur time and the help, the explanations also, have a great day !

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.