我正在尝试将数据从datepicker添加到我的数据库中,但是在提交查询之后,在数据库中,没有添加Date列中的日期,我很确定我获得的方式我的代码中的datepicker日期非常错误,但我似乎无法找到解决方法..
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(); //
}
}
}
没有错误信息,没有任何错误,只是不起作用。
答案 0 :(得分:1)
您使用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
。