2013-08-28 8 views
15

Zaczynam się uczyć javaFX i muszę wypełnić tabelę danymi z mojej bazy danych. Czytałem dużo kodu online, ale nie znalazłem tego, czego szukałem. Czytam this, ale nie wiem, jak zaimplementować tę ostatnią funkcję. Czytałem jakiś inny kod, aby to zrobić i jak dotąd jest to część mojego kodu:Wypełnij tabelę przy użyciu bazy danych w JavaFX

@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); 
     } 
} 

mam nadzieję, że może mi pomóc

+0

Oto [przykład] (https://gist.github.com/jewelsea/4957967), który [pobiera nazwy z bazy danych do listy] (http://stackoverflow.com/questions/14878788/javafx- zapytanie o wątek-do-sql). Koncepcja jest podobna do zapełniania TableView z bazy danych. Poradnik [TableView samouczek] (http://docs.oracle.com/javafx/2/ui_controls/table-view.htm) może pomóc, jeśli jeszcze go nie zbadałeś. – jewelsea

+0

możliwe duplikaty [Wyświetl elementy z bazy danych w JavaFX TableView] (http://stackoverflow.com/questions/16228502/display-items-froma-a-database-in-a-javafx-tableview) – jewelsea

Odpowiedz

21

Jestem pewien, że to pomoże Ci:

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

W klasie kontrolera wykonaj następujące czynności:

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

i utworzyć oddzielny plik Pojo java dla każdego podmiotu (tabela) chce Mani pulate za pomocą 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(); 
    } 
} 

Testowałem ten program, który działa idealnie.

+2

Tylko notatkę tutaj nie jest to zalecany sposób tworzenia klasy modelu "Usermaster"; posiadanie właściwości 'public' jest generalnie złym pomysłem. Powinieneś uczynić właściwości 'private' i zapewnić metody dostępu do właściwości, wraz z metodami' get' i 'set', jak w [przykładach w samouczku] (http://docs.oracle.com/javase/8/javafx /properties-binding-tutorial/binding.htm#JFXBD107) –

Powiązane problemy