Advertisement
Guest User

Untitled

a guest
Jun 6th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.84 KB | None | 0 0
  1. package application;
  2.  
  3. import java.awt.event.MouseEvent;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10.  
  11. import javafx.application.Application;
  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.Button;
  18. import javafx.scene.control.ChoiceBox;
  19. import javafx.scene.control.PasswordField;
  20. import javafx.scene.control.TextField;
  21. import javafx.scene.layout.GridPane;
  22. import javafx.scene.text.Text;
  23. import javafx.stage.Stage;
  24.  
  25. public class Main extends Application {
  26.     Stage theStage;
  27.  
  28.     @Override
  29.     public void start(Stage stage) {
  30.         // creating label email
  31.         theStage = stage;
  32.         Text text1 = new Text("user name");
  33.  
  34.         // creating label password
  35.         Text text2 = new Text("Password");
  36.  
  37.         // Creating Text Filed for email
  38.         TextField textField1 = new TextField();
  39.  
  40.         // Creating Text Filed for password
  41.         PasswordField textField2 = new PasswordField();
  42.  
  43.         // Creating Buttons
  44.         Button button1 = new Button("log in");
  45.         Button button2 = new Button("sign up");
  46.         Button signUpButton = new Button("sign up");
  47.         Button back = new Button("back");
  48.  
  49.         Text logInChoice = new Text("log in as :");
  50.  
  51.         // Choice box for location
  52.         ChoiceBox logInChoiceBox = new ChoiceBox();
  53.         logInChoiceBox.getItems().addAll("Manager", "User");
  54.  
  55.         Text nametext = new Text("Enter your name ");
  56.         Text pass = new Text("choose your password ");
  57.         Text creditCard = new Text("enter credit number ");
  58.         TextField nameField = new TextField();
  59.         TextField passField = new TextField();
  60.         TextField creditField = new TextField();
  61.         Text signUpChoice = new Text("sign up as :");
  62.         ChoiceBox signUpChoiceBox = new ChoiceBox();
  63.         signUpChoiceBox.getItems().addAll("Manager", "User");
  64.  
  65.         // Creating a Grid Pane
  66.         GridPane gridPane = new GridPane();
  67.         GridPane gridPane2 = new GridPane();
  68.  
  69.         // Setting size for the pane
  70.         gridPane.setMinSize(600, 400);
  71.         gridPane2.setMinSize(600, 400);
  72.  
  73.         // Setting the padding
  74.         gridPane.setPadding(new Insets(10, 10, 10, 10));
  75.         gridPane2.setPadding(new Insets(10, 10, 10, 10));
  76.  
  77.         // Setting the vertical and horizontal gaps between the columns
  78.         gridPane.setVgap(30);
  79.         gridPane.setHgap(30);
  80.         gridPane2.setVgap(30);
  81.         gridPane2.setHgap(30);
  82.  
  83.         // Setting the Grid alignment
  84.         gridPane.setAlignment(Pos.CENTER);
  85.         gridPane2.setAlignment(Pos.CENTER);
  86.  
  87.         // Arranging all the nodes in the grid
  88.         gridPane.add(text1, 0, 0);
  89.         gridPane.add(textField1, 1, 0);
  90.         gridPane.add(text2, 0, 1);
  91.         gridPane.add(textField2, 1, 1);
  92.         gridPane.add(logInChoice, 0, 2);
  93.         gridPane.add(logInChoiceBox, 1, 2);
  94.         gridPane.add(button1, 0, 3);
  95.         gridPane.add(button2, 1, 3);
  96.  
  97.         gridPane2.add(nametext, 0, 0);
  98.         gridPane2.add(nameField, 1, 0);
  99.         gridPane2.add(pass, 0, 1);
  100.         gridPane2.add(passField, 1, 1);
  101.         gridPane2.add(creditCard, 0, 2);
  102.         gridPane2.add(creditField, 1, 2);
  103.         gridPane2.add(signUpChoice, 0, 3);
  104.         gridPane2.add(signUpChoiceBox, 1, 3);
  105.         gridPane2.add(signUpButton, 0, 4);
  106.         gridPane2.add(back, 1, 4);
  107.  
  108.         // Styling nodes
  109.         button1.setStyle("-fx-background-color: darkslateblue; -fx-text-fill: white;");
  110.         button2.setStyle("-fx-background-color: darkslateblue; -fx-text-fill: white;");
  111.  
  112.         text1.setStyle("-fx-font: normal bold 20px 'serif' ");
  113.         text2.setStyle("-fx-font: normal bold 20px 'serif' ");
  114.         logInChoice.setStyle("-fx-font: normal bold 20px 'serif' ");
  115.  
  116.         // Creating a scene object
  117.         Scene scene = new Scene(gridPane);
  118.         Scene scene2 = new Scene(gridPane2);
  119.  
  120.         // Setting title to the Stage
  121.         stage.setTitle("login ");
  122.  
  123.         // Adding scene to the stage
  124.         stage.setScene(scene);
  125.  
  126.         // Displaying the contents of the stage
  127.         stage.show();
  128.  
  129.         button2.setOnAction(new EventHandler<ActionEvent>() {
  130.             @Override
  131.             public void handle(ActionEvent event) {
  132.                 theStage.setTitle("sign up");
  133.                 theStage.setScene(scene2);
  134.                 theStage.show();
  135.  
  136.             }
  137.         });
  138.  
  139.         button1.setOnAction(new EventHandler<ActionEvent>() {
  140.             @Override
  141.             public void handle(ActionEvent event) {
  142.                 System.out.println(textField1.getText());
  143.                 System.out.println(textField2.getText());
  144.                 System.out.println(logInChoiceBox.getValue());
  145.  
  146.             }
  147.         });
  148.  
  149.         back.setOnAction(new EventHandler<ActionEvent>() {
  150.             @Override
  151.             public void handle(ActionEvent event) {
  152.                 theStage.setScene(scene);
  153.                 theStage.show();
  154.  
  155.             }
  156.         });
  157.  
  158.         signUpButton.setOnAction(new EventHandler<ActionEvent>() {
  159.             @Override
  160.             public void handle(ActionEvent event) {
  161.                 String name = nameField.getText();
  162.                 String pass = passField.getText();
  163.                 String numberOfCredit = creditField.getText();
  164.                 String type = (String) signUpChoiceBox.getValue();
  165.                
  166.                
  167.           /*       String databaseURL = "jdbc:mysql://localhost:3306/sample47";
  168.                 String user = "root";
  169.                 String password = "admin";
  170.                 Connection conn = null;
  171.                 try {
  172.                     Class.forName("com.mysql.jdbc.Driver");
  173.                     conn = DriverManager.getConnection(databaseURL, user, password);
  174.                     if (conn != null) {
  175.                         System.out.println("Connected to the database");
  176.                     }
  177.                 } catch (ClassNotFoundException ex) {
  178.                     System.out.println("Could not find database driver class");
  179.                     ex.printStackTrace();
  180.                 } catch (SQLException ex) {
  181.                     System.out.println("An error occurred. Maybe user/password is invalid");
  182.                     ex.printStackTrace();
  183.                 } finally {
  184.                     if (conn != null) {
  185.                         try {
  186.                             conn.close();
  187.                         } catch (SQLException ex) {
  188.                             ex.printStackTrace();
  189.                         }
  190.                     }
  191.                 }*/
  192.  
  193.                 try {
  194.                     Class.forName("com.mysql.jdbc.Driver") ;
  195.                 } catch (ClassNotFoundException e) {
  196.                     // TODO Auto-generated catch block
  197.                     e.printStackTrace();
  198.                 }
  199.                 Connection conn = null;
  200.                 try {
  201.                     conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sample47?useSSL=false", "root", "admin");
  202.                 } catch (SQLException e) {
  203.                     // TODO Auto-generated catch block
  204.                     e.printStackTrace();
  205.                 }
  206.                 PreparedStatement stmt = null;
  207.                 try {
  208.                     String query = "select TITLE from book where Book_ID=1" ;
  209.                     stmt = conn.prepareStatement(query);
  210.                 } catch (SQLException e) {
  211.                     // TODO Auto-generated catch block
  212.                     e.printStackTrace();
  213.                 }
  214.                 try {
  215.                     ResultSet rs = stmt.executeQuery() ;
  216.                     while(rs.next()) {
  217.                         System.out.println(rs.getString("TITLE"));
  218.                     }
  219.                 } catch (SQLException e) {
  220.                     // TODO Auto-generated catch block
  221.                     e.printStackTrace();
  222.                 }
  223.                
  224.            
  225.  
  226.             }
  227.  
  228.         });
  229.  
  230.     }
  231.  
  232.     public static void main(String args[]) {
  233.         launch(args);
  234.     }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement