Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package application;
- import java.sql.*;
- import com.mysql.jdbc.Connection;
- import javafx.application.Application;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.control.PasswordField;
- import javafx.scene.control.TextField;
- import javafx.scene.layout.GridPane;
- import javafx.scene.layout.HBox;
- import javafx.scene.layout.Pane;
- import javafx.scene.text.Text;
- import javafx.stage.Stage;
- public class Main extends Application {
- private Connection conn = null;
- private PreparedStatement psLog, psReg = null;
- private ResultSet rsLog = null;
- private Scene frontpage, home, login, register;
- private Stage window;
- private Button btnLogin, btnReg, btnSubmit, btnRegister;
- private PasswordField passwordLoginTf, passwordRegtf;
- private TextField usernameLoginTf, usernameRegtf;
- private Text statusText;
- public Connection connect() throws Exception {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- conn = (Connection) DriverManager.getConnection(
- "jdbc:mysql://localhost:3306/slutprojekt?autoReconnect=true&useSSL=false", "root", "");
- return conn;
- } catch (Exception e) {
- e.printStackTrace();
- } return null;
- }
- public void loginQuery() {
- btnSubmit.addEventHandler(ActionEvent.ACTION, new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent actionEvent) {
- try {
- psLog = conn.prepareStatement("SELECT * FROM account WHERE username "
- + "= ? and password = ?");
- psLog.setString(1, usernameLoginTf.getText());
- psLog.setString(2, passwordLoginTf.getText());
- rsLog = psLog.executeQuery();
- if (rsLog.next()) btnSubmit.setOnAction(e -> window.setScene(frontpage));
- else statusText.setText("Wrong credentials");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- public void regQuery() {
- btnRegister.addEventHandler(ActionEvent.ACTION, new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent actionEvent) {
- try {
- psReg = conn.prepareStatement("INSERT INTO account (username, password) VALUES (?,?)");
- psReg.setString(1, usernameRegtf.getText());
- psReg.setString(2, passwordRegtf.getText());
- psReg.executeUpdate();
- btnRegister.setOnAction(e -> window.setScene(frontpage));
- } catch (Exception e) {
- e.printStackTrace();
- }
- }});
- }
- public Scene frontpage() {
- if (home == null) {
- GridPane gridfp = new GridPane();
- gridfp.setHgap(10);
- gridfp.setVgap(10);
- HBox hboxfp = new HBox();
- hboxfp.setSpacing(10.0);
- Label lblfp = new Label("Welcome to ?");
- btnLogin = new Button("Click to login");
- btnReg = new Button("Click to register");
- hboxfp.getChildren().addAll(btnLogin, btnReg, lblfp);
- gridfp.add(lblfp, 5, 2);
- gridfp.add(btnLogin, 5, 3);
- gridfp.add(btnReg, 5, 4);
- frontpage = new Scene(gridfp, 400, 400);
- }
- return frontpage;
- }
- public Scene login() {
- if (login == null) {
- GridPane gridLogin = new GridPane();
- gridLogin.setHgap(10);
- gridLogin.setVgap(10);
- HBox hboxLogin = new HBox();
- hboxLogin.setSpacing(10.0);
- Label lblLogin = new Label("Please login");
- btnSubmit = new Button("Submit");
- Label usernameLoginlbl = new Label("Username: ");
- Label passwordLoginlbl = new Label("Password: ");
- statusText = new Text();
- usernameLoginTf = new TextField();
- passwordLoginTf = new PasswordField();
- hboxLogin.getChildren().addAll(btnSubmit, usernameLoginlbl, passwordLoginlbl,
- statusText, usernameLoginTf, passwordLoginTf, lblLogin);
- gridLogin.add(lblLogin, 2, 1);
- gridLogin.add(usernameLoginlbl, 2, 2);
- gridLogin.add(usernameLoginTf, 3, 2);
- gridLogin.add(passwordLoginlbl, 2, 3);
- gridLogin.add(passwordLoginTf, 3, 3);
- gridLogin.add(btnSubmit, 3, 4);
- gridLogin.add(statusText, 3, 5);
- login = new Scene(gridLogin, 400, 400);
- }
- return login;
- }
- public Scene register() {
- if (register == null) {
- GridPane gridReg = new GridPane();
- gridReg.setHgap(10);
- gridReg.setVgap(10);
- HBox hboxregScene = new HBox();
- hboxregScene.setSpacing(10.0);
- Label lblReg = new Label("Register here");
- Label lblRegusername = new Label("Username: ");
- usernameRegtf = new TextField();
- passwordRegtf = new PasswordField();
- Label passwordLabel2 = new Label("Password: ");
- btnRegister = new Button("Register");
- hboxregScene.getChildren().addAll(lblReg, lblRegusername, usernameRegtf,
- passwordRegtf, passwordLabel2, btnRegister);
- gridReg.add(lblReg, 2, 1);
- gridReg.add(lblRegusername, 1, 2);
- gridReg.add(usernameRegtf, 2, 2);
- gridReg.add(passwordLabel2, 1, 3);
- gridReg.add(passwordRegtf, 2, 3);
- gridReg.add(btnRegister, 2, 4);
- register = new Scene(gridReg, 400, 400);
- }
- return register;
- }
- public Scene home() {
- if (home == null) {
- TextField tfld = new TextField("TExt");
- Pane root = new Pane();
- root.getChildren().add(tfld);
- home = new Scene(root, 400, 400);
- }
- return home;
- }
- @Override
- public void start(Stage primaryStage) throws Exception {
- connect();
- frontpage();
- login();
- register();
- loginQuery();
- regQuery();
- btnLogin.setOnAction(e -> window.setScene(login));
- btnReg.setOnAction(e -> window.setScene(register));
- window = primaryStage;
- window.setScene(frontpage);
- window.show();
- window.setTitle("An application");
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement