Advertisement
Guest User

Untitled

a guest
Jun 11th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.36 KB | None | 0 0
  1. package application;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.Date;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.time.LocalDate;
  10. import java.time.format.DateTimeFormatter;
  11.  
  12. import javafx.event.ActionEvent;
  13. import javafx.event.EventHandler;
  14. import javafx.geometry.Insets;
  15. import javafx.geometry.Pos;
  16. import javafx.scene.Scene;
  17. import javafx.scene.control.Alert;
  18. import javafx.scene.control.Button;
  19. import javafx.scene.control.ButtonType;
  20. import javafx.scene.control.ChoiceBox;
  21. import javafx.scene.control.DatePicker;
  22. import javafx.scene.control.TextField;
  23. import javafx.scene.control.Alert.AlertType;
  24. import javafx.scene.layout.GridPane;
  25. import javafx.scene.text.Text;
  26. import javafx.stage.Stage;
  27. import sun.util.resources.LocaleData;
  28.  
  29. public class ModifyBook extends Stage{
  30.  
  31.     ModifyBook ( String type ,String ISBN ,Stage prev , Stage pPrev , int id ) throws SQLException{
  32.         Stage current = this;
  33.         setAttributes(type ,prev,ISBN,current , pPrev ,id);
  34.     }
  35.    
  36.     private void setAttributes(String type ,Stage prev ,String isbn , Stage current , Stage pPrev , int id) throws SQLException {
  37.        
  38.        
  39.         String input;
  40.        Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/Online_Book_Store?useSSL=false", "root", "ahmed666" );       
  41.         String query="select * from BOOK_STORE where ISBN = ?";
  42.         PreparedStatement pst=con.prepareStatement(query);
  43.         pst.setString(1,isbn);
  44.         ResultSet rs = pst.executeQuery();
  45.         while(rs.next()) {
  46.            
  47.         Text ISBN = new Text("book ISBN ");
  48.         Text title = new Text("title ");
  49.         Text bookCopies = new Text("bookCopies ");
  50.         Text pubisherID = new Text("publisher ID ");
  51.         Text pubplicationYear = new Text("publication year ");
  52.         Text threshold = new Text("threshold ");
  53.         Text price = new Text("price ");
  54.         Text category = new Text("category");
  55.         Text authorID= new Text("Author ID");
  56.         Text authorName = new Text("Author Name");
  57.  
  58.         TextField bookISBNField = new TextField();
  59.         bookISBNField.setText(rs.getString("ISBN"));
  60.         TextField titleField = new TextField(rs.getString("TITLE"));
  61.         TextField BookCopiesField = new TextField(rs.getString("BOOK_COPIES"));
  62.         TextField publisherIDField = new TextField(rs.getString("PUBLISHER_ID"));
  63.         DatePicker publicationYearField = new DatePicker();
  64.         TextField thresholdField = new TextField(rs.getString("THRESHOLD"));
  65.         TextField priceField = new TextField(rs.getString("PRICE"));
  66.         TextField AuthorIDField = new TextField(rs.getString("AUTHOR_ID"));
  67.         TextField AuthorNameField = new TextField(rs.getString("AUTHOR_Name"));
  68.        
  69.        
  70.         ChoiceBox categoryField = new ChoiceBox();
  71.         categoryField.getItems().addAll( "Science", "Art", "Religion","History" , "Geography");
  72.         categoryField.setValue(rs.getString("CATEGORY"));
  73.  
  74.  
  75.         Button editBook = new Button("edit book");
  76.         Button cancel = new Button("cancel");
  77.         GridPane gridPane = new GridPane();
  78.  
  79.         gridPane.setMinSize(600, 400);
  80.         gridPane.setPadding(new Insets(10, 10, 10, 10));
  81.  
  82.         // Setting the vertical and horizontal gaps between the columns
  83.         gridPane.setVgap(30);
  84.         gridPane.setHgap(30);
  85.  
  86.         // Setting the Grid alignment
  87.         gridPane.setAlignment(Pos.CENTER);
  88.  
  89.         // Arranging all the nodes in the grid
  90.  
  91.         gridPane.add(ISBN, 0, 0);
  92.         gridPane.add(bookISBNField, 1, 0);
  93.         gridPane.add(title, 0, 1);
  94.         gridPane.add(titleField, 1, 1);
  95.         gridPane.add(bookCopies, 0, 2);
  96.         gridPane.add(BookCopiesField, 1, 2);
  97.         gridPane.add(pubisherID, 0, 3);
  98.         gridPane.add(publisherIDField, 1, 3);
  99.         gridPane.add(pubplicationYear, 0, 4);
  100.         gridPane.add(publicationYearField, 1, 4);
  101.         gridPane.add(threshold, 0, 5);
  102.         gridPane.add(thresholdField, 1, 5);
  103.         gridPane.add(price, 0, 6);
  104.         gridPane.add(priceField, 1, 6);
  105.         gridPane.add(authorID, 0, 7);
  106.         gridPane.add(AuthorIDField, 1, 7);
  107.         gridPane.add(authorName, 0, 8);
  108.         gridPane.add(AuthorNameField, 1, 8);
  109.         gridPane.add(category, 0, 9);
  110.         gridPane.add(categoryField, 1, 9);
  111.         gridPane.add(editBook, 0, 10);
  112.         gridPane.add(cancel, 1, 10);
  113.  
  114.         // Styling nodes
  115.         editBook.setStyle("-fx-background-color: darkslateblue; -fx-text-fill: white;");
  116.         cancel.setStyle("-fx-background-color: darkslateblue; -fx-text-fill: white;");
  117.  
  118.         ISBN.setStyle("-fx-font: normal bold 20px 'serif' ");
  119.         title.setStyle("-fx-font: normal bold 20px 'serif' ");
  120.         bookCopies.setStyle("-fx-font: normal bold 20px 'serif' ");
  121.         pubisherID.setStyle("-fx-font: normal bold 20px 'serif' ");
  122.         pubplicationYear.setStyle("-fx-font: normal bold 20px 'serif' ");
  123.         threshold.setStyle("-fx-font: normal bold 20px 'serif' ");
  124.         price.setStyle("-fx-font: normal bold 20px 'serif' ");
  125.         category.setStyle("-fx-font: normal bold 20px 'serif' ");
  126.         authorID.setStyle("-fx-font: normal bold 20px 'serif' ");
  127.         authorName.setStyle("-fx-font: normal bold 20px 'serif' ");
  128.  
  129.         // Creating a scene object
  130.         Scene scene2 = new Scene(gridPane);
  131.  
  132.         // Setting title to the Stage
  133.         setTitle("ADD BOOK ");
  134.  
  135.         // Adding scene to the stage
  136.         setScene(scene2);
  137.  
  138.  
  139.         cancel.setOnAction(new EventHandler<ActionEvent>() {
  140.             @Override
  141.             public void handle(ActionEvent event) {
  142.                 current.close();
  143.                 prev.show();
  144.  
  145.             }
  146.         });
  147.  
  148.         editBook.setOnAction(new EventHandler<ActionEvent>() {
  149.             @Override
  150.             public void handle(ActionEvent event) {
  151.                 String ISBN = bookISBNField.getText();
  152.                 String title = titleField.getText();
  153.                 String BookCopies = BookCopiesField.getText();
  154.                 String publisherID = publisherIDField.getText();
  155.                 //String publicationYear = publicationYearField.getText();
  156.                 String threshold = thresholdField.getText();
  157.                 String price = priceField.getText();
  158.                 String category = (String) categoryField.getValue();
  159.                 String authorID = AuthorIDField.getText();
  160.                 String authorName = AuthorNameField.getText();
  161.  
  162.                
  163.  
  164.                 try {
  165.                     Class.forName("com.mysql.jdbc.Driver");
  166.                 } catch (ClassNotFoundException e1) {
  167.                     // TODO Auto-generated catch block
  168.                     e1.printStackTrace();
  169.                 }
  170.  
  171.                 Connection conn = null;
  172.                 try {
  173.                     conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Online_Book_Store?useSSL=false",
  174.                             "root", "ahmed666");
  175.                 } catch (SQLException e) {
  176.                     // TODO Auto-generated catch block
  177.                     e.printStackTrace();
  178.                 }
  179.                 try {
  180.                     String query;
  181.                     String selectQuery;
  182.                     String selectQuery2 ;
  183.  
  184.                     query = "update BOOK_STORE set ISBN=? , TITLE=? , BOOK_COPIES=? , PUBLISHER_ID=? , PUBLICATION_YEAR=? , THRESHOLD=? , PRICE=? , CATEGORY=? ,AUTHOR_ID =? , AUTHOR_Name =? where ISBN=? ";
  185.                    
  186.                     selectQuery = "select * from BOOK_STORE where ISBN = ?";
  187.                     PreparedStatement selectStmt = conn.prepareStatement(selectQuery);
  188.                     selectStmt.setString(1, ISBN);
  189.                     ResultSet res = selectStmt.executeQuery();
  190.                    
  191.                     selectQuery2 = "select * from PUBLISHER where ID = ?";
  192.                     PreparedStatement selectStmt2 = conn.prepareStatement(selectQuery2);
  193.                     selectStmt2.setString(1, publisherID);
  194.                     ResultSet res2 = selectStmt2.executeQuery();
  195.  
  196.  
  197.  
  198.                     PreparedStatement stmt = conn.prepareStatement(query);
  199.                     stmt.setString(1, ISBN);
  200.                     stmt.setString(2, title);
  201.                     stmt.setString(3, BookCopies);
  202.                     stmt.setString(4, publisherID);
  203.                     stmt.setObject(5, publicationYearField.getValue());
  204.                     stmt.setString(6, threshold);
  205.                     stmt.setString(7, price);
  206.                     stmt.setString(8, category);
  207.                     stmt.setString(9, authorID);
  208.                     stmt.setString(10, authorName);
  209.                     stmt.setString(11, isbn);
  210.                     if (price.isEmpty()) {
  211.                         Alert alert = new Alert(AlertType.WARNING, "please enter book price", ButtonType.OK);
  212.                         alert.showAndWait();
  213.                     } else if (threshold.isEmpty()) {
  214.                         Alert alert = new Alert(AlertType.WARNING, "please enter book threshold", ButtonType.OK);
  215.                         alert.showAndWait();
  216.                     } else if (!res2.first()) {
  217.                         Alert alert = new Alert(AlertType.WARNING, "please enter this publisher first", ButtonType.OK);
  218.                         alert.showAndWait();
  219.  
  220.                     }
  221.                    
  222.                    
  223.                     else if(ISBN.equals(isbn)) {
  224.                        
  225.                         stmt.executeUpdate();
  226.                         current.close();
  227.                         if(type.equals("ISBN")) {
  228.                             SearchForManager search = new SearchForManager(ISBN , type , pPrev , id);
  229.                             search.show();
  230.                         }
  231.                         else if(type == "TITLE") {
  232.                             SearchForManager search = new SearchForManager(title , type , pPrev , id);
  233.                             search.show();
  234.                         }
  235.                         else {
  236.                             SearchForManager search = new SearchForManager(category , type , pPrev , id);
  237.                             search.show();
  238.                         }
  239.                        
  240.                        
  241.                     }
  242.                     else if (res.first() ) {
  243.                         Alert alert = new Alert(AlertType.WARNING, "dublicate ISBN", ButtonType.OK);
  244.                         alert.showAndWait();
  245.  
  246.                     }
  247.                     else {
  248.                         stmt.executeUpdate();
  249.                         current.close();
  250.                         if(type.equals("ISBN")) {
  251.                             SearchForManager search = new SearchForManager(ISBN , type , pPrev , id);
  252.                             search.show();
  253.                         }
  254.                         else if(type == "TITLE") {
  255.                             SearchForManager search = new SearchForManager(title , type , pPrev , id);
  256.                             search.show();
  257.                         }
  258.                         else {
  259.                             SearchForManager search = new SearchForManager(category , type , pPrev , id);
  260.                             search.show();
  261.                         }
  262.                     }
  263.                 } catch (SQLException e) {
  264.                     // TODO Auto-generated catch block
  265.                     e.printStackTrace();
  266.                 }
  267.  
  268.                 try {
  269.                     conn.close();
  270.                 } catch (SQLException e) {
  271.                     // TODO Auto-generated catch block
  272.                     e.printStackTrace();
  273.                 }
  274.  
  275.             }
  276.  
  277.         });
  278.  
  279.     }
  280.     }
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement