LoginPage Create in javafx Using Mysql


Tutorial-- > 1
Topic: LoginPage Create in javafx


Login Page Digene code (Using JavaFX fxml) 
We using scene builderfor this degine


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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="500.0" prefWidth="720.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.LoginController">
    <children>
        <ImageView fitHeight="478.0" fitWidth="720.0" layoutX="1.0" layoutY="2.0" pickOnBounds="true" preserveRatio="true">
            <image>
                <Image url="@../Pic/doc.png" />
            </image>
        </ImageView>
        <ImageView fitHeight="91.0" fitWidth="234.0" layoutX="5.0" layoutY="13.0" pickOnBounds="true" preserveRatio="true">
            <image>
                <Image url="@../Pic/logindd.jpg" />
            </image>
        </ImageView>
        <ImageView fitHeight="151.0" fitWidth="263.0" layoutX="3.0" layoutY="348.0" pickOnBounds="true" preserveRatio="true">
            <image>
                <Image url="@../Pic/login_pic.gif" />
            </image>
        </ImageView>
        <TextField fx:id="txtUser" layoutX="52.0" layoutY="125.0" promptText="Enter User Email" />
        <PasswordField fx:id="txtPassword" layoutX="52.0" layoutY="160.0" promptText="Enter Password" />
        <CheckBox layoutX="18.0" layoutY="189.0" mnemonicParsing="false" text="Remenrber Password" underline="true" />
        <Button layoutX="152.0" layoutY="209.0" mnemonicParsing="false" onAction="#Login" text="Login" />
        <Button layoutX="18.0" layoutY="211.0" mnemonicParsing="false" prefHeight="4.0" prefWidth="103.0" text="Forget Password " underline="true">
            <font>
                <Font size="10.0" />
            </font>
        </Button>
        <ImageView fitHeight="148.0" fitWidth="171.0" layoutX="559.0" layoutY="347.0" pickOnBounds="true" preserveRatio="true">
            <image>
                <Image url="@../Pic/hms.png" />
            </image>
        </ImageView>
        <Button layoutX="654.0" layoutY="14.0" mnemonicParsing="false" onAction="#LogintoSignup" text="SignUp" />
        <ImageView fitHeight="25.0" fitWidth="30.0" layoutX="16.0" layoutY="125.0" pickOnBounds="true" preserveRatio="true">
            <image>
                <Image url="@../Pic/people.png" />
            </image>
        </ImageView>
        <ImageView fitHeight="25.0" fitWidth="25.0" layoutX="16.0" layoutY="158.0" pickOnBounds="true" preserveRatio="true">
            <image>
                <Image url="@../Pic/lock.png" />
            </image>
        </ImageView>
    </children>
</Pane>




Login Controler Page Code:

package sample;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class LoginController {

    @FXML
    private TextField txtUser;

    @FXML
    private TextField txtPassword;

    public void Login(ActionEvent even) throws Exception{

        String email = txtUser.getText();
        String password = txtPassword.getText();

        if(check(email, password)){
            ((Node)even.getSource()).getScene().getWindow().hide();

            Stage primaryStage = new Stage();
            Parent root = FXMLLoader.load(getClass().getResource("AdminPanel.fxml")); 
                                                                
                                                                     // You Call a new fxml file )

            primaryStage.setTitle("Hospital Management System");
            primaryStage.setScene(new Scene(root, 720, 500));
            primaryStage.show();
        }
        else {
               //   you print any this item in your choose
        }
    }

    public static boolean check(String email, String password){

        PreparedStatement pst = null;
        ResultSet rs = null;
        String query = "select email,pass from admin where email=? and pass=?";

        try{

            Class.forName("com.mysql.jdbc.Driver");          // mysql driver name default
            Connection con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/admin?useSSL=false"," ? "," ? ");  
                                                                                           //mysql usename and password
            pst = con.prepareStatement(query);
            pst.setString(1,email);
            pst.setString(2,password);
            rs = pst.executeQuery();

            if(rs.next()){
                return true;
            }
            else{
                return false;
            }
        }catch (Exception e){

            return false;
        }
    }

}


Sample Output in Login Page:




Video Link:




Scene builder Download Link (Click Me)

Post a Comment

0 Comments