2012-12-31 34 views
0

正如標題所說,我需要更多的建議來正確實現在JavaFX 2.0中檢索和顯示數據到TableView。正確的方式來檢索mysql數據庫中的數據並將其顯示在JavaFX 2.0中的TableView中?

我見過這個相關的question及其引用使用DATAFX,但我找不到關於如何使用JDBC數據源實現它的教程。

請幫助我亟需它。注:我已經有工作代碼(硬編碼),但我仍然在尋找一種方便的方法來做到這一點。

在此先感謝。

+0

什麼是你的代碼的問題? – gontard

回答

0

這裏是一個工作示例:

public void start(Stage stage) throws ClassNotFoundException, SQLException { 
    Class.forName("com.mysql.jdbc.Driver"); 

    Scene scene =new Scene(new Group(),800,600); 

    JdbcDataSource dataSource=new JdbcDataSource("jdbc:mysql://localhost:3306/mybasename?zeroDateTimeBehavior=convertToNull&user=username&password=userpassword", "clients", "descr"); 
    TableView tableView=new TableView(); 
    tableView.setItems(dataSource.getData()); 
    tableView.getColumns().addAll(dataSource.getColumns()); 
    ((Group)scene.getRoot()).getChildren().add(tableView); 

    stage.setScene(scene); 
    stage.show(); 
} 
相關問題