我正在研究一个小型JavaFX应用程序,该应用程序将数据存储到表视图中,然后允许用户操纵数据。我当前的问题是将单元格数据从tableview保存到数据库中。
我不知道如何存储整个单元格数据,然后使用基于主键的更新查询将其更新为SQL,在本例中为“卡车编号”列。我需要存储的主要数据来自ComboBox和CheckBox,但是获取其他列以及如何获取相应的Truck#也没有什么坏处。因此,我的目标是使用户能够在comboBox中输入一些值,然后单击菜单中的save选项,并将单元格信息存储到数据库中,而不是使用文本字段和单独更新项目。
在“组合框”列中写入的内容应该保存https://imgur.com/a/UCOTnLm
我已经具有更新功能,用户可以输入特定的Truck#并编辑其他字段来更新数据库,但是我需要将TableView中当前的所有内容保存到数据库中。
public class TruckInfo {
private CheckBox In;
private CheckBox Out;
private String truck_id;
private Integer year;
private String pallet_num;
private String tr_class;
private ComboBox<String> msgBox;
private String combo;
public TruckInfo() {
this.truck_id = "";
this.year = 0;
this.pallet_num = "";
this.tr_class = "";
this.combo = "";
}
public TruckInfo(String truck_id, Integer year, String pallet_num, String tr_class, String combo) {
this.In = new CheckBox();
In.getStyleClass().add("check-box-style-in");
this.Out = new CheckBox();
Out.getStyleClass().add("check-box-style-out");
this.truck_id = truck_id;
this.year = year;
this.pallet_num = pallet_num;
this.tr_class = tr_class;
this.msgBox = new ComboBox<String>();
msgBox.getItems().addAll("Kenosha", "Keep Local", "Do Not Use", "Shop");
msgBox.setEditable(true);
msgBox.setValue(combo);
this.combo = combo;
}
public String getCombo() {
return combo;
}
public void setCombo(String combo) {
this.combo = combo;
}
public CheckBox getIn() {
return In;
}
public void setIn(CheckBox in) {
In = in;
}
public CheckBox getOut() {
return Out;
}
public void setOut(CheckBox out) {
Out = out;
}
public String getTruck_id() {
return truck_id;
}
public void setTruck_id(String truck_id) {
this.truck_id = truck_id;
}
public Integer getYear() {
return year;
}
public void setYear(Integer year) {
this.year = year;
}
public String getPallet_num() {
return pallet_num;
}
public void setPallet_num(String pallet_num) {
this.pallet_num = pallet_num;
}
public String getTr_class() {
return tr_class;
}
public void setTr_class(String tr_class) {
this.tr_class = tr_class;
}
public ComboBox<String> getMsgBox() {
return msgBox;
}
public void setMsgBox(ComboBox<String> msgBox) {
this.msgBox = msgBox;
}
}
public class TruckController implements Initializable {
private TruckDAO dao;
ObservableList<TruckInfo> obList = FXCollections.observableArrayList();
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
dao = new TruckDAO();
try {
Connection connection = new DBConnect().getConnection();
ResultSet rs = connection.createStatement().executeQuery("SELECT * FROM truck");
while (rs.next()) {
obList.add(new TruckInfo(rs.getString("truck_id"), rs.getInt("year"),
rs.getString("pallet_num"), rs.getString("class"), rs.getString("message")));
}
System.out.println("Database Connected");
connection.close();
} catch (SQLException e) {
Logger.getLogger(TruckController.class.getName()).log(Level.SEVERE, null, e);
}
inColumn.setCellValueFactory(new PropertyValueFactory<>("In"));
outColumn.setCellValueFactory(new PropertyValueFactory<>("Out"));
truckNumColumn.setCellValueFactory(new PropertyValueFactory<>("truck_id"));
yearColumn.setCellValueFactory(new PropertyValueFactory<>("year"));
palletNumColumn.setCellValueFactory(new PropertyValueFactory<>("pallet_num"));
trClassColumn.setCellValueFactory(new PropertyValueFactory<>("tr_class"));
msgColumn.setCellValueFactory(new PropertyValueFactory<>("msgBox"));
truckTable.setItems(obList);
}
@FXML
public TableView<TruckInfo> truckTable;
@FXML
public MenuBar menuBar;
@FXML
public TableColumn<TruckInfo, CheckBox> inColumn;
@FXML
public TableColumn<TruckInfo, CheckBox> outColumn;
@FXML
public TableColumn<TruckInfo, String> truckNumColumn;
@FXML
public TableColumn<TruckInfo, Integer> yearColumn;
@FXML
public TableColumn<TruckInfo, String> palletNumColumn;
@FXML
public TableColumn<TruckInfo, String> trClassColumn;
@FXML
public TableColumn<TruckInfo, ComboBox<String>> msgColumn;
public void save(ActionEvent event) {
TruckInfo savedData = new TruckInfo();
ObservableList<TruckInfo> tableData = truckTable.getItems();
List <List<String>> arrList = new ArrayList<>();
for(int i = 0; i < truckTable.getItems().size(); i++ ) {
savedData = truckTable.getItems().get(i);
arrList.add(new ArrayList<>());
arrList.get(i).add(savedData.getTruck_id());
arrList.get(i).add(""+savedData.getYear());
arrList.get(i).add(""+savedData.getPallet_num());
arrList.get(i).add(""+savedData.getTr_class());
arrList.get(i).add(""+savedData.getCombo());
}
for (int i = 0; i < arrList.size(); i++) {
for (int j = 0; j < arrList.get(i).size(); j++) {
System.out.println(arrList.get(i).get(j));
}
}
String id = savedData.getTruck_id();
int year = savedData.getYear();
String pallet = savedData.getPallet_num();
String clas = savedData.getTr_class();
String msg = savedData.getCombo();
String idSave = savedData.getTruck_id();
System.out.println(msg);
System.out.println(idSave);
String sql = "UPDATE truck SET message = ? WHERE truck_id = '" + idSave + "'";
try {
Connection connection = new DBConnect().getConnection();
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, msg);
statement.execute();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
结果应该是用户在ComboBox列中输入一些文本,然后在退出应用程序之前进行保存,以便可以重新访问文本。
答案 0 :(得分:0)
我想出了一个答案,如果有人想知道的话。
public void save(ActionEvent event) {
TruckInfo savedData = new TruckInfo();
for(int i = 0; i < truckTable.getItems().size(); i++) {
savedData = truckTable.getItems().get(i);
String idSave = (String) truckNumColumn.getCellObservableValue(savedData).getValue();
ComboBox<String> msg = (ComboBox<String>) msgColumn.getCellObservableValue(savedData).getValue();
System.out.println(msg.getValue());
System.out.println(idSave);
String sql = "UPDATE truck SET message = ? WHERE truck_id = '" + idSave + "'";
try {
Connection connection = new DBConnect().getConnection();
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, msg.getValue());
statement.execute();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
答案在于getCellObservableValue()。getValue()