JavaFX Scene Builder Login and SignUp page design with MySQL Database Integration

Code Source: Classs mysqlconnect.java  package login; import java.sql.Connection; import java.sql.DriverManager; import javax.swi...



Code Source:
Classs mysqlconnect.java 

package login;

import java.sql.Connection;
import java.sql.DriverManager;
import javax.swing.JOptionPane;

/**
 *
 * @author amir
 */
public class mysqlconnect {
    Connection conn = null;
    public static Connection ConnectDb(){
 
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/market","root","");
            JOptionPane.showMessageDialog(null, "Connection Established");
            return conn;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
            return null;
        } 
    }

}



Class Login.java

package login;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author amir
 */
public class Login extends Application {
    
    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
        
        Scene scene = new Scene(root);   
        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
}


 Class FXMLDocumentController.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package login;

import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javax.swing.JOptionPane;

/**
 *
 * @author amir
 */
public class FXMLDocumentController implements Initializable {
    
  @FXML
    private AnchorPane pane_login;

    @FXML
    private TextField txt_username;

    @FXML
    private PasswordField txt_password;

    @FXML
    private ComboBox type;

    @FXML
    private Button btn_login;

    @FXML
    private AnchorPane pane_signup;

    @FXML
    private TextField txt_username_up;

    @FXML
    private TextField txt_password_up;

    @FXML
    private TextField email_up;

    @FXML
    private ComboBox  type_up;
    
    
    Connection conn = null;
    ResultSet rs = null;
    PreparedStatement pst = null;
    
    public void LoginpaneShow(){
    
        pane_login.setVisible(true);
        pane_signup.setVisible(false);
    }
    
    public void SignuppaneShow(){
    
        pane_login.setVisible(false);
        pane_signup.setVisible(true);
    }
    
    
    @FXML  
    private void Login (ActionEvent event) throws Exception{  
    conn = mysqlconnect.ConnectDb();
    String sql = "Select * from users where username = ? and password = ? and type = ? ";
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1, txt_username.getText());
            pst.setString(2, txt_password.getText());
            pst.setString(3, type.getValue().toString());
            
            rs = pst.executeQuery();
            
            if(rs.next()){ 
                JOptionPane.showMessageDialog(null, "Username And Password is Corect");
                
                btn_login.getScene().getWindow().hide();
                Parent root = FXMLLoader.load(getClass().getResource("CPanel.fxml"));
                Stage mainStage = new Stage();
                Scene scene = new Scene(root);
                mainStage.setScene(scene);
                mainStage.show();
                
            }else
                JOptionPane.showMessageDialog(null, "Invalide Username Or Password");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }
   
    }
    
    
    public void add_users(ActionEvent event){    
        conn = mysqlconnect.ConnectDb();
        String sql = "insert into users (username,password,type,email) values (?,?,?,?)";
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1, txt_username_up.getText());
            pst.setString(2, txt_password_up.getText());
            pst.setString(3, type_up.getValue().toString());
            pst.setString(4, email_up.getText());
            pst.execute();
            
            JOptionPane.showMessageDialog(null, "Saved");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }  
    }
    
    
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
         type_up.getItems().addAll("Admin","Client","Cashier","Storekeeper");
         type.getItems().addAll("Admin","Client","Cashier","Storekeeper");
    }    
    
}


