这是我的代码:
for (int i=1; i<= columnCount; i++) {
HBox h = new HBox();
Label column = new Label(rsmd.getColumnName(i));
TextField entry = new TextField();
h.getChildren().addAll(column, entry);
dialogVbox.getChildren().add(h);
}
我需要在for循环中运行它,因为创建的行数取决于从数据库表中检索到的列。此后,当用户单击按钮时,我需要检索他们在“输入”文本字段中输入的新值。有什么办法吗?我假设我可能必须在循环外创建一些动态列表并填充它,但是我不确定该怎么做。任何帮助将不胜感激!
答案 0 :(得分:-1)
为什么不使用直接在相应TextField
的编辑操作中填充的地图:
Map<String, String> newValues = new HashMap<>();
for (int i=1; i<= columnCount; i++) {
final String columnName = rsmd.getColumnName(i);
HBox h = new HBox();
Label column = new Label(columnName);
TextField entry = new TextField();
entry.textProperty().addListener((e,o,n) -> newValues.put(columnName, n));
h.getChildren().addAll(column, entry);
dialogVbox.getChildren().add(h);
}
然后插入数据库
String columns = newValues.entrySet().stream().map( n -> n.getKey() ).collect( Collectors.joining( "," ) );
String values = newValues.entrySet().stream().map( n -> n.getValue() ).collect( Collectors.joining( "," ) );
String sql = "insert into table ( " + columns + ") values (" + values + ")";
// run sql