努力用SQLite数据填充JavaFXTableView

努力用SQLite数据填充JavaFXTableView,sqlite,javafx,tableview,Sqlite,Javafx,Tableview,我试图将数据从SQLite数据库获取到JavaFX TableView对象中,并且已经获得了所添加的行,但是实际数据没有显示出来。我到处寻找,试图了解如何做到这一点,但我对Java相对较新。下面是代码,如果有人可以看一下并提供帮助。不可否认,代码有点混乱,因为我已经尝试了各种方法让它工作。先谢谢你 import java.net.URL; import java.util.ResourceBundle; import javafx.event.ActionEvent; import javafx

我试图将数据从SQLite数据库获取到JavaFX TableView对象中,并且已经获得了所添加的行,但是实际数据没有显示出来。我到处寻找,试图了解如何做到这一点,但我对Java相对较新。下面是代码,如果有人可以看一下并提供帮助。不可否认,代码有点混乱,因为我已经尝试了各种方法让它工作。先谢谢你

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.stage.Stage;
import javafx.event.EventHandler;
import javafx.stage.WindowEvent;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TextField;
import javafx.scene.control.TitledPane;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.control.TableView;
import javafx.collections.ObservableList;
import javafx.collections.FXCollections;

public class MasterController {
    Stage stage = new Stage();
    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private TitledPane titledPane;

    @FXML
    private BorderPane borderPane;

    @FXML
    private TableView tableViewId;

    @FXML
    private MenuBar mainMenuId;

    @FXML
    private MenuItem closeId;

    @FXML
    private MenuItem deleteId;

    @FXML
    private MenuItem helpId;

    @FXML
    private Label searchLabelId;

    @FXML
    private TextField searchId;

    @FXML
    private Button goId;

    @FXML
    private TableColumn<Music, String> artistColId;

    @FXML
    private TableColumn<Music, String> titleColId;

    @FXML
    private TableColumn<Music, String> albumColId;

    @FXML
    private TableColumn<Music, String> genreColId;

    @FXML
    void closeClicked(ActionEvent event) {

    }

    @FXML
    void deleteClicked(ActionEvent event) {

    }

    @FXML
    void goClicked(MouseEvent event) {

    }

    @FXML
    void helpClicked(ActionEvent event) {

    }

    @FXML
    void initialize() {
        assert titledPane != null : "fx:id=\"titledPane\" was not injected: check your FXML file 'Untitled'.";
        assert borderPane != null : "fx:id=\"borderPane\" was not injected: check your FXML file 'Untitled'.";
        assert mainMenuId != null : "fx:id=\"mainMenuId\" was not injected: check your FXML file 'Untitled'.";
        assert closeId != null : "fx:id=\"closeId\" was not injected: check your FXML file 'Untitled'.";
        assert deleteId != null : "fx:id=\"deleteId\" was not injected: check your FXML file 'Untitled'.";
        assert helpId != null : "fx:id=\"helpId\" was not injected: check your FXML file 'Untitled'.";
        assert searchLabelId != null : "fx:id=\"searchLabelId\" was not injected: check your FXML file 'Untitled'.";
        assert searchId != null : "fx:id=\"searchId\" was not injected: check your FXML file 'Untitled'.";
        assert goId != null : "fx:id=\"goId\" was not injected: check your FXML file 'Untitled'.";
        assert tableViewId != null : "fx:id=\"tableViewId\" was not injected: check your FXML file 'Untitled'.";
        assert artistColId != null : "fx:id=\"artistColId\" was not injected: check your FXML file 'Untitled'.";
        assert titleColId != null : "fx:id=\"titleColId\" was not injected: check your FXML file 'Untitled'.";
        assert albumColId != null : "fx:id=\"albumColId\" was not injected: check your FXML file 'Untitled'.";
        assert genreColId != null : "fx:id=\"genreColId\" was not injected: check your FXML file 'Untitled'.";

    }