FXMLDocument.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.Font?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="500.0" style="-fx-background-color: #28252e;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="login.FXMLDocumentController">
   <children>
      <HBox layoutY="14.0" prefHeight="71.0" prefWidth="500.0" style="-fx-border-color: #0fea88; -fx-border-width: 2;" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
         <children>
            <Button mnemonicParsing="false" onAction="#LoginpaneShow" prefHeight="45.0" prefWidth="220.0" style="-fx-background-color: #0fea88;" text="Login" textFill="WHITE">
               <font>
                  <Font name="Times New Roman Bold" size="30.0" />
               </font>
               <HBox.margin>
                  <Insets bottom="10.0" left="15.0" right="15.0" top="10.0" />
               </HBox.margin>
            </Button>
            <Button mnemonicParsing="false" onAction="#SignuppaneShow" prefHeight="45.0" prefWidth="220.0" style="-fx-background-color: #4e5a51;" text="SignUp" textFill="#ebebeb">
               <font>
                  <Font name="Times New Roman Bold" size="30.0" />
               </font>
               <HBox.margin>
                  <Insets bottom="10.0" left="15.0" right="15.0" top="10.0" />
               </HBox.margin>
            </Button>
         </children>
      </HBox>
      <AnchorPane fx:id="pane_login" layoutX="6.0" layoutY="106.0" prefHeight="277.0" prefWidth="500.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
         <children>
            <TextField fx:id="txt_username" layoutX="27.0" layoutY="78.0" prefHeight="40.0" prefWidth="450.0" promptText="Enter UserName">
               <font>
                  <Font name="Times New Roman Bold" size="18.0" />
               </font>
            </TextField>
            <PasswordField fx:id="txt_password" layoutX="27.0" layoutY="141.0" prefHeight="40.0" prefWidth="450.0">
               <font>
                  <Font name="Times New Roman Bold" size="24.0" />
               </font>
            </PasswordField>
            <ComboBox fx:id="type" layoutX="257.0" layoutY="8.0" prefHeight="40.0" prefWidth="218.0" promptText="Chose Type" />
            <Button fx:id="btn_login" layoutX="50.0" layoutY="203.0" mnemonicParsing="false" onAction="#Login" prefHeight="51.0" prefWidth="403.0" style="-fx-background-color: #0fea88;" text="Login" textFill="WHITE">
               <font>
                  <Font name="Times New Roman Bold" size="28.0" />
               </font>
            </Button>
         </children>
      </AnchorPane>
      <AnchorPane fx:id="pane_signup" layoutY="123.0" prefHeight="288.0" prefWidth="500.0" visible="false" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
         <children>
            <TextField fx:id="txt_username_up" layoutX="272.0" layoutY="69.0" prefHeight="30.0" prefWidth="200.0" promptText="Enter Your Username" />
            <TextField fx:id="txt_password_up" layoutX="272.0" layoutY="122.0" prefHeight="30.0" prefWidth="200.0" promptText="Enter Your Password" />
            <TextField fx:id="email_up" layoutX="272.0" layoutY="173.0" prefHeight="30.0" prefWidth="200.0" promptText="Enter Your Email" />
            <ComboBox fx:id="type_up" layoutX="272.0" layoutY="24.0" prefHeight="30.0" prefWidth="200.0" promptText="Chose Type" />
            <Button layoutX="25.0" layoutY="228.0" mnemonicParsing="false" onAction="#add_users" prefHeight="40.0" prefWidth="450.0" style="-fx-background-color: #0fea88;" text="SignUp" textFill="WHITE">
               <font>
                  <Font name="Times New Roman Bold" size="27.0" />
               </font>
            </Button>
            <ImageView fitHeight="176.0" fitWidth="218.0" layoutX="38.0" layoutY="24.0" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@signup2.png" />
               </image>
            </ImageView>
         </children>
      </AnchorPane>
   </children>
</AnchorPane>



Class CPanelController.java 

package login;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;

/**
 * FXML Controller class
 *
 * @author amir
 */
public class CPanelController implements Initializable {

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }      
}

CPanel.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane id="AnchorPane" prefHeight="451.0" prefWidth="629.0" style="-fx-background-color: red;" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/11.0.1" fx:controller="login.CPanelController">
   <children>
      <Label layoutX="57.0" layoutY="142.0" prefHeight="198.0" prefWidth="549.0" text="Welcom , Cpanel" textFill="WHITE">
         <font>
            <Font name="Times New Roman Bold" size="69.0" />
         </font>
      </Label>
   </children>
</AnchorPane>


COMMENTS

Name

CSS,3,HTML,3,Java,59,JavaFX,59,JFoenix,15,PHP,4,Scene Builder,29,SQL,9,
ltr
item
Code Amir: JavaFX Scene Builder Login and SignUp page design with MySQL Database Integration
JavaFX Scene Builder Login and SignUp page design with MySQL Database Integration
https://i.ytimg.com/vi/RLbMKWej1_Q/0.jpg
https://i.ytimg.com/vi/RLbMKWej1_Q/0.jpg
Code Amir
https://codebyamir.blogspot.com/2020/04/javafx-scene-builder-login-and-signup.html
https://codebyamir.blogspot.com/
https://codebyamir.blogspot.com/
https://codebyamir.blogspot.com/2020/04/javafx-scene-builder-login-and-signup.html
true
405304041427069011
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy