Advertisement
thatcrackertho

BookController.java

Apr 20th, 2019
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.44 KB | None | 0 0
  1. package sample;
  2.  
  3. import javafx.beans.property.SimpleIntegerProperty;
  4. import javafx.beans.property.SimpleStringProperty;
  5. import javafx.collections.FXCollections;
  6. import javafx.collections.ObservableList;
  7. import javafx.event.ActionEvent;
  8. import javafx.fxml.FXML;
  9. import javafx.fxml.Initializable;
  10. import javafx.scene.control.TableColumn;
  11. import javafx.scene.control.TableView;
  12. import javafx.scene.control.cell.PropertyValueFactory;
  13. import org.w3c.dom.events.MouseEvent;
  14.  
  15. import java.io.IOException;
  16. import java.net.URL;
  17. import java.sql.Connection;
  18. import java.sql.PreparedStatement;
  19. import java.sql.ResultSet;
  20. import java.util.ResourceBundle;
  21.  
  22. import static sample.Main.getConnection;
  23.  
  24.  
  25. public class BookScreenController implements Initializable{
  26.  
  27.     public static Connection conn;
  28.     public static ResultSet rs;
  29.     private static ObservableList<Book> bookList;
  30.  
  31.  
  32.     @FXML
  33.     public static TableView<Book> bookTable = new TableView<>();
  34.     public static TableColumn<Book, String> title = new TableColumn<>();
  35.     public static TableColumn<Book, String> authorFirstName = new TableColumn<>();
  36.     public static TableColumn<Book, String> authorLastName = new TableColumn<>();
  37.     public static TableColumn<Book, Integer> isbn = new TableColumn<>();
  38.     public static TableColumn<Book, Integer> numberOfPages = new TableColumn<>();
  39.  
  40.     @FXML
  41.     private void handleDisplayTables(ActionEvent event) throws NullPointerException {
  42.         try {
  43.             conn = getConnection();
  44.             PreparedStatement statement = conn.prepareStatement("SELECT * FROM BOOKS");
  45.             rs = statement.executeQuery();
  46.             bookList = FXCollections.observableArrayList();
  47.  
  48.             while (rs.next()) {
  49.                 String title = rs.getString("title");
  50.                 String authorFirstName = rs.getString("authorFirstName");
  51.                 String authorLastName = rs.getString("authorLastName");
  52.                 int isbn = rs.getInt("isbn");
  53.                 int numberOfPages = rs.getInt("numberOfPages");
  54.  
  55.                 bookList.add(new Book(title, authorFirstName, authorLastName,isbn, numberOfPages));
  56.  
  57.                 System.out.println(title + " " + authorFirstName + " " + authorLastName + " " + isbn + " " + numberOfPages);
  58.  
  59.  
  60.  
  61.  
  62.             }
  63.         } catch (Exception e) {
  64.             System.out.println(e);
  65.         }
  66.  
  67. //        title.setCellValueFactory(new PropertyValueFactory<>("title"));
  68. //        isbn.setCellValueFactory(new PropertyValueFactory<>("isbn"));
  69. //        authorFirstName.setCellValueFactory(new PropertyValueFactory<>("authorFirstName"));
  70. //        authorLastName.setCellValueFactory(new PropertyValueFactory<>("authorLastName"));
  71. //        numberOfPages.setCellValueFactory(new PropertyValueFactory<>("numberOfPages"));
  72.  
  73. //        title.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getTitle()));
  74. //        authorFirstName.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getAuthorFirstName()));
  75. //        authorLastName.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getAuthorLastName()));
  76. //        isbn.setCellValueFactory(cellData -> new SimpleIntegerProperty(cellData.getValue().getIsbn()).asObject());
  77. //        numberOfPages.setCellValueFactory(cellData -> new SimpleIntegerProperty(cellData.getValue().getNumberOfPages()).asObject());
  78.  
  79.         title.setCellValueFactory(cellData -> { return (new SimpleStringProperty(((Book)cellData.getValue()).getTitle()));});
  80.         authorFirstName.setCellValueFactory(cellData -> { return (new SimpleStringProperty(((Book)cellData.getValue()).getAuthorFirstName()));});
  81.         authorLastName.setCellValueFactory(cellData -> { return (new SimpleStringProperty(((Book)cellData.getValue()).getAuthorLastName()));});
  82.         isbn.setCellValueFactory(cellData -> { return (new SimpleIntegerProperty(((Book)cellData.getValue()).getIsbn())).asObject();});
  83.         numberOfPages.setCellValueFactory(cellData -> { return (new SimpleIntegerProperty(((Book)cellData.getValue()).getNumberOfPages())).asObject();});
  84.         bookTable.setItems(bookList);
  85.         bookTable.getColumns().addAll(title,authorFirstName,authorLastName,isbn,numberOfPages);
  86.         bookTable.refresh();
  87.     }
  88.  
  89.     public void closeApplication(ActionEvent event) throws Exception{
  90.         System.exit(0);
  91.     }
  92.  
  93.  
  94.     @Override
  95.     public void initialize(URL url, ResourceBundle resourceBundle) {
  96.  
  97.  
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement