将数据从datepicker添加到db javafx中

sqserrrh  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(325)

我正试图将日期选择器中的数据添加到我的数据库中,但是在提交查询之后,在数据库中,日期列中没有添加日期,我很确定在代码中从日期选择器中获取日期的方式是完全错误的,但我似乎找不到解决方法。。

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

}
}

没有错误信息,什么都没有,只是不起作用。

k5hmc34c

k5hmc34c1#

你用 getValue() 获取用户从 DatePicker . 因为这是一个日期,所以在数据库中将其表示为一个日期并使用 setDate(...)PreparedStatement . 所以:

@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(); /
   }

}

你需要的进口商品是 java.time.LocalDate 以及 java.sql.Date .

相关问题