Advertisement
Guest User

Untitled

a guest
Apr 18th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. package application;
  2.  
  3. import java.sql.*;
  4. import com.mysql.jdbc.Connection;
  5. import javafx.application.Application;
  6. import javafx.event.ActionEvent;
  7. import javafx.event.EventHandler;
  8. import javafx.scene.Scene;
  9. import javafx.scene.control.Button;
  10. import javafx.scene.control.Label;
  11. import javafx.scene.control.PasswordField;
  12. import javafx.scene.control.TextField;
  13. import javafx.scene.layout.GridPane;
  14. import javafx.scene.layout.HBox;
  15. import javafx.scene.layout.Pane;
  16. import javafx.scene.text.Text;
  17. import javafx.stage.Stage;
  18. public class Main extends Application {
  19.  
  20. private Connection conn = null;
  21. private PreparedStatement psLog, psReg = null;
  22. private ResultSet rsLog = null;
  23.  
  24. private Scene frontpage, home, login, register;
  25. private Stage window;
  26. private Button btnLogin, btnReg, btnSubmit, btnRegister;
  27. private PasswordField passwordLoginTf, passwordRegtf;
  28. private TextField usernameLoginTf, usernameRegtf;
  29. private Text statusText;
  30.  
  31. public Connection connect() throws Exception {
  32. try {
  33. Class.forName("com.mysql.jdbc.Driver");
  34. conn = (Connection) DriverManager.getConnection(
  35. "jdbc:mysql://localhost:3306/slutprojekt?autoReconnect=true&useSSL=false", "root", "");
  36. return conn;
  37. } catch (Exception e) {
  38. e.printStackTrace();
  39. } return null;
  40. }
  41.  
  42.  
  43. public void loginQuery() {
  44. btnSubmit.addEventHandler(ActionEvent.ACTION, new EventHandler<ActionEvent>() {
  45. @Override
  46. public void handle(ActionEvent actionEvent) {
  47. try {
  48. psLog = conn.prepareStatement("SELECT * FROM account WHERE username "
  49. + "= ? and password = ?");
  50. psLog.setString(1, usernameLoginTf.getText());
  51. psLog.setString(2, passwordLoginTf.getText());
  52. rsLog = psLog.executeQuery();
  53. if (rsLog.next()) btnSubmit.setOnAction(e -> window.setScene(frontpage));
  54. else statusText.setText("Wrong credentials");
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. }
  58. }
  59. });
  60. }
  61.  
  62. public void regQuery() {
  63. btnRegister.addEventHandler(ActionEvent.ACTION, new EventHandler<ActionEvent>() {
  64. @Override
  65. public void handle(ActionEvent actionEvent) {
  66. try {
  67. psReg = conn.prepareStatement("INSERT INTO account (username, password) VALUES (?,?)");
  68. psReg.setString(1, usernameRegtf.getText());
  69. psReg.setString(2, passwordRegtf.getText());
  70. psReg.executeUpdate();
  71. btnRegister.setOnAction(e -> window.setScene(frontpage));
  72. } catch (Exception e) {
  73. e.printStackTrace();
  74. }
  75. }});
  76. }
  77.  
  78. public Scene frontpage() {
  79. if (home == null) {
  80. GridPane gridfp = new GridPane();
  81. gridfp.setHgap(10);
  82. gridfp.setVgap(10);
  83. HBox hboxfp = new HBox();
  84. hboxfp.setSpacing(10.0);
  85. Label lblfp = new Label("Welcome to ?");
  86. btnLogin = new Button("Click to login");
  87. btnReg = new Button("Click to register");
  88. hboxfp.getChildren().addAll(btnLogin, btnReg, lblfp);
  89. gridfp.add(lblfp, 5, 2);
  90. gridfp.add(btnLogin, 5, 3);
  91. gridfp.add(btnReg, 5, 4);
  92. frontpage = new Scene(gridfp, 400, 400);
  93. }
  94. return frontpage;
  95. }
  96.  
  97. public Scene login() {
  98. if (login == null) {
  99. GridPane gridLogin = new GridPane();
  100. gridLogin.setHgap(10);
  101. gridLogin.setVgap(10);
  102. HBox hboxLogin = new HBox();
  103. hboxLogin.setSpacing(10.0);
  104. Label lblLogin = new Label("Please login");
  105. btnSubmit = new Button("Submit");
  106. Label usernameLoginlbl = new Label("Username: ");
  107. Label passwordLoginlbl = new Label("Password: ");
  108. statusText = new Text();
  109. usernameLoginTf = new TextField();
  110. passwordLoginTf = new PasswordField();
  111. hboxLogin.getChildren().addAll(btnSubmit, usernameLoginlbl, passwordLoginlbl,
  112. statusText, usernameLoginTf, passwordLoginTf, lblLogin);
  113. gridLogin.add(lblLogin, 2, 1);
  114. gridLogin.add(usernameLoginlbl, 2, 2);
  115. gridLogin.add(usernameLoginTf, 3, 2);
  116. gridLogin.add(passwordLoginlbl, 2, 3);
  117. gridLogin.add(passwordLoginTf, 3, 3);
  118. gridLogin.add(btnSubmit, 3, 4);
  119. gridLogin.add(statusText, 3, 5);
  120. login = new Scene(gridLogin, 400, 400);
  121. }
  122. return login;
  123. }
  124.  
  125. public Scene register() {
  126. if (register == null) {
  127. GridPane gridReg = new GridPane();
  128. gridReg.setHgap(10);
  129. gridReg.setVgap(10);
  130. HBox hboxregScene = new HBox();
  131. hboxregScene.setSpacing(10.0);
  132. Label lblReg = new Label("Register here");
  133. Label lblRegusername = new Label("Username: ");
  134. usernameRegtf = new TextField();
  135. passwordRegtf = new PasswordField();
  136. Label passwordLabel2 = new Label("Password: ");
  137. btnRegister = new Button("Register");
  138. hboxregScene.getChildren().addAll(lblReg, lblRegusername, usernameRegtf,
  139. passwordRegtf, passwordLabel2, btnRegister);
  140. gridReg.add(lblReg, 2, 1);
  141. gridReg.add(lblRegusername, 1, 2);
  142. gridReg.add(usernameRegtf, 2, 2);
  143. gridReg.add(passwordLabel2, 1, 3);
  144. gridReg.add(passwordRegtf, 2, 3);
  145. gridReg.add(btnRegister, 2, 4);
  146. register = new Scene(gridReg, 400, 400);
  147. }
  148. return register;
  149. }
  150.  
  151. public Scene home() {
  152. if (home == null) {
  153. TextField tfld = new TextField("TExt");
  154. Pane root = new Pane();
  155. root.getChildren().add(tfld);
  156. home = new Scene(root, 400, 400);
  157. }
  158. return home;
  159. }
  160.  
  161. @Override
  162. public void start(Stage primaryStage) throws Exception {
  163.  
  164. connect();
  165. frontpage();
  166. login();
  167. register();
  168. loginQuery();
  169. regQuery();
  170.  
  171. btnLogin.setOnAction(e -> window.setScene(login));
  172. btnReg.setOnAction(e -> window.setScene(register));
  173.  
  174. window = primaryStage;
  175. window.setScene(frontpage);
  176. window.show();
  177. window.setTitle("An application");
  178. }
  179.  
  180. public static void main(String[] args) {
  181. launch(args);
  182. }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement