Advertisement
Bollo_Omar

Controller class

Apr 27th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.04 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package tabledemo;
  6.  
  7. import java.net.URL;
  8. import java.sql.Connection;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.sql.Statement;
  12. import java.util.ResourceBundle;
  13. import javafx.collections.FXCollections;
  14. import javafx.collections.ObservableList;
  15. import javafx.event.ActionEvent;
  16. import javafx.fxml.FXML;
  17. import javafx.fxml.Initializable;
  18. import javafx.scene.control.Label;
  19. import javafx.scene.control.TableColumn;
  20. import javafx.scene.control.TableView;
  21. import javafx.scene.control.cell.PropertyValueFactory;
  22.  
  23.  
  24. /**
  25.  *
  26.  * @author User
  27.  */
  28. public class SampleController implements Initializable {
  29.    
  30.     @FXML public TableView displayDepartments;
  31.     @FXML public TableColumn deptNameCol;
  32.     @FXML public TableColumn cityCol;
  33.     @FXML public TableColumn townCol;
  34.     @FXML public TableColumn phoneCol;
  35.     @FXML public TableColumn addressCol;
  36.     @FXML public TableColumn emailCol;
  37.     public ObservableList data;
  38.     @Override
  39.     public void initialize(URL url, ResourceBundle rb) {
  40.        
  41.         deptNameCol.setCellValueFactory(new PropertyValueFactory<Department, Department>("deptN"));
  42.         cityCol.setCellValueFactory(new PropertyValueFactory<Department, String>("cityN"));
  43.         townCol.setCellValueFactory(new PropertyValueFactory<Department, String>("townN"));
  44.         phoneCol.setCellValueFactory(new PropertyValueFactory<Department, String>("townN"));
  45.         addressCol.setCellValueFactory(new PropertyValueFactory<Department, String>("addressN"));
  46.         emailCol.setCellValueFactory(new PropertyValueFactory<Department, String>("emailN"));
  47.        
  48.         populateDepartmentTable();        
  49.     }
  50.     @SuppressWarnings("unchecked")
  51.     public void populateDepartmentTable(){
  52.        
  53.         String SQL = "select deptName, city, town, phone, address, email from departments where town='westlands'";
  54.         data = FXCollections.observableArrayList();
  55.        
  56.         DBConnections db = new DBConnections();
  57.         Connection conn;
  58.         int k =0;
  59.         try{
  60.        
  61.             conn = db.getConnection();
  62.             Statement stmt = conn.createStatement();
  63.             ResultSet rs = stmt.executeQuery(SQL);
  64.            // System.out.print(SQL);
  65.             int sum=0;
  66.             while(rs.next()){
  67.                  
  68.             ObservableList<String> row = FXCollections.observableArrayList();
  69.            
  70.             for(int i=1; i<rs.getMetaData().getColumnCount()+1; i++){
  71.                
  72.                 row.add(rs.getString(i));              
  73.             }
  74.              System.out.println("row["+(k += 1)+"]"+row);
  75.            data.add(row);
  76.              
  77.             }
  78.             //displayDepartments.setItems(data);
  79.             displayDepartments.getItems().addAll(data);
  80.            
  81.            System.out.println(data);
  82.            
  83.            
  84.         }
  85.        
  86.         catch(Exception ex){
  87.            
  88.         }
  89.        
  90.        
  91.     }
  92.    
  93.    
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement