|
我有一个包含机场的数据库表,每个机场都有一个名称和一个ID。
/ G6 o1 Q$ k+ S" j8 }' q1 |在JavaFX中,我有一个带有的表单,ComboBox组合框需要显示所有机场名称,并且在提交表单时需要将机场的ID插入数据库(而不是其名称)。
( \3 i9 J! @$ D" g但是我并没有真正弄清楚解决方案是什么。
, M0 ]& E+ V- I我有一个
' G# a, H# c4 l! n8 `( ]# g# tObservableList vliegveldenList = FXCollections.observableArrayList();8 S$ X# O7 V* b+ m$ ^1 t* j
ObservableList vliegveldenIDList = FXCollections.observableArrayList();6 K) p& j; F& f
数据库连接填充 ComboBox+ Q9 s3 v3 v! J: [" F+ z3 w
ResultSet rs = Project_Fasten_Your_Seatbelt.conn.createStatement()- w2 S5 f7 H1 W5 f6 ^+ u
.executeQuery("SELECT vliegveldnaam, vliegveld_id FROM fys_project.vliegvelden;");: H( U C5 u3 w( v: ~1 H
while (rs.next()) {
% S5 r: {1 o. R1 o vliegveldenList.add(rs.getString(1));" C4 l! x9 U9 O( ^. J
vliegveldenIDList.add(rs.getString(2));
3 \6 F. i" l5 L2 J: ~+ j* ^}
) c1 G+ w2 @- x: |: z- k填充组合框:
9 y1 }7 C0 t+ yvliegveldHerkomst.setValue("Luchthaven ...");
8 i- w3 Z1 H9 Y8 N) F! }vliegveldHerkomst.setItems(vliegveldenList);0 n3 y( `& N5 `) K- |( }
并在按下按钮时将其添加到数据库中:% b8 t( r6 u) _1 j. v: e+ S
String registratieValue = registratieNmrTxt.getText();- K" z {& ~% L( Q: p
String vluchtValue = vluchtNrmTxt.getText();
% d8 `; ~4 V. CString vliegveldValue = (String) vliegveldHerkomst.getSelectionModel().getSelectedItem();4 J( W6 z) M' d) m1 `
String bestemmingValue = (String) vliegveldBestemming.getSelectionModel().getSelectedItem(); ' F: v4 p# Y1 P$ x6 _: P( k s
String gevondenValue = (String) vliegveldGevonden.getSelectionModel().getSelectedItem();% X" _% g' b$ ?8 q" x( }
LocalDate dGevondenValue = datumGevondenDate.getValue();4 T; A- u! ~, A0 `* |+ W$ Z y8 h
LocalDate dVluchtValue = datumVluchtDate.getValue();
9 J6 V. n- Q4 I6 |String gewichtValue = gewichtBagageTxt.getText();
) [1 o% x- I. s8 v) gString kleurenValue = (String) kleuren.getSelectionModel().getSelectedItem();
7 F3 s5 \2 F+ p6 S- yString kofferValue = (String) kofferMerken.getSelectionModel().getSelectedItem();- }6 }1 Q+ V" ^1 x; t
String opmerkingValue = opmerkingArea.getText();6 T* [8 X0 k8 H1 a+ i: ?
//Data gevonden bagage invoeren; F! ^6 J8 Y/ l4 I- V* T- x8 \
Project_Fasten_Your_Seatbelt.conn.createStatement().executeUpdate(/ z" E M p5 Y6 @9 ?0 G: ^
"INSERT INTO gevondenbagage "
8 U3 g p* d( B/ F* n+ v + "(registratienummer, datumgevonden, datumaangemeld, vliegveldherkomst, "
7 S" f+ G' I0 A& M% n% r + "vliegveldbestemming, vliegveldgevonden, vluchtnummer, vluchtdatum, gewicht, "* m7 D1 ~: k2 V! i" u! z
+ "kleur, merk, `speciale opmerkingen`, userid)"
R$ r" a, G% c6 l + "VALUES ('" + registratieValue + "','" + dGevondenValue + "','" + today.format(localDate) + "','"
W& ?' _' P$ D: O9 Q0 r- O + vliegveldValue + "','" + bestemmingValue + "','" + gevondenValue + "','"
$ h; M. Y: x1 }0 S: K + vluchtValue + "','" + dVluchtValue + "','" + gewichtValue + "','"
- q& ?) m+ k$ ]4 D0 u. ^) O + kleurenValue + "','" + kofferValue + "','" + opmerkingValue + "','"
?8 p9 `) c; `7 H + Project_Fasten_Your_Seatbelt.getUserId() + "')");9 m1 S* F+ ?; L0 G0 B, X- O: p
一切正常,但是我要为设置机场的ID,而不是机场的名称vliegveldValue。- [( A6 l; i3 C7 U6 ]* P! [
我该怎么做呢?
" D% P! w4 X, p* K3 _ % U! l, ^* {8 a; z
解决方案:
9 e) C" P e4 Q5 u9 n. \( L0 E 4 `% e% h& O! g/ x- g7 |
. ]/ f2 ~( R# N0 @. i6 q2 F$ h
7 V/ Y' x6 ]3 s2 R& P 您可以创建一个如AirPort带班ID和name成员以及ComboBox该显示这些对象:ComboBox。, m7 p1 o0 k. l a( q" e( r7 M
AirPort 班级: T; w5 S# J8 b3 f, f
public class AirPort {
f' T( m! F" o& Y) ]9 l1 v private int ID;
" X* N ~/ s0 @0 a. o+ [" o! x) T private String name;
- _' U5 |& D; y8 s0 T E% _6 G public AirPort(int id, String name) {* I A- ^$ z ?5 U
this.ID = id;. ?1 b' {1 R9 b8 Z/ \- F( d
this.name = name;
# N! M2 K' n0 f+ j8 z" w }' @( o4 Z2 S- h
public int getID() { return ID; }/ b) \; c8 U' ?8 ]: }8 a) X, ~
public String getName() { return name; }
& f& a( K1 [( T- }+ v}1 |' [. G) H% i/ J/ a8 b
从数据库中获取项目并创建ComboBox:1 I9 e% g9 E3 e' n! C( ?3 I1 u
// Fill the list from the DataBase/ C# G; Q6 ~! b9 F. {
ObservableList airports = FXCollections.observableArrayList();
! \0 u) X; C; X, q- oairports.addAll(new AirPort(0, "Heathrow"),
1 ^* F$ G% ~( j new AirPort(1, "Frankfurt"),
* @/ u: n1 `) f+ m$ o! ~ new AirPort(2, "NewYork"));( I5 U) }+ ^ z5 S7 I% q; L
ComboBox combo = new ComboBox();7 A p) N8 M8 o* E% @
combo.setItems(airports);
5 q/ b7 F+ y0 C6 s! h% U J最后要显示对象的名称,您可以使用例如StringConverter:! r% H8 v4 d$ Z( c( |& I
combo.setConverter(new StringConverter() {, m p4 U' b1 e5 b" X
@Override% R( \: l0 j9 J3 f0 i7 L1 Q
public String toString(AirPort object) {
! l: C, ^$ z r# D" ~ return object.getName();
/ E+ p2 [/ z; o- x }) x- O6 e/ T) j- U
@Override) S% A% w% K) G. w( l- ~. |
public AirPort fromString(String string) {3 ~) F, k8 V' x2 f# P
return combo.getItems().stream().filter(ap ->
, N9 B1 G7 Z% O; D* R ap.getName().equals(string)).findFirst().orElse(null);
3 c/ ]# r; B! h& J# f& K1 F* L0 `9 m }0 n/ P9 h3 H! |* [. F! C; ]3 M, m
});8 e/ M" v. z* L$ D7 h! O
然后,当值更改时,您将获得AirPort包含所需ID的对象:6 ^8 W0 e2 W1 ~+ }6 Z
combo.valueProperty().addListener((obs, oldval, newval) -> {
# _, q& t- v6 b1 u9 f6 R if(newval != null)6 e, [1 g* d* ?. E5 N$ J% l. u
System.out.println("Selected airport: " + newval.getName() 4 Y0 t0 X! N \- u
+ ". ID: " + newval.getID());
8 d6 S9 }) F' Y+ V* x$ s1 n: s; |}); |
|