        public void prepareStageEvents(Stage stage)
    {
        System.out.println("Preparing stage events...");

        this.stage = stage;
        ObservableList<Music> targetData = FXCollections.observableArrayList();
        System.out.println(targetData);
        tableViewId.setItems(Music.showAll());
        stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
                public void handle(WindowEvent we) {
                    System.out.println("Close button was clicked!");
                    Application.terminate();
                }
            });
    }   
}
import java.net.URL;
导入java.util.ResourceBundle;
导入javafx.event.ActionEvent;
导入javafx.fxml.fxml;
导入javafx.stage.stage;
导入javafx.event.EventHandler;
导入javafx.stage.WindowEvent;
导入javafx.scene.control.Button;
导入javafx.scene.control.Label;
导入javafx.scene.control.MenuBar;
导入javafx.scene.control.MenuItem;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TextField;
导入javafx.scene.control.TitledPane;
导入javafx.scene.input.MouseEvent;
导入javafx.scene.layout.BorderPane;
导入javafx.scene.control.TableView;
导入javafx.collections.ObservableList;
导入javafx.collections.FXCollections;
公共类主控器{
阶段=新阶段();
@FXML
私人资源;
@FXML
私有URL位置;
@FXML
私有标题窗格标题窗格;
@FXML
私有边框窗格边框窗格;
@FXML
私有TableView tableViewId;
@FXML
私有菜单栏mainMenuId;
@FXML
私有菜单项closeId;
@FXML
私有菜单项deleteId;
@FXML
私有菜单项帮助ID;
@FXML
私有标签searchLabelId;
@FXML
私有文本字段搜索ID;
@FXML
私用钮扣;
@FXML
私人桌柱艺术家;
@FXML
私有表列标题实体;
@FXML
私有表列albumColId;
@FXML
私有表列genreclid;
@FXML
void closeClicked(ActionEvent事件){
}
@FXML
void deleteClicked(ActionEvent事件){
}
@FXML
void goClicked(MouseEvent事件){
}
@FXML
单击的无效帮助(ActionEvent事件){
}
@FXML
void initialize(){
assert titledPane!=null:“fx:id=\“titledPane\”未被注入:检查FXML文件“Untitled”;
assert borderPane!=null:“fx:id=\”未注入“borderPane\”:检查FXML文件“Untitled”;
assert mainMenuId!=null:“fx:id=\“mainMenuId\”未被注入:检查FXML文件“Untitled”;
assert closeId!=null:“fx:id=\“closeId\”未被注入:检查FXML文件“Untitled”;
assert deleteId!=null:“fx:id=\“deleteId\”未被注入:检查FXML文件“Untitled”;
assert helpId!=null:“fx:id=\“helpId\”未被注入:检查FXML文件“Untitled”;
assert searchLabelId!=null:“fx:id=\”未注入“searchLabelId\”:检查FXML文件“Untitled”;
assert searchId!=null:“fx:id=\”未注入“searchId\”:检查FXML文件“Untitled”;
assert goId!=null:“fx:id=\“goId\”未被注入:检查FXML文件“Untitled”;
assert tableViewId!=null:“fx:id=\“tableViewId\”未被注入:检查FXML文件“Untitled”;
assert artistColId!=null:“fx:id=\”未注入“artistColId\”:检查FXML文件“Untitled”;
assert titleColId!=null:“fx:id=\”未注入“titleColId\”:检查FXML文件“Untitled”;
assert-albumColId!=null:“fx:id=\“albumColId\”未被注入:检查FXML文件“Untitled”;
assert genreclid!=null:“fx:id=\“genreclid\”未被注入:检查FXML文件“Untitled”;
}
公共空间准备活动(阶段)
{
System.out.println(“准备阶段事件…”);
这个阶段=阶段;
ObservableList targetData=FXCollections.observableArrayList();
System.out.println(目标数据);
tableViewId.setItems(Music.showAll());
stage.setOnCloseRequest(新的EventHandler(){
公共无效句柄(WindowEvent we){
System.out.println(“单击关闭按钮!”);
Application.terminate();
}
});
}   
}
这是音乐课程代码

import java.util.List;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import javafx.scene.control.TableView;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

public class Music{
    public int trackId;
    public String title;
    public String artist;
    public String album;
    public String genre;

    public Music(int trackId, String title, String artist, String album, String genre){
        this.trackId = trackId;
        this.title = title;
        this.artist = artist;
        this.album = album;
        this.genre = genre;
    }

    @Override public String toString()
    {
        return trackId + "\t" + title + "\t" + artist + "\t" + album + "\t" + genre;
    }

    public static ObservableList<Music> showAll(){
        PreparedStatement statement = Application.database.newStatement("select tracks.TrackId, tracks.Title, artist.ArtistName, album.albumTitle, album.albumGenre from tracks inner join artist on tracks.artistId = artist.artistId left outer join album on tracks.albumId = album.albumId");
        ObservableList<Music> musicData = FXCollections.observableArrayList();
        if (statement != null)      // Assuming the statement correctly initated...
        {
            ResultSet results = Application.database.runQuery(statement);       // ...run the query!

            if (results != null)        // If some results are returned from the query...
            {
                try {                               // ...add each one to the list.
                    while (results.next()) {                                               
                        musicData.add(new Music(results.getInt("TrackId"), results.getString("Title"), results.getString("ArtistName"), results.getString("albumTitle"), results.getString("albumGenre")));
                    }
                }
                catch (SQLException resultsexception)       // Catch any error processing the results.
                {
                    System.out.println("Database result processing error: " + resultsexception.getMessage());
                }
            }
        }
        return musicData;
    }
}
import java.util.List;
导入java.sql.ResultSet;
导入java.sql.SQLException;
导入java.sql.PreparedStatement;
导入javafx.scene.control.TableView;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
公共音乐课{
公共int trackId;
公共字符串标题;
公共弦乐艺术家;
公共弦乐专辑;
公共弦乐体裁;
公共音乐(int trackId、字符串标题、字符串艺术家、字符串专辑、字符串流派){
this.trackId=trackId;
this.title=标题;
这个艺术家=艺术家;
this.album=相册;
这个。流派=流派;
}
@重写公共字符串toString()
{
返回trackId+“\t”+标题+“\t”+艺术家+“\t”+专辑+“\t”+流派;
}
公共静态可观察列表showAll(){
PreparedStatement语句=Application.database.newStatement(“从曲目中选择tracks.TrackId、tracks.Title、artist.ArtistName、album.albumTitle、album.albumGene内部加入曲目上的艺术家。artistId=artist.artistId左外部加入曲目上的相册。albumId=album.albumId”);
ObservableList musicData=FXCollections.observableArrayList();
if(statement!=null)//假设该语句已正确初始化。。。
{
ResultSet results=Application.database.runQuery(语句);/…运行查询!
if(results!=null)//如果查询返回了一些结果。。。
{
尝试{//…将每个添加到列表中。
while(results.next()){
马斯