Advertisement
Guest User

Untitled

a guest
Apr 4th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.22 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.net.URL;
  3. import java.sql.Connection;
  4. import java.sql.SQLException;
  5. import java.util.ResourceBundle;
  6. import javafx.collections.FXCollections;
  7. import javafx.collections.ObservableList;
  8. import javafx.event.ActionEvent;
  9. import javafx.fxml.FXML;
  10. import javafx.fxml.FXMLLoader;
  11. import javafx.fxml.Initializable;
  12. import javafx.scene.Parent;
  13. import javafx.scene.Scene;
  14. import javafx.scene.control.Alert;
  15. import javafx.scene.control.Alert.AlertType;
  16. import javafx.scene.control.Button;
  17. import javafx.scene.control.ChoiceBox;
  18. import javafx.scene.control.TextField;
  19. import javafx.stage.Stage;
  20. import java.util.Optional;
  21. import javafx.scene.control.ButtonBar.ButtonData;
  22. import javafx.scene.control.ButtonType;
  23. import javafx.scene.control.TableColumn;
  24. import javafx.scene.control.TableView;
  25. import javafx.scene.control.cell.PropertyValueFactory;
  26. import java.sql.*;
  27. import java.sql.Connection;
  28. import java.sql.DriverManager;
  29. import java.sql.SQLException;
  30. import java.sql.ResultSet;
  31. import javafx.scene.layout.VBox;
  32.  
  33. public class MenuController implements Initializable {
  34.  
  35. Connection conn = null;
  36. ResultSet rs = null;
  37. PreparedStatement pst = null;
  38.  
  39. //Fxid's for my objects in the Menu.fxml and the the LibraryMember.fxml
  40. @FXML
  41. private Button addBookBtn;
  42. @FXML
  43. private Button listAllBooksBtn;
  44. @FXML
  45. private Button addLibraryMemberBtn;
  46. @FXML
  47. private Button listAllLibraryMembersBtn;
  48. @FXML
  49. private Button searchBtn;
  50. @FXML
  51. private Button borrowAndReturnBookBtn;
  52. @FXML
  53. private Button returnBookBtn;
  54. @FXML
  55. private Button closeBtn;
  56. @FXML
  57. private Button backToMenuBtn;
  58. @FXML
  59. private ChoiceBox<String> genderChoiceBox;
  60. @FXML
  61. private TextField searchMemberTextField;
  62. @FXML
  63. private TextField memberIDTextField;
  64. @FXML
  65. private TextField firstNameTextField;
  66. @FXML
  67. private TextField surnameTextField;
  68. @FXML
  69. private TextField ageTextField;
  70. @FXML
  71. private TextField contactNoTextField;
  72. @FXML
  73. private TextField addressTextField;
  74. @FXML
  75. private Button clearMemberTextFieldsButton;
  76. @FXML
  77. private Button addMemberButton;
  78. @FXML
  79. private Button updateMemberButton;
  80. @FXML
  81. private Button deleteMemberButton;
  82.  
  83. //This is what I am working with for the table
  84. @FXML
  85. private TableView<MemberTable> memberListTable;
  86. @FXML
  87. //private TableColumn <MemberTable, Integer> idMemberColumn;
  88. private TableColumn<MemberTable, String> idMemberColumn;
  89. @FXML
  90. private TableColumn<MemberTable, String> firstNameColumn;
  91. @FXML
  92. private TableColumn<MemberTable, String> surnameColumn;
  93. @FXML
  94. // private TableColumn <MemberTable, Integer> ageColumn;
  95. private TableColumn<MemberTable, String> ageColumn;
  96. @FXML
  97. private TableColumn<MemberTable, String> genderColumn;
  98. @FXML
  99. // private TableColumn <MemberTable, Integer> contactNoColumn;
  100. private TableColumn<MemberTable, String> contactNoColumn;
  101. @FXML
  102. private TableColumn<MemberTable, String> addressColumn;
  103.  
  104. //FXid's for Book.fxml
  105. @FXML
  106. private ChoiceBox<String> categoryChoiceBox;
  107. @FXML
  108. private TextField bookTitleTextField;
  109. @FXML
  110. private TextField authorTextField;
  111. @FXML
  112. private TextField publishedTextField;
  113. @FXML
  114. private ChoiceBox <Integer> bookCopiesChoiceBox;
  115. // private ChoiceBox<String> bookCopiesChoiceBox;
  116. @FXML
  117. private TextField dateAddedBookTextField;
  118. @FXML
  119. private TextField searchBooksTextField;
  120. @FXML
  121. private Button clearBookFieldsBtn;
  122. @FXML
  123. private Button addBookDetailsBtn;
  124. @FXML
  125. private Button updateBookBtn;
  126. @FXML
  127. private Button deleteBookBtn;
  128.  
  129. //FXid's for TableView for Books in Book.fxml
  130. // @FXML
  131. // private TableView<BookTable> bookListTable;
  132. @FXML
  133. private TableColumn <BookTable, Integer> bookID;
  134. // private TableColumn<BookTable, String> bookIDColumn;
  135. @FXML
  136. private TableColumn<BookTable, String> bookTitleColumn;
  137. @FXML
  138. private TableColumn<BookTable, String> bookAuthorColumn;
  139. @FXML
  140. // private TableColumn <BookTable, Integer> yearPublishedColumn;
  141. private TableColumn<BookTable, String> yearPublishedColumn;
  142. @FXML
  143. private TableColumn <BookTable, Number> bookCopiesColumn;
  144. // private TableColumn<BookTable, String> bookCopiesColumn;
  145. @FXML
  146. private TableColumn<BookTable, String> bookCategoryColumn;
  147. @FXML
  148. private TableColumn<BookTable, String> dateAddedBookColumn;
  149.  
  150. // FXid's for LoanBR.fxml
  151.  
  152. private int count = 1;
  153.  
  154. Stage LibraryApplication;
  155. Stage LibraryMember;
  156. Stage Book;
  157.  
  158. private ObservableList<BookTable> bookDetails;
  159.  
  160. @FXML
  161. private TableView<BookTable> bookListTable;
  162.  
  163. private final ObservableList<BookTable> data;
  164.  
  165. public MenuController() {
  166. this.data = FXCollections.observableArrayList(
  167. new BookTable(1, "Test1", "Author1", "2015", 1, "Fiction", "04-04-16"),
  168. new BookTable(1, "Test2", "Author2", "2015", 1, "Fiction", "04-04-16"),
  169. new BookTable(1, "Test3", "Author3", "2015", 1, "Fiction", "04-04-16"),
  170. new BookTable(1, "Test4", "Author4", "2015", 1, "Fiction", "04-04-16"),
  171. new BookTable(1, "Test5", "Author5", "2015", 1, "Fiction", "04-04-16")
  172. );
  173. }
  174.  
  175. @FXML
  176. private void handleBookButtonAction(ActionEvent event) throws IOException {
  177. Stage stage;
  178. Parent root;
  179. if (event.getSource() == addBookBtn) {
  180. stage = (Stage) addBookBtn.getScene().getWindow();
  181.  
  182. root = FXMLLoader.load(getClass().getResource("Book.fxml"));
  183. } else {
  184. stage = (Stage) backToMenuBtn.getScene().getWindow();
  185. root = FXMLLoader.load(getClass().getResource("Menu.fxml"));
  186. }
  187.  
  188. // bookID.setCellValueFactory(new PropertyValueFactory<BookTable, Integer>("bookID"));
  189. // bookTitleColumn.setCellValueFactory(new PropertyValueFactory<BookTable, String>("bookTitle"));
  190. // bookAuthorColumn.setCellValueFactory(new PropertyValueFactory<BookTable, String>("author"));
  191. // yearPublishedColumn.setCellValueFactory(new PropertyValueFactory<BookTable, String>("published"));
  192. // bookCopiesColumn.setCellValueFactory(new PropertyValueFactory<BookTable, Number>("bookCopies"));
  193. // bookCategoryColumn.setCellValueFactory(new PropertyValueFactory<BookTable, String>("category"));
  194. // dateAddedBookColumn.setCellValueFactory(new PropertyValueFactory<BookTable, String>("dateAddedBook"));
  195. //
  196. // bookListTable.setItems(data);
  197. // bookListTable.getColumns().addAll(bookID, bookTitleColumn, bookAuthorColumn, yearPublishedColumn, bookCopiesColumn, bookCategoryColumn, dateAddedBookColumn);
  198. //
  199. // final VBox vbox = new VBox();
  200. // vbox.getChildren().addAll(bookListTable);
  201. //
  202. // System.out.println("Book Database has been loaded");
  203.  
  204. Scene scene = new Scene(root);
  205. stage.setScene(scene);
  206. stage.show();
  207.  
  208. try{
  209.  
  210. //ResultSet
  211. Statement stmt = conn.createStatement();
  212. String sql = "SELECT bookid, booktitle, author, published, "
  213. + "bookcopies, category, dateadded FROM book";
  214.  
  215. ResultSet rs = stmt.executeQuery(sql);
  216.  
  217. // bookDetails.add(new BookTable(33, "bla",
  218. // "bla", "bla", 3,
  219. // "rrt", "4rsgsf"
  220. // ));
  221.  
  222. while(rs.next()){
  223.  
  224. }
  225.  
  226. rs.close();
  227.  
  228.  
  229. }catch(Exception e){
  230. e.printStackTrace();
  231. System.out.println("Error on Building Data");
  232.  
  233. }
  234. System.out.println("Book Database has been loaded");
  235.  
  236. }
  237.  
  238.  
  239. @FXML
  240. private void handleClearBookTextFieldsButtonAction(ActionEvent event) {
  241.  
  242. if (event.getSource() == clearBookFieldsBtn) {
  243. //memberIDTextField.setText(""); Also makes the text field empty
  244. clearBookTextFields();
  245. }
  246.  
  247. }
  248.  
  249. //Method to clear all the required text fields in Book.fxml
  250. private void clearBookTextFields() {
  251.  
  252. categoryChoiceBox.setValue("Non-Fiction");
  253. bookTitleTextField.clear();
  254. authorTextField.clear();
  255. publishedTextField.clear();
  256. // bookCopiesChoiceBox.setValue(1); This is not accepted since it has now become a String, temporarily
  257. bookCopiesChoiceBox.setValue(1);
  258. }
  259.  
  260. //Validate the textfields in Book.fxml (Still in progress, only checking to see if it is empty or not)
  261. private void bookDetailsValid() {
  262.  
  263. if (bookTitleTextField.getText().equals("") == true
  264. || authorTextField.getText().equals("")
  265. || publishedTextField.getText().equals("")
  266. || bookCopiesChoiceBox.getValue() == null
  267. || categoryChoiceBox.getValue().equals("")
  268. || dateAddedBookTextField.getText().equals("")) {
  269. Alert fail = new Alert(AlertType.INFORMATION);
  270. fail.setHeaderText("Error!");
  271. fail.setContentText("Missing or Incorrect Details! Please Try Again");
  272. fail.showAndWait();
  273. System.out.println("Missing or Incorrect Details! Please Try Again");
  274. } else {
  275.  
  276. bookDetails = bookListTable.getItems();
  277. bookDetails.add(new BookTable(count++, bookTitleTextField.getText(),
  278. authorTextField.getText(), publishedTextField.getText(), bookCopiesChoiceBox.getValue(),
  279. categoryChoiceBox.getValue(), dateAddedBookTextField.getText()
  280. ));
  281.  
  282. try{
  283. String SQL = "SELECT * from book";
  284. //ResultSet
  285. Statement stmt = conn.createStatement();
  286.  
  287. String booktitle = bookTitleTextField.getText();
  288. String author = authorTextField.getText();
  289. String published = publishedTextField.getText();
  290. int bookcopies = bookCopiesChoiceBox.getValue();
  291. String category = categoryChoiceBox.getValue();
  292. String dateaddedbook = dateAddedBookTextField.getText();
  293.  
  294.  
  295.  
  296.  
  297. String sql = "INSERT INTO book (booktitle, author, published, bookcopies,"
  298. + " category, dateadded) VALUES ('"+booktitle+"', '"+author+"', '"+published+"', '"+bookcopies+"', '"+category+"', '"+dateaddedbook+"')";
  299. stmt.executeUpdate(sql);
  300. }catch(Exception e){
  301. e.printStackTrace();
  302. System.out.println("Error on Building Data");
  303.  
  304. }
  305. System.out.println("Data has been Added");
  306.  
  307. Double.parseDouble(publishedTextField.getText());
  308. // Double.parseDouble(bookCopiesChoiceBox.getValue());
  309. clearBookTextFields();
  310. }
  311.  
  312. }
  313.  
  314. private void editBookDetailsValid() {
  315.  
  316. if (bookTitleTextField.getText().equals("") == true
  317. || authorTextField.getText().equals("")
  318. || publishedTextField.getText().equals("")
  319. || bookCopiesChoiceBox.getValue() == null
  320. || categoryChoiceBox.getValue().equals("")
  321. || dateAddedBookTextField.getText().equals("")) {
  322. Alert fail = new Alert(AlertType.INFORMATION);
  323. fail.setHeaderText("Error!");
  324. fail.setContentText("Missing or Incorrect Details! Please Try Again");
  325. fail.showAndWait();
  326. System.out.println("Missing or Incorrect Details! Please Try Again");
  327. } else {
  328.  
  329. bookDetails = bookListTable.getItems();
  330. int selectedIndex = bookListTable.getSelectionModel().getSelectedIndex();
  331. BookTable selectedBook = bookListTable.getSelectionModel().getSelectedItem();
  332.  
  333. bookDetails.add(new BookTable(count++, bookTitleTextField.getText(),
  334. authorTextField.getText(), publishedTextField.getText(), bookCopiesChoiceBox.getValue(),
  335. categoryChoiceBox.getValue(),
  336. dateAddedBookTextField.getText()
  337. ));
  338.  
  339. bookListTable.getItems().remove(selectedIndex);
  340.  
  341. Double.parseDouble(publishedTextField.getText());
  342. // Double.parseDouble(bookCopiesChoiceBox.getValue());
  343.  
  344. clearBookTextFields();
  345. }
  346.  
  347. }
  348.  
  349. //Handler Button for adding a member
  350. @FXML
  351. private void handleAddBookDetailsButtonAction(ActionEvent event) {
  352.  
  353. bookDetailsValid();
  354. }
  355.  
  356. @FXML
  357. private void handleEditBookDetailsButtonAction(ActionEvent event) {
  358.  
  359. editBookDetailsValid();
  360.  
  361. }
  362.  
  363. // The method below is used to allow the user to select a row of data
  364. // from the Member's Table. Clicking on the row of data, outputs the data
  365. // into their respective text fields that is used to add new library members.
  366. @FXML
  367. private void initialiseBookTable() {
  368.  
  369. bookID.setCellValueFactory(new PropertyValueFactory<BookTable, Integer>("bookID"));
  370. bookTitleColumn.setCellValueFactory(cellData -> cellData.getValue().bookTitleProperty());
  371. bookAuthorColumn.setCellValueFactory(cellData -> cellData.getValue().authorProperty());
  372. yearPublishedColumn.setCellValueFactory(cellData -> cellData.getValue().publishedProperty());
  373. bookCopiesColumn.setCellValueFactory(cellData -> cellData.getValue().bookCopiesProperty());
  374. bookCategoryColumn.setCellValueFactory(cellData -> cellData.getValue().categoryProperty());
  375. dateAddedBookColumn.setCellValueFactory(cellData -> cellData.getValue().dateAddedBookProperty());
  376.  
  377. // Clear person details.
  378. showBookDetails(null);
  379.  
  380. // Listen for selection changes and show the person details when changed.
  381. bookListTable.getSelectionModel().selectedItemProperty().addListener(
  382. (observable, oldValue, newValue) -> showBookDetails(newValue));
  383. }
  384.  
  385. private void showBookDetails(BookTable book) {
  386. if (book != null) {
  387. // Fill the labels with info from the person object.
  388. bookTitleTextField.setText(book.getBookTitle());
  389. authorTextField.setText(book.getAuthor());
  390. publishedTextField.setText(book.getPublished());
  391. // bookCopiesChoiceBox.setValue(book.getBookCopies());
  392. categoryChoiceBox.setValue(book.getCategory());
  393. dateAddedBookTextField.setText(book.getDateAddedBook());
  394. } else {
  395.  
  396. }
  397. }
  398.  
  399. /**
  400. * Called when the user clicks on the delete button.
  401. */
  402. @FXML
  403. private void handleDeleteBook() {
  404.  
  405. deleteBook();
  406.  
  407. }
  408.  
  409. private void deleteBook() {
  410.  
  411. int selectedIndex = bookListTable.getSelectionModel().getSelectedIndex();
  412. if (selectedIndex >= 0) {
  413. bookListTable.getItems().remove(selectedIndex);
  414. bookID.setCellValueFactory(new PropertyValueFactory<BookTable, Integer>("bookID"));
  415. } else {
  416. // Nothing selected.
  417. Alert alert = new Alert(AlertType.WARNING);
  418. alert.setTitle("No Selection");
  419. alert.setHeaderText("No Book Selected");
  420. alert.setContentText("Please select a Book from the table.");
  421.  
  422. alert.showAndWait();
  423. }
  424.  
  425. }
  426.  
  427. @Override
  428. public void initialize(URL url, ResourceBundle rb) {
  429. conn = DBClass.ConnecrDb();
  430. }
  431.  
  432. }
  433.  
  434. import java.sql.Connection;
  435. import java.sql.DriverManager;
  436. import java.sql.SQLException;
  437. import java.sql.ResultSet;
  438. import javafx.application.Application;
  439. import javafx.collections.ObservableList;
  440. import javafx.fxml.FXMLLoader;
  441. import javafx.scene.Parent;
  442. import javafx.scene.Scene;
  443. import javafx.stage.Stage;
  444.  
  445.  
  446. public class Menu extends Application {
  447.  
  448. public Menu() {
  449.  
  450.  
  451. }
  452.  
  453. @Override
  454. public void start(Stage stage) throws Exception {
  455.  
  456. //Code to open the main menu
  457. Parent root = FXMLLoader.load(getClass().getResource("Menu.fxml"));
  458.  
  459. Scene scene = new Scene(root);
  460.  
  461. stage.setScene(scene);
  462. stage.setMaximized(true);
  463. stage.show();
  464. }
  465.  
  466. /**
  467. * @param args the command line arguments
  468. */
  469. public static void main(String[] args) {
  470. launch(args);
  471. }
  472.  
  473. }
  474.  
  475. import javafx.beans.property.SimpleStringProperty;
  476. import javafx.beans.property.SimpleIntegerProperty;
  477. import javafx.beans.property.StringProperty;
  478. import javafx.beans.property.IntegerProperty;
  479.  
  480.  
  481. public class BookTable {
  482. private final StringProperty category;
  483. private final StringProperty bookTitle;
  484. private final StringProperty author;
  485. private final IntegerProperty bookCopies;
  486. private final StringProperty published;
  487. private final StringProperty dateAddedBook;
  488. private final IntegerProperty bookID;
  489.  
  490. //public BookTable() {
  491. // this(1, null, null, null, 1, null, null);
  492. // }
  493.  
  494. public BookTable(int bookID, String bookTitle, String author,
  495. String published, int bookCopies, String category, String dateAddedBook) {
  496.  
  497. this.bookID = new SimpleIntegerProperty(bookID);
  498. this.category = new SimpleStringProperty(category);
  499. this.bookTitle = new SimpleStringProperty(bookTitle);
  500. this.author = new SimpleStringProperty(author);
  501. this.bookCopies = new SimpleIntegerProperty(bookCopies);
  502. this.published = new SimpleStringProperty(published);
  503. this.dateAddedBook = new SimpleStringProperty(dateAddedBook);
  504. }
  505.  
  506. public Integer getBookID() {
  507. return bookID.get();
  508. }
  509.  
  510. public String getCategory() {
  511. return category.get();
  512. }
  513.  
  514. public void setCategory(String category) {
  515. this.category.set(category);
  516. }
  517.  
  518. public StringProperty categoryProperty() {
  519. return category;
  520. }
  521.  
  522. public String getBookTitle() {
  523. return bookTitle.get();
  524. }
  525.  
  526. public void setBookTitle(String bookTitle) {
  527. this.bookTitle.set(bookTitle);
  528. }
  529.  
  530. public StringProperty bookTitleProperty() {
  531.  
  532. return bookTitle;
  533. }
  534.  
  535. public String getAuthor() {
  536. return author.get();
  537. }
  538.  
  539. public void setAuthor(String author) {
  540. this.author.set(author);
  541. }
  542.  
  543. public StringProperty authorProperty() {
  544. return author;
  545. }
  546.  
  547. public String getPublished() {
  548. return published.get();
  549. }
  550.  
  551. public void setPublished(String published) {
  552. this.published.set(published);
  553. }
  554.  
  555. public StringProperty publishedProperty() {
  556. return published;
  557. }
  558.  
  559. public Integer getBookCopies() {
  560. return bookCopies.get();
  561. }
  562.  
  563. public void setBookCopies(int bookCopies) {
  564. this.bookCopies.set(bookCopies);
  565. }
  566.  
  567. public IntegerProperty bookCopiesProperty() {
  568. return bookCopies;
  569. }
  570.  
  571. public String getDateAddedBook() {
  572. return dateAddedBook.get();
  573. }
  574.  
  575. public void setDateAddedBook(String dateAddedBook) {
  576. this.dateAddedBook.set(dateAddedBook);
  577. }
  578.  
  579. public StringProperty dateAddedBookProperty() {
  580. return dateAddedBook;
  581. }
  582. }
  583.  
  584. import java.sql.Connection;
  585. import java.sql.DriverManager;
  586. import java.sql.SQLException;
  587.  
  588. public class DBClass {
  589.  
  590. Connection conn = null;
  591.  
  592. public static Connection ConnecrDb() {
  593. try {
  594. Class.forName("com.mysql.jdbc.Driver");
  595. Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/codexlms?zeroDateTimeBehavior=convertToNull","root","password");
  596. System.out.println("Connection success!");
  597. return conn;
  598. } catch(Exception e){
  599. System.out.println(e);
  600. return null;
  601. }
  602. }
  603. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement