1

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");

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

3 Answers 3

3

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);
Sign up to request clarification or add additional context in comments.

3 Comments

can you explain bit more?I am new to javafx how to get list from database into arraylist?
That question relates to your database, not to javafx. What database are you using?
I am using mysql database
0

This is the code used to populate data intoto a ListView(JavaFX (fxml)).

 private void populateListView {
            try {
                String query = "select * from main_table";
                PreparedStatement prestate = model.getConnection().prepareStatement(query);
                ResultSet result = prestate.executeQuery();

                while (result.next()) {
                    String current = result.getString("title");
                    ObservableList<String> list = FXCollections.observableArrayList(current);
                    listview.getItems().addAll(list);
                }

                prestate.close();
                result.close();     
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

Comments

0

Thank you. The answer mentioned in this post is working in my case too. I am using postgres as database.

creating a table view

@FXML private TableView<AccountMaster> detailstable;

I am getting the details from DB and storing it in a generic list.

List<Student> list = DatabaseClient.getInstance().studentDetailsSearch(id,name);

creating observable list of generic type and then setting the list values in observable list.

ObservableList<AccountMaster> searchDetailsList = FXCollections.observableArrayList(list );

setting it in table view

detailstable.getItems().addAll(searchDetailsList); 

With this we can populate values from database into ObservableList in javafx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.