Advertisement
Guest User

sign up

a guest
Jun 7th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.09 KB | None | 0 0
  1. package application;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8.  
  9. import com.mysql.cj.jdbc.integration.jboss.ExtendedMysqlExceptionSorter;
  10.  
  11. import javafx.event.ActionEvent;
  12. import javafx.event.EventHandler;
  13. import javafx.geometry.Insets;
  14. import javafx.geometry.Pos;
  15. import javafx.scene.Scene;
  16. import javafx.scene.control.Alert;
  17. import javafx.scene.control.Alert.AlertType;
  18. import javafx.scene.control.Button;
  19. import javafx.scene.control.ButtonType;
  20. import javafx.scene.control.ChoiceBox;
  21. import javafx.scene.control.PasswordField;
  22. import javafx.scene.control.TextField;
  23. import javafx.scene.layout.GridPane;
  24. import javafx.scene.text.Text;
  25. import javafx.stage.Stage;
  26.  
  27. public class SignUp extends Stage{
  28.    
  29.     public  SignUp(Stage stage,Scene scene) {
  30.         setAttribute(stage,scene);
  31.        
  32.     }
  33.    
  34.     void setAttribute(Stage stage,Scene scene) {
  35.         Text userName = new Text("user Name ");
  36.         Text pass = new Text("password ");
  37.         Text creditCard = new Text("credit number ");
  38.         Text FName = new Text("first name ");
  39.         Text Lname = new Text("last name ");
  40.         Text phone = new Text("phone number ");
  41.         Text shippingAddress = new Text("shipping address ");
  42.         Text EMail = new Text("E-mai");
  43.         TextField userNameField = new TextField();
  44.         PasswordField passField = new PasswordField();
  45.         TextField creditField = new TextField();
  46.         TextField firstNameField = new TextField();
  47.         TextField lastNameField = new TextField();
  48.         TextField phoneField = new TextField();
  49.         TextField shippingField = new TextField();
  50.         TextField EMailField = new TextField();
  51.  
  52.         Text signUpChoice = new Text("sign up as :");
  53.         ChoiceBox signUpChoiceBox = new ChoiceBox();
  54.         signUpChoiceBox.getItems().addAll( "User","Manager");
  55.         signUpChoiceBox.getSelectionModel().selectFirst();
  56.  
  57.        
  58.         Button signUpButton = new Button("sign up");
  59.         Button back = new Button("back");
  60.         GridPane gridPane2 = new GridPane();
  61.        
  62.         gridPane2.setMinSize(600, 400);
  63.         gridPane2.setPadding(new Insets(10, 10, 10, 10));
  64.  
  65.         // Setting the vertical and horizontal gaps between the columns
  66.         gridPane2.setVgap(30);
  67.         gridPane2.setHgap(30);
  68.  
  69.         // Setting the Grid alignment
  70.         gridPane2.setAlignment(Pos.CENTER);
  71.  
  72.         // Arranging all the nodes in the grid
  73.  
  74.         gridPane2.add(userName, 0, 0);
  75.         gridPane2.add(userNameField, 1, 0);
  76.         gridPane2.add(pass, 0, 1);
  77.         gridPane2.add(passField, 1, 1);
  78.         gridPane2.add(FName, 0, 2);
  79.         gridPane2.add(firstNameField, 1, 2);
  80.         gridPane2.add(Lname, 0, 3);
  81.         gridPane2.add(lastNameField, 1, 3);
  82.         gridPane2.add(phone, 0, 4);
  83.         gridPane2.add(phoneField, 1, 4);
  84.         gridPane2.add(EMail, 0, 5);
  85.         gridPane2.add(EMailField, 1, 5);
  86.         gridPane2.add(shippingAddress, 0, 6);
  87.         gridPane2.add(shippingField, 1, 6);
  88.         gridPane2.add(creditCard, 0, 7);
  89.         gridPane2.add(creditField, 1, 7);
  90.         gridPane2.add(signUpChoice, 0, 8);
  91.         gridPane2.add(signUpChoiceBox, 1, 8);
  92.         gridPane2.add(signUpButton, 0, 9);
  93.         gridPane2.add(back, 1, 9);
  94.  
  95.         // Styling nodes
  96.         signUpButton.setStyle("-fx-background-color: darkslateblue; -fx-text-fill: white;");
  97.         back.setStyle("-fx-background-color: darkslateblue; -fx-text-fill: white;");
  98.  
  99.         userName.setStyle("-fx-font: normal bold 20px 'serif' ");
  100.         pass.setStyle("-fx-font: normal bold 20px 'serif' ");
  101.         FName.setStyle("-fx-font: normal bold 20px 'serif' ");
  102.         Lname.setStyle("-fx-font: normal bold 20px 'serif' ");
  103.         phone.setStyle("-fx-font: normal bold 20px 'serif' ");
  104.         EMail.setStyle("-fx-font: normal bold 20px 'serif' ");
  105.         shippingAddress.setStyle("-fx-font: normal bold 20px 'serif' ");
  106.         creditCard.setStyle("-fx-font: normal bold 20px 'serif' ");
  107.         signUpChoice.setStyle("-fx-font: normal bold 20px 'serif' ");
  108.  
  109.         // Creating a scene object
  110.         Scene scene2 = new Scene(gridPane2);
  111.        
  112.         // Setting title to the Stage
  113.         stage.setTitle("Sign Up ");
  114.  
  115.         // Adding scene to the stage
  116.         stage.setScene(scene2);
  117.  
  118.         // Displaying the contents of the stage
  119.         stage.show();
  120.  
  121.  
  122.  
  123.  
  124.         back.setOnAction(new EventHandler<ActionEvent>() {
  125.             @Override
  126.             public void handle(ActionEvent event) {
  127.                 stage.setTitle("Log in ");
  128.  
  129.                 // Adding scene to the stage
  130.                 stage.setScene(scene);
  131.  
  132.                 // Displaying the contents of the stage
  133.                 stage.show();
  134.  
  135.  
  136.             }
  137.         });
  138.  
  139.         signUpButton.setOnAction(new EventHandler<ActionEvent>()  {
  140.             @Override
  141.             public void handle(ActionEvent event) {
  142.                 String userName = userNameField.getText();
  143.                 String pass = passField.getText();
  144.                 String firstName =firstNameField.getText();
  145.                 String lastName = lastNameField.getText();
  146.                 String phone= phoneField.getText();
  147.                 String Email =EMailField.getText();
  148.                 String shipping =shippingField.getText();
  149.                 String numberOfCredit = creditField.getText();
  150.                 String type = (String) signUpChoiceBox.getValue();
  151.                
  152.                 System.out.println(userName);
  153.                 System.out.println(pass);
  154.                 System.out.println(firstName);
  155.                 System.out.println(lastName);
  156.                 System.out.println(phone);
  157.                 System.out.println(Email);
  158.                 System.out.println(shipping);
  159.                 System.out.println(numberOfCredit);
  160.                 System.out.println(type);
  161.  
  162.  
  163.                
  164.  
  165.                
  166.                     try {
  167.                         Class.forName("com.mysql.jdbc.Driver") ;
  168.                     } catch (ClassNotFoundException e1) {
  169.                         // TODO Auto-generated catch block
  170.                         e1.printStackTrace();
  171.                     }
  172.                
  173.                 Connection conn = null;
  174.                 try {
  175.                     conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Online_Book_Store?useSSL=false", "root", "admin");
  176.                 } catch (SQLException e) {
  177.                     // TODO Auto-generated catch block
  178.                     e.printStackTrace();
  179.                 }
  180.                 try {
  181.                     String query;
  182.                     String selectQuery;
  183.                    
  184.                     if(type.equals("Manager")) {
  185.                          query = "insert into MANAGERS values (?,?,?,?,?,?,?,?)" ;
  186.                          selectQuery="select * from MANAGERS where USER_NAME = ?";
  187.                     }else {
  188.                          query = "insert into USERS values (?,?,?,?,?,?,?,?)" ;
  189.                          selectQuery="select * from USERS where USER_NAME = ?";
  190.                     }
  191.                     PreparedStatement selectStmt = conn.prepareStatement(selectQuery);
  192.                     selectStmt.setString(1, userName);
  193.                     ResultSet res=selectStmt.executeQuery();
  194.                    
  195.  
  196.  
  197.                    
  198.                     PreparedStatement stmt = conn.prepareStatement(query);
  199.                     stmt.setString(1, userName);
  200.                     stmt.setString(2, pass);
  201.                     stmt.setString(3, firstName);
  202.                     stmt.setString(4, lastName);
  203.                     stmt.setString(5, phone);
  204.                     stmt.setString(6, Email);
  205.                     stmt.setString(7, shipping);
  206.                     stmt.setString(8, numberOfCredit);
  207.                     if(pass.isEmpty()) {
  208.                         Alert alert = new Alert(AlertType.WARNING, "please enter password" , ButtonType.OK);
  209.                         alert.showAndWait();
  210.                     }else if (res.next()) {
  211.                         Alert alert = new Alert(AlertType.WARNING, "dublicate user name " , ButtonType.OK);
  212.                         alert.showAndWait();                       
  213.                     }
  214.                     else {
  215.                         stmt.executeUpdate();
  216.  
  217.                     }
  218.                 } catch (SQLException e) {
  219.                     // TODO Auto-generated catch block
  220.                     e.printStackTrace();
  221.                 }
  222.                
  223.                 try {
  224.                     conn.close();
  225.                 } catch (SQLException e) {
  226.                     // TODO Auto-generated catch block
  227.                     e.printStackTrace();
  228.                 }
  229.            
  230.  
  231.             }
  232.  
  233.         });
  234.  
  235.  
  236.        
  237.     }
  238.    
  239.    
  240.    
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement