将数据库中的值填充到javafx中的ObservableList中 [英] populate values from database into ObservableList in javafx

查看:966
本文介绍了将数据库中的值填充到javafx中的ObservableList中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何正确填充从mysql数据库的值到javafx中的observableList?我使用:

How can I properly populate values from a mysql database to an observableList in javafx? I am using:

ObservableList< String> deptList = FXCollections.observableArrayList(CSE,ME);

手动填充值到observablelist,但我不知道从mysql数据库获取数据并填充到可观察列表中。

to manually populate values to observablelist,but I have no idea how to fetch data from a mysql database and populate into observable list.

推荐答案

一个数据库连接到你的mysql数据库并执行你的查询。 (我不会为你写这个,因为有很多良好 示例 < a href =http://alvinalexander.com/java/java-mysql-select-query-example.com/java/java-mysql-select-query-example =nofollow>在线。 )一旦你有你的ResultSet:

In order to get a List from your mysql database, open a database connection to your mysql database and execute your query. (I'm not going to write this one out for you, because there's plenty of good examples online.) Once you have your ResultSet:

List<String> listOfSomething;
ResultSet rs = statement.executeQuery(query);
while (rs.next()) {
    String current = rs.getString("whatever");
    listOfSomething.add(current);
}



您可以从数据库中获取列表并将其转换为可观察列表:

You can just get a list from your database and convert it to an observable list:

List listOfSomething = new ArrayList(ofWhatever);
ObservableList<String> observableListOfSomething = FXCollections.observableArrayList(listOfSomething);

这篇关于将数据库中的值填充到javafx中的ObservableList中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