我有一个机场数据库表,每个机场都有一个名称和一个ID.
在JavaFX中,我有一个表单,其中ComboBox
,组合框需要显示所有机场名称,并且在提交表单时,需要将机场的ID插入数据库(而不是其名称).
但我真的不知道解决方案是什么.
我有一个
ObservableList vliegveldenList = FXCollections.observableArrayList(); ObservableList vliegveldenIDList = FXCollections.observableArrayList();
填充数据库连接 ComboBox
ResultSet rs = Project_Fasten_Your_Seatbelt.conn.createStatement() .executeQuery("SELECT vliegveldnaam, vliegveld_id FROM fys_project.vliegvelden;"); while (rs.next()) { vliegveldenList.add(rs.getString(1)); vliegveldenIDList.add(rs.getString(2)); }
填充组合框:
vliegveldHerkomst.setValue("Luchthaven ..."); vliegveldHerkomst.setItems(vliegveldenList);
按下按钮时会将其添加到数据库中:
String registratieValue = registratieNmrTxt.getText(); String vluchtValue = vluchtNrmTxt.getText(); String vliegveldValue = (String) vliegveldHerkomst.getSelectionModel().getSelectedItem(); String bestemmingValue = (String) vliegveldBestemming.getSelectionModel().getSelectedItem(); String gevondenValue = (String) vliegveldGevonden.getSelectionModel().getSelectedItem(); LocalDate dGevondenValue = datumGevondenDate.getValue(); LocalDate dVluchtValue = datumVluchtDate.getValue(); String gewichtValue = gewichtBagageTxt.getText(); String kleurenValue = (String) kleuren.getSelectionModel().getSelectedItem(); String kofferValue = (String) kofferMerken.getSelectionModel().getSelectedItem(); String opmerkingValue = opmerkingArea.getText(); //Data gevonden bagage invoeren Project_Fasten_Your_Seatbelt.conn.createStatement().executeUpdate( "INSERT INTO gevondenbagage " + "(registratienummer, datumgevonden, datumaangemeld, vliegveldherkomst, " + "vliegveldbestemming, vliegveldgevonden, vluchtnummer, vluchtdatum, gewicht, " + "kleur, merk, `speciale opmerkingen`, userid)" + "VALUES ('" + registratieValue + "','" + dGevondenValue + "','" + today.format(localDate) + "','" + vliegveldValue + "','" + bestemmingValue + "','" + gevondenValue + "','" + vluchtValue + "','" + dVluchtValue + "','" + gewichtValue + "','" + kleurenValue + "','" + kofferValue + "','" + opmerkingValue + "','" + Project_Fasten_Your_Seatbelt.getUserId() + "')");
这一切都运行正常,但我想为机场的名称设置机场的名称,而不是机场的名称vliegveldValue
.
我该怎么做呢?
您可以AirPort
使用ID
和name
成员创建一个类,并ComboBox
显示这些对象:ComboBox
.
AirPort
类:
public class AirPort { private int ID; private String name; public AirPort(int id, String name) { this.ID = id; this.name = name; } public int getID() { return ID; } public String getName() { return name; } }
从数据库中获取项目并创建ComboBox
:
// Fill the list from the DataBase ObservableListairports = FXCollections.observableArrayList(); airports.addAll(new AirPort(0, "Heathrow"), new AirPort(1, "Frankfurt"), new AirPort(2, "NewYork")); ComboBox combo = new ComboBox<>(); combo.setItems(airports);
最后显示您可以使用的对象的名称,例如StringConverter
:
combo.setConverter(new StringConverter() { @Override public String toString(AirPort object) { return object.getName(); } @Override public AirPort fromString(String string) { return combo.getItems().stream().filter(ap -> ap.getName().equals(string)).findFirst().orElse(null); } });
然后当值发生变化时,您将获得AirPort
包含所需ID的对象:
combo.valueProperty().addListener((obs, oldval, newval) -> { if(newval != null) System.out.println("Selected airport: " + newval.getName() + ". ID: " + newval.getID()); });