Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. public class DBUtilities {
  2. // Getting connection to the database.
  3. public static Connection getConnection() throws SQLException {
  4. String url = "jdbc:mysql://localhost:3306/studentmanagementdb";
  5. String user = "admin";
  6. String pass = "administrator";
  7. Connection connection = DriverManager.getConnection(url, user, pass);
  8. return connection;
  9. }
  10. // Close prepared statement.
  11. public static void closePreparedStatement(PreparedStatement pStmt) {
  12. try {
  13. if(pStmt != null) {
  14. pStmt.close();
  15. }
  16. }catch(SQLException sExepction) {
  17. showErrorMsg("SQL Database Error:", "Prepared Statement could not be closed.");
  18. }
  19. }
  20. // Close result set.
  21. public static void closeResultSet(ResultSet resSet) {
  22. try {
  23. if (resSet != null){
  24. resSet.close();
  25. }
  26. }catch (SQLException sException) {
  27. showErrorMsg("SQL Database Error:", "Result Set could not be closed.");
  28. }
  29. }
  30. public static void closeConnection(Connection connect) {
  31. try {
  32. if(connect != null) {
  33. connect.close();
  34. }
  35. }catch (SQLException sException) {
  36. showErrorMsg("SQL Database Error:", "Database Connection could not be close.");
  37. }
  38. }
  39. // Shows error message.
  40. public static void showErrorMsg(String title, String msg) {
  41. Platform.runLater(() -> {
  42. Alert alert = new Alert(Alert.AlertType.ERROR);
  43. alert.setTitle(title);
  44. alert.setHeaderText(msg);
  45. alert.setWidth(200);
  46. alert.setHeight(200);
  47. alert.showAndWait();
  48. });
  49. }
  50. }
  51.  
  52. @FXML
  53. private TextField txtUserName;
  54. @FXML
  55. private PasswordField txtPassword;
  56. @FXML
  57. private Button btnLogin;
  58.  
  59. Connection connection = null;
  60. PreparedStatement preparedStatement = null;
  61. ResultSet resultSet = null;
  62. DBUtilities dbUtil = new DBUtilities();
  63.  
  64.  
  65.  
  66. // Setting the login button.
  67. @FXML
  68. private void setBtnLogin(ActionEvent event) {
  69.  
  70. try {
  71. connection = dbUtil.getConnection();
  72. String sqlQuery = "SELECT * FROM 'User_Login_Details' WHERE 'User_Name' = ? AND 'User_Password' = ?";
  73. connection.prepareStatement(sqlQuery);
  74. preparedStatement.setString(1, txtUserName.getText());
  75. preparedStatement.setString(2, txtPassword.getText());
  76. resultSet = preparedStatement.executeQuery();
  77.  
  78. }catch (Exception exception) {
  79. //dbUtilities.showErrorMsg("Database Connection Error:", "Could not connect to the database.");
  80. exception.printStackTrace();
  81. }finally {
  82. dbUtil.closePreparedStatement(preparedStatement);
  83. dbUtil.closeResultSet(resultSet);
  84. dbUtil.closeConnection(connection);
  85. }
  86. }
  87. @Override
  88. public void initialize(URL location, ResourceBundle resources) {
  89. btnLogin.setOnAction(this::setBtnLogin);
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement