Javafx database driven login script demo.





import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
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.stage.Stage;
import javafx.scene.control.TextField;
import javafx.scene.control.PasswordField;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;

public class LoginScript extends Application
{
    PreparedStatement ps;
    Connection conn;
    ResultSet rs;
    public static void main(String[]args)
    {
        Application.launch(args);
    }
    @Override
    public void start(Stage primaryStage)
    {
        BorderPane bp = new BorderPane();
        Scene scene = new Scene(bp,600,400);
        
        //creating the grid
        GridPane grid = new GridPane();
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setAlignment(Pos.CENTER);
        
        //placing the grid in the borderpane
        bp.setCenter(grid);
        
        //creating elements to be on the grid
        Text title = new Text("Please Login Below.");
        title.setFill(Color.TOMATO);
        title.setFont(Font.font("TAHOMA", FontWeight.BOLD,20));
        grid.add(title,0,0,2,1);
        
        Label usernameL = new Label("Username:");
        grid.add(usernameL,0,1);
        //creating the usernamefield.
        TextField usernameTF = new TextField();
        usernameTF.setPromptText("Enter Your Username");
        usernameTF.setPrefWidth(250);
        usernameTF.setPrefHeight(20);
        grid.add(usernameTF,1,1);
        
        Label passwordL = new Label("Password:");
        grid.add(passwordL,0,2);
        
        //creating the passwordfield
        PasswordField passwordF = new PasswordField();
        passwordF.setPrefWidth(250);
        passwordF.setPrefHeight(20);
        grid.add(passwordF,1,2);
        
        //creating the login button
        Button loginB = new Button("login");
        HBox hbBtn = new HBox(10);
        hbBtn.getChildren().add(loginB);
        grid.add(hbBtn,2,4);
        
        //setting login action to the button
        loginB.setOnAction((ActionEvent t) -> {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                 conn = DriverManager.getConnection("jdbc:mysql://localhost/address","root","kevo");//my database connection string my database name is address username is root and password is kevo
                 String username = usernameTF.getText();
                 String password = passwordF.getText();
                 PreparedStatement ps = conn.prepareStatement("SELECT * FROM account WHERE username='"+username+"'AND password='"+password+"'");
                 rs = ps.executeQuery();
                 int count = 0;
                 while(rs.next())
                 {
                     count = count + 1;
                 }
                 if(count==1)
                 {
                     Alert info = new Alert(AlertType.INFORMATION);
                     info.setTitle("Success Dialog");
                     info.setHeaderText("Dialog For Successful Login!");
                     info.setContentText("Login Was Successful");
                     info.showAndWait();
                 }
                 else if(count>1)
                 {
                     Alert warning = new Alert(AlertType.WARNING);
                     warning.setTitle("Warning Dialog");
                     warning.setHeaderText("Dialog For Access Denied");
                     warning.setContentText("Access Denied!!Duplicate User Detecetd.");
                     warning.showAndWait();
                 }
                 else
                 {
                     Alert err = new Alert(AlertType.WARNING);
                     err.setTitle("Error Dialog");
                     err.setHeaderText("Dialog For Access Denied");
                     err.setContentText("Access Denied!!Such Account Does Not Exist!!");
                     err.showAndWait();
                 }
                 conn.close();
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(LoginScript.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(LoginScript.class.getName()).log(Level.SEVERE, null, ex);
            }
        });
        primaryStage.setTitle("Login Script!");
        primaryStage.setScene(scene);
        primaryStage.show();
        
    }
}

The code above gives a result like this:






Comments

Popular posts from this blog

Javafx adding icons to menu and menuitem.

Javafx displaying image from database.

Javafx inserting uploaded image into database.