Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package application;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import com.mysql.cj.jdbc.integration.jboss.ExtendedMysqlExceptionSorter;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.geometry.Insets;
- import javafx.geometry.Pos;
- import javafx.scene.Scene;
- import javafx.scene.control.Alert;
- import javafx.scene.control.Alert.AlertType;
- import javafx.scene.control.Button;
- import javafx.scene.control.ButtonType;
- import javafx.scene.control.ChoiceBox;
- import javafx.scene.control.PasswordField;
- import javafx.scene.control.TextField;
- import javafx.scene.layout.GridPane;
- import javafx.scene.text.Text;
- import javafx.stage.Stage;
- public class SignUp extends Stage{
- public SignUp(Stage stage,Scene scene) {
- setAttribute(stage,scene);
- }
- void setAttribute(Stage stage,Scene scene) {
- Text userName = new Text("user Name ");
- Text pass = new Text("password ");
- Text creditCard = new Text("credit number ");
- Text FName = new Text("first name ");
- Text Lname = new Text("last name ");
- Text phone = new Text("phone number ");
- Text shippingAddress = new Text("shipping address ");
- Text EMail = new Text("E-mai");
- TextField userNameField = new TextField();
- PasswordField passField = new PasswordField();
- TextField creditField = new TextField();
- TextField firstNameField = new TextField();
- TextField lastNameField = new TextField();
- TextField phoneField = new TextField();
- TextField shippingField = new TextField();
- TextField EMailField = new TextField();
- Text signUpChoice = new Text("sign up as :");
- ChoiceBox signUpChoiceBox = new ChoiceBox();
- signUpChoiceBox.getItems().addAll( "User","Manager");
- signUpChoiceBox.getSelectionModel().selectFirst();
- Button signUpButton = new Button("sign up");
- Button back = new Button("back");
- GridPane gridPane2 = new GridPane();
- gridPane2.setMinSize(600, 400);
- gridPane2.setPadding(new Insets(10, 10, 10, 10));
- // Setting the vertical and horizontal gaps between the columns
- gridPane2.setVgap(30);
- gridPane2.setHgap(30);
- // Setting the Grid alignment
- gridPane2.setAlignment(Pos.CENTER);
- // Arranging all the nodes in the grid
- gridPane2.add(userName, 0, 0);
- gridPane2.add(userNameField, 1, 0);
- gridPane2.add(pass, 0, 1);
- gridPane2.add(passField, 1, 1);
- gridPane2.add(FName, 0, 2);
- gridPane2.add(firstNameField, 1, 2);
- gridPane2.add(Lname, 0, 3);
- gridPane2.add(lastNameField, 1, 3);
- gridPane2.add(phone, 0, 4);
- gridPane2.add(phoneField, 1, 4);
- gridPane2.add(EMail, 0, 5);
- gridPane2.add(EMailField, 1, 5);
- gridPane2.add(shippingAddress, 0, 6);
- gridPane2.add(shippingField, 1, 6);
- gridPane2.add(creditCard, 0, 7);
- gridPane2.add(creditField, 1, 7);
- gridPane2.add(signUpChoice, 0, 8);
- gridPane2.add(signUpChoiceBox, 1, 8);
- gridPane2.add(signUpButton, 0, 9);
- gridPane2.add(back, 1, 9);
- // Styling nodes
- signUpButton.setStyle("-fx-background-color: darkslateblue; -fx-text-fill: white;");
- back.setStyle("-fx-background-color: darkslateblue; -fx-text-fill: white;");
- userName.setStyle("-fx-font: normal bold 20px 'serif' ");
- pass.setStyle("-fx-font: normal bold 20px 'serif' ");
- FName.setStyle("-fx-font: normal bold 20px 'serif' ");
- Lname.setStyle("-fx-font: normal bold 20px 'serif' ");
- phone.setStyle("-fx-font: normal bold 20px 'serif' ");
- EMail.setStyle("-fx-font: normal bold 20px 'serif' ");
- shippingAddress.setStyle("-fx-font: normal bold 20px 'serif' ");
- creditCard.setStyle("-fx-font: normal bold 20px 'serif' ");
- signUpChoice.setStyle("-fx-font: normal bold 20px 'serif' ");
- // Creating a scene object
- Scene scene2 = new Scene(gridPane2);
- // Setting title to the Stage
- stage.setTitle("Sign Up ");
- // Adding scene to the stage
- stage.setScene(scene2);
- // Displaying the contents of the stage
- stage.show();
- back.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent event) {
- stage.setTitle("Log in ");
- // Adding scene to the stage
- stage.setScene(scene);
- // Displaying the contents of the stage
- stage.show();
- }
- });
- signUpButton.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent event) {
- String userName = userNameField.getText();
- String pass = passField.getText();
- String firstName =firstNameField.getText();
- String lastName = lastNameField.getText();
- String phone= phoneField.getText();
- String Email =EMailField.getText();
- String shipping =shippingField.getText();
- String numberOfCredit = creditField.getText();
- String type = (String) signUpChoiceBox.getValue();
- System.out.println(userName);
- System.out.println(pass);
- System.out.println(firstName);
- System.out.println(lastName);
- System.out.println(phone);
- System.out.println(Email);
- System.out.println(shipping);
- System.out.println(numberOfCredit);
- System.out.println(type);
- try {
- Class.forName("com.mysql.jdbc.Driver") ;
- } catch (ClassNotFoundException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- Connection conn = null;
- try {
- conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Online_Book_Store?useSSL=false", "root", "admin");
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- try {
- String query;
- String selectQuery;
- if(type.equals("Manager")) {
- query = "insert into MANAGERS values (?,?,?,?,?,?,?,?)" ;
- selectQuery="select * from MANAGERS where USER_NAME = ?";
- }else {
- query = "insert into USERS values (?,?,?,?,?,?,?,?)" ;
- selectQuery="select * from USERS where USER_NAME = ?";
- }
- PreparedStatement selectStmt = conn.prepareStatement(selectQuery);
- selectStmt.setString(1, userName);
- ResultSet res=selectStmt.executeQuery();
- PreparedStatement stmt = conn.prepareStatement(query);
- stmt.setString(1, userName);
- stmt.setString(2, pass);
- stmt.setString(3, firstName);
- stmt.setString(4, lastName);
- stmt.setString(5, phone);
- stmt.setString(6, Email);
- stmt.setString(7, shipping);
- stmt.setString(8, numberOfCredit);
- if(pass.isEmpty()) {
- Alert alert = new Alert(AlertType.WARNING, "please enter password" , ButtonType.OK);
- alert.showAndWait();
- }else if (res.next()) {
- Alert alert = new Alert(AlertType.WARNING, "dublicate user name " , ButtonType.OK);
- alert.showAndWait();
- }
- else {
- stmt.executeUpdate();
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- try {
- conn.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement