2013-08-28 62 views
15

我開始學習javaFX,我需要用數據庫中的數據填充表格。我在網上閱讀了很多代碼,但是我還沒有找到我想要的東西。我讀this,但我不知道如何實現最後一個功能。我看了一些其他的代碼來做到這一點,到目前爲止,這是我的一些代碼:在JavaFX中使用數據庫填充tableview

@FXML private TableView<User> table; 
@FXML private TableColumn<User, String> nameCol; 
@FXML private TableColumn<User, String> emailCol; 
private ObservableList<User> data; 

public void initialize(URL location, ResourceBundle resources) { 
    nameCol.setCellValueFactory(new PropertyValueFactory(「name」)); 
    emailCol.setCellValueFactory(new PropertyValueFactory(「email」)); 
    buildData(); 
} 
public void buildData() { 
     Connection connect = new Connection(); 
     Statement st = connect.Connect(); 
     data = FXCollections.observableArrayList(); 
     try { 
      ResultSet rs = st.executeQuery("SELECT * FROM USER"); 
      while (rs.next()) { 
       ObservableList<User> row = FXCollections.observableArrayList(); 
       for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) { 
        row.add(rs.getString(i)); 
        System.out.println(row); 
       } 
       data.add(pol); 
      } 
      tabla.setItems(data);   
     } catch (SQLException ex) { 
      JOptionPane.showMessageDialog(null, ex); 
     } 
} 

我希望你能幫助我

+0

這裏是一個[樣品](https://gist.github.com/jewelsea/4957967),其[從數據庫獲取名稱轉換成一個ListView](http://stackoverflow.com/questions/14878788/javafx-後臺線程的-SQL查詢)。這個概念與從數據庫填充TableView類似。如果你尚未研究它,[TableView教程](http://docs.oracle.com/javafx/2/ui_controls/table-view.htm)可能會有幫助。 [從一個JavaFX TableView中顯示數據庫中的項目]的 – jewelsea

+0

可能重複(http://stackoverflow.com/questions/16228502/display-items-from-a-database-in-a-javafx-tableview) – jewelsea

回答

21

我敢肯定,這將幫助您:

public class DBClass{  
    public Connection getConnection() throws ClassNotFoundException, SQLException{  
      Class.forName("com.mysql.jdbc.Driver"); 
      return DriverManager.getConnection("jdbc:mysql://192.168.0.1:3306/dbname","mysqluser","mysqluserpwd"); 
    } 
} 

在控制器類執行以下操作:

@FXML 
void initialize(){ 
    assert tableview != null : "fx:id=\"tableview\" was not injected: check your FXML file 'UserMaster.fxml'."; 
    colUserName.setCellValueFactory(
     new PropertyValueFactory<Usermaster,String>("userName"));   
    colPassword.setCellValueFactory(    
     new PropertyValueFactory<Usermaster,String>("userPassword")); 
    colUserType.setCellValueFactory(
     new PropertyValueFactory<Usermaster,String>("userType"));   
    colPhoto.setCellValueFactory(
     new PropertyValueFactory<Object,ImageView>("userPhoto")); 
    objDbClass = new DBClass(); 
    try{ 
     con = objDbClass.getConnection(); 
     buildData(); 
    } 
    catch(ClassNotFoundException ce){ 
     logger.info(ce.toString()); 
    } 
    catch(SQLException ce){ 
     logger.info(ce.toString()); 
    } 
} 

private ObservableList<Usermaster> data; 

public void buildData(){   
    data = FXCollections.observableArrayList(); 
    try{  
     String SQL = "Select * from usermaster Order By UserName";    
     ResultSet rs = con.createStatement().executeQuery(SQL); 
     while(rs.next()){ 
      Usermaster cm = new Usermaster(); 
      cm.userId.set(rs.getInt("UserId"));      
      Image img = new Image("tailoring/UserPhoto/User"+cm.getUserId().toString()+".jpg");     

      ImageView mv = new ImageView(); 
      mv.setImage(img); 
      mv.setFitWidth(70); 
      mv.setFitHeight(80); 
      cm.userPhoto.set(mv); 
      cm.userName.set(rs.getString("UserName")); 
      cm.userPassword.set(rs.getString("UserPassword")); 
      cm.userType.set(rs.getString("UserType")); 
      data.add(cm);     
     } 
     tableview.setItems(data); 
    } 
    catch(Exception e){ 
      e.printStackTrace(); 
      System.out.println("Error on Building Data");    
    } 
} 

,並創建一個POJO爲每個實體(表)一個單獨的Java文件想嘛呢使用TableView進行調度

public class Usermaster{  

    public SimpleIntegerProperty userId = new SimpleIntegerProperty(); 
    public ObjectProperty userPhoto = new SimpleObjectProperty(); 
    public SimpleStringProperty userName = new SimpleStringProperty(); 
    public SimpleStringProperty userPassword = new SimpleStringProperty(); 
    public SimpleStringProperty userType = new SimpleStringProperty(); 
    public SimpleStringProperty encPass = new SimpleStringProperty(); 
    public SimpleStringProperty updt = new SimpleStringProperty(); 
    public SimpleStringProperty uptm = new SimpleStringProperty(); 

    public Integer getUserId() { 
     return userId.get(); 
    } 

    public Object getUserPhoto() { 
     return userPhoto.get(); 
    } 

    public String getUserName() { 
     return userName.get(); 
    } 

    public String getUserPassword() { 
     return userPassword.get(); 
    } 

    public String getUserType() { 
     return userType.get(); 
    } 

    public String getEncPass() { 
     return encPass.get(); 
    } 

    public String getUpdt() { 
     return updt.get(); 
    } 

    public String getUptm() { 
     return uptm.get(); 
    } 
} 

我測試過這個程序,它的工作完美。

+2

剛一說明在這裏,這不是推薦的方式來創建模型類'Usermaster';擁有「公共」屬性通常是一個非常糟糕的主意。您應該使屬性爲'private',並提供屬性訪問器方法以及'get'和'set'方法,如[教程中的示例](http://docs.oracle.com/javase/8/javafx /properties-binding-tutorial/binding.htm#JFXBD107) –