我正在创建javafx应用程序,其中我插入了从数据库填充到另一个数据库表的组合框的所选项目,但问题是组合框的选择模型在给出被选中的项目的某个时候是不一致的但有些它会将空值发送给数据库
代码:
package application;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
import org.controlsfx.control.textfield.TextFields;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TableColumn;
public class EntryFormController implements Initializable {
Connection connection = SqliteConnection.Connector();
PreparedStatement preparedStatement = null;
ResultSet rs = null;
ObservableList<String> cBoxList1 = FXCollections.observableArrayList();
ObservableList<String> cBoxList2 = FXCollections.observableArrayList();
@FXML
private ComboBox<String> TruckNo;
@FXML
private ComboBox<String> exp1;
@FXML
private ComboBox<String> exp2;
@FXML
private void addButton(ActionEvent e) {
String query = "insert into entry (Truck,Expense)values(?,?),(?,?) ";
try {
preparedStatement = connection.prepareStatement(query);
preparedStatement.setString
(2,TruckNo.getSelectionModel().getSelectedItem());
preparedStatement.setString
(3, exp1.getSelectionModel().getSelectedItem());
TruckNo.getSelectionModel().getSelectedItem());
preparedStatement.setString
(9,exp2.getSelectionModel().getSelectedItem());
preparedStatement.execute();
TruckNo.setValue(null);
TruckNo.valueProperty().set(null);
exp1.setValue(null);
exp1.valueProperty().set(null);
exp2.setValue(null);
exp2.valueProperty().set(null);
preparedStatement.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
@Override
public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub
exp1.setItems(cBoxList1);
exp1.setEditable(true);
loadDatabaseDataCombo1();
exp2.setItems(cBoxList2);
exp2.setEditable(true);
TruckNo.setItems(trkList);
TruckNo.setEditable(true);
loadDatabaseDataCombo6();
TextFields.bindAutoCompletion(exp1.getEditor(), exp1.getItems());
TextFields.bindAutoCompletion(exp2.getEditor(), exp2.getItems());
TextFields.bindAutoCompletion(TruckNo.getEditor(),TruckNo.getItems());
}
private void loadDatabaseDataCombo6() {
String query = "select * from Truck";
try {
preparedStatement = connection.prepareStatement(query);
rs = preparedStatement.executeQuery();
while (rs.next()) {
trkList.add(rs.getString("TruckNo"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private void loadDatabaseDataCombo2() {
String query = "select * from Expense";
try {
preparedStatement = connection.prepareStatement(query);
rs = preparedStatement.executeQuery();
while (rs.next()) {
cBoxList2.add(rs.getString("name"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private void loadDatabaseDataCombo1() {
String query = "select * from Expense";
try {
preparedStatement = connection.prepareStatement(query);
rs = preparedStatement.executeQuery();
while (rs.next()) {
cBoxList1.add(rs.getString("name"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}