JavaFX - 显示表列中数据库的所有数据

时间:2017-06-14 13:38:33

标签: java javafx tablerow

我的tablecolumn有问题。现在,数据只显示在1行,而数据库有更多行。例如,我必须制作一张表来检查艺术家制作哪些歌曲。有些艺术家制作了一首以上的歌曲,但桌子上只播放了一首歌。

package application;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;

public class Controller {
       protected Statement stmt = null;
       protected ResultSet rs = null;
       protected ResultSet rsLijst= null;

       protected Connection con = null;
       protected ObservableList<String> soort;
       protected ObservableList<String> naam;
       protected String value = "";
       protected int val = 0;
        public class PersonModel {

            String naam;
            String single;

            public PersonModel(String naam,String single) {
                this.naam = naam;
                this.single = single;
            }

            public String getNaam() {
                return naam;
            }
            public String getSingle() {
                return single;
            }
        }
        @FXML
        private TableView<PersonModel> tabel;
        @FXML
        private TableColumn<PersonModel, String> singleArtiest;
        @FXML
        private TableColumn<PersonModel, String> artiestenlel;
        @FXML
        private ComboBox<String> invoerartiest;
        @FXML
        private Button artiestenclick;
         @FXML
         void onClickZoeken(ActionEvent event) {
            tabel.getItems().clear();
            value = invoerartiest.getValue();
            val = invoerartiest.getSelectionModel().getSelectedIndex();

            tabel.getItems().addAll(new PersonModel(value, naam.get(val)));
            System.out.println(val);
        };
         @FXML
            void initialize() throws SQLException {
                String dbUsername = "root";
                String dbPassword = "";
                String dbURL = "jdbc:mysql://localhost:3306/hitdossier";
                try {
                    Connection conn = DriverManager.getConnection(dbURL, dbUsername, dbPassword);
                    soort = FXCollections.observableArrayList();
                    naam = FXCollections.observableArrayList();

                    // Execute query and store result in a resultset
                    ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM artiest");
                    ResultSet rsLijst = conn.createStatement().executeQuery("SELECT * FROM single");

                    while (rs.next()) {
                        //get string from db,whichever way
                        soort.add(rs.getString(2));
                    }
                    while (rsLijst.next()) {
                        //get string from db,whichever way
                        naam.add(rsLijst.getString(2));
                    }

                } catch (SQLException ex) {
                    System.err.println("Error"+ex);
                }

                artiestenlel.setCellValueFactory(new PropertyValueFactory<PersonModel, String>("naam"));
                singleArtiest.setCellValueFactory(new PropertyValueFactory<PersonModel, String>("single"));
                invoerartiest.getItems().addAll(soort);
                invoerartiest.getSelectionModel().selectFirst();

         }
}

代码取自:https://pastebin.com/Zg0eYW4P

0 个答案:

没有答案