Advertisement
Guest User

Untitled

a guest
Jun 7th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.83 KB | None | 0 0
  1. package application;
  2.  
  3.  
  4. import javafx.scene.Scene;
  5. import javafx.stage.Stage;
  6. import javafx.scene.text.Font;
  7. import javafx.scene.text.Text;
  8. import javafx.scene.control.Button;
  9. import javafx.scene.control.TextField;
  10. import javafx.scene.Group;
  11. import javafx.scene.Node;
  12. import javafx.scene.layout.GridPane;
  13.  
  14.  
  15.  
  16. import java.sql.Connection;
  17. import java.sql.DriverManager;
  18. import java.sql.ResultSet;
  19. import java.sql.SQLException;
  20.  
  21. import javafx.event.ActionEvent;
  22. import javafx.event.EventHandler;
  23. import javafx.geometry.Insets;
  24. import javafx.geometry.Pos;
  25. import com.mysql.jdbc.PreparedStatement;
  26.  
  27.  
  28. public class HomePage extends Stage {
  29.     public HomePage(String userName) {
  30.         Stage current = this;
  31.        
  32.         Connection con;
  33.        
  34.        
  35.        
  36.        
  37.         // Text Area for search label
  38.         TextField search = new TextField();
  39.         search.setMinSize(100, 50);
  40.        
  41.         //search button
  42.         Button buttonFour = new Button("Search by name");
  43.         buttonFour.setMinSize(100, 50);
  44.        
  45.        
  46.        
  47.        
  48.         //search button
  49.         Button buttonFive = new Button("Search by ISBN");
  50.         buttonFive.setMinSize(100, 50);
  51.        
  52.        
  53.        
  54.         //personal info button
  55.         Button buttonOne = new Button("Edit personal info");
  56.         buttonOne.setMinSize(100, 50);
  57.        
  58.        
  59.         //Shopping cart button
  60.         Button buttonTwo = new Button("Shopping cart");
  61.         buttonTwo.setMinSize(100, 50);
  62.        
  63.         //Shopping cart button
  64.         Button buttonThree = new Button("Logout");
  65.         buttonThree.setMinSize(100, 50);
  66.        
  67.        
  68.        
  69.        
  70.        
  71.        
  72.         //Styling nodes  
  73.         buttonOne.setStyle("-fx-background-color: darkslateblue; -fx-text-fill: white;");
  74.         buttonTwo.setStyle("-fx-background-color: darkslateblue; -fx-text-fill: white;");
  75.         buttonThree.setStyle("-fx-background-color: darkslateblue; -fx-text-fill: white;");
  76.         buttonFour.setStyle("-fx-background-color: darkslateblue; -fx-text-fill: white;");
  77.         buttonFive.setStyle("-fx-background-color: darkslateblue; -fx-text-fill: white;");
  78.    
  79.        
  80.        
  81.         //Creating a Grid Pane
  82.         GridPane gridPane = new GridPane();    
  83.          
  84.         //Setting size for the pane  
  85.         gridPane.setMinSize(600, 400);
  86.            
  87.         //Setting the padding  
  88.         gridPane.setPadding(new Insets(10, 10, 10, 10));
  89.          
  90.         //Setting the vertical and horizontal gaps between the columns
  91.         gridPane.setVgap(30);
  92.         gridPane.setHgap(30);      
  93.          
  94.         //Setting the Grid alignment
  95.         gridPane.setAlignment(Pos.CENTER);
  96.            
  97.         //Arranging all the nodes in the grid
  98.         gridPane.add(buttonFive, 0, 0);
  99.         gridPane.add(search,1,0);
  100.         gridPane.add(buttonFour, 2, 0);      
  101.         gridPane.add(buttonOne, 1, 1);
  102.         gridPane.add(buttonTwo, 1, 2);
  103.         gridPane.add(buttonThree, 1, 3);
  104.        
  105.        
  106.        
  107.         buttonFive.setOnAction(new EventHandler<ActionEvent>() {
  108.             @Override
  109.             public void handle(ActionEvent event) {
  110.               String ISBN = search.getText();
  111.               ResultSet rs = null;
  112.              
  113.               // Empty input
  114.               if(ISBN.isEmpty()) {
  115.                   System.out.println("Enter an ISBN");
  116.               }
  117.               else {
  118.                   System.out.println(ISBN);
  119.                  
  120.                   // check ISBN is an integer value or not
  121.                   try {
  122.                       int i = Integer.parseInt(ISBN.trim());
  123.                       // connect to db and get resultSet
  124.                       try {
  125.                           Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/Online_Book_Store?useSSL=false", "root", "ahmed666" );
  126.                           PreparedStatement p = (PreparedStatement) con.prepareStatement("select * from BOOK_STORE where ISBN="+ISBN);
  127.                           rs = p.executeQuery();
  128.                          
  129.                       }
  130.                       catch (SQLException e) {
  131.                           // TODO Auto-generated catch block
  132.                           e.printStackTrace();
  133.                       }
  134.                       // send results to the new page
  135.                       SearchResult page = new SearchResult(rs,current);
  136.                       current.close();
  137.                       page.show();
  138.                      
  139.                   }
  140.                   catch(NumberFormatException nfe)
  141.                   {
  142.                       System.out.println("Invalid ISBN");
  143.                   }
  144.                  
  145.               }
  146.              
  147.  
  148.             }
  149.         });
  150.        
  151.        
  152.         buttonFour.setOnAction(new EventHandler<ActionEvent>() {
  153.             @Override
  154.             public void handle(ActionEvent event) {
  155.               String title = search.getText();
  156.               ResultSet rs = null;
  157.              
  158.               // Empty input
  159.               if(title.isEmpty()) {
  160.                   System.out.println("Enter a Title");
  161.               }
  162.               else {
  163.                   System.out.println(title);
  164.                  
  165.                   // connect to db and get resultSet
  166.                   try {
  167.                       Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/Online_Book_Store?useSSL=false", "root", "ahmed666" );
  168.                       PreparedStatement p = (PreparedStatement) con.prepareStatement("select * from BOOK_STORE where TITLE='"+title+"'");
  169.                       rs = p.executeQuery();
  170.                      
  171.                   }
  172.                   catch (SQLException e) {
  173.                       // TODO Auto-generated catch block
  174.                       e.printStackTrace();
  175.                   }
  176.                  
  177.                   // send results to the new page
  178.                   SearchResult page = new SearchResult(rs , current);
  179.                   current.close();
  180.                   page.show();
  181.               }
  182.              
  183.  
  184.             }
  185.         });
  186.        
  187.        
  188.        
  189.        
  190.        
  191.            
  192.        
  193.        
  194.         Scene scene = new Scene(gridPane);
  195.        
  196.         scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
  197.         setScene(scene);
  198.         setTitle(userName);
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement