Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.06 KB | None | 0 0
  1. package dbtotableview;
  2.  
  3. import java.net.URL;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9.  
  10. import java.util.ResourceBundle;
  11. import javafx.collections.FXCollections;
  12. import javafx.collections.ObservableList;
  13. import javafx.event.ActionEvent;
  14. import javafx.fxml.FXML;
  15. import javafx.fxml.Initializable;
  16. import javafx.scene.control.TableColumn;
  17. import javafx.scene.control.TableView;
  18. import javafx.scene.control.cell.PropertyValueFactory;
  19.  
  20.  
  21. public class FXMLDocumentController implements Initializable {
  22.    
  23.     @FXML private TableView<Product> tableView;
  24.     @FXML private TableColumn<Product, Integer> c1;
  25.     @FXML private TableColumn<Product, String> c2;
  26.     @FXML private TableColumn<Product, String> c3;
  27.    
  28.     @FXML
  29.     private void handleButtonAction(ActionEvent event) throws SQLException {}  
  30.    
  31.     @Override
  32.     public void initialize(URL location, ResourceBundle resources) {
  33.         c1.setCellValueFactory(new PropertyValueFactory<>("id"));
  34.         c2.setCellValueFactory(new PropertyValueFactory<>("cell1"));
  35.         c3.setCellValueFactory(new PropertyValueFactory<>("cell2"));
  36.  
  37.         tableView.setItems(parseUserList());
  38.     }    
  39.  
  40.     private ObservableList<Product> parseUserList(){
  41.         ObservableList<Product> products = FXCollections.observableArrayList();
  42.  
  43.         try {
  44.             Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/database1?serverTimezone=UTC","root","");
  45.             Statement stmt = conn.createStatement();
  46.             ResultSet rs = stmt.executeQuery("SELECT * FROM modules");
  47.            
  48.             while(rs.next()){
  49.                 int i = rs.getInt(1);
  50.                 String c = rs.getString(2);
  51.                 String cs = rs.getString(3);
  52.  
  53.                 products.add(new Product(i, c, cs));
  54.             }
  55.            
  56.         } catch (SQLException ex) {
  57.             System.out.println(ex.getMessage());
  58.         }
  59.        
  60.         return products;
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement