-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBUtils.java
172 lines (164 loc) · 6.06 KB
/
DBUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package com.example.loanproject;
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import java.sql.*;
import javafx.stage.Stage;
import java.io.IOException;
import java.util.Objects;
public class DBUtils {
public static void changeScene(ActionEvent event, String fxmlFile, String title, String username) {
Parent root = null;
if (username != null) {
try {
FXMLLoader loader = new FXMLLoader(DBUtils.class.getResource(fxmlFile));
root = loader.load();
LoginController loginController = loader.getController();
LoginController.setUserInformation(username);
} catch (IOException e) {
e.printStackTrace();
}
} else {
try {
root = FXMLLoader.load(DBUtils.class.getResource(fxmlFile));
}
catch (IOException e) {
e.printStackTrace();
}
}
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
stage.setTitle(title);
stage.setScene(new Scene(root));
stage.show();
}
public static void signUpUser(ActionEvent event, String username, String password) {
Connection connection = null;
PreparedStatement psInsert = null;
PreparedStatement psCheckUserExists = null;
ResultSet resultSet = null;
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost:3305/loanproject" ,"root","Ashokp1201");
psCheckUserExists = connection.prepareStatement("SELECT * FROM users WHERE username =?");
psCheckUserExists.setString(1, username);
resultSet = psCheckUserExists.executeQuery();
if (resultSet.isBeforeFirst()) {
System.out.println("User already Exists ");
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setContentText("You cannot use this username.");
alert.show();
} else {
psInsert = connection.prepareStatement("INSERT INTO users (username,password) VALUES (?,?)");
psInsert.setString(1, username);
psInsert.setString(2, password);
psInsert.executeUpdate();
changeScene(event, "logged-in.fxml", "Home", username);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (resultSet != null) {
try {
resultSet.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
if (psCheckUserExists != null) {
try {
psCheckUserExists.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (psInsert != null) {
try {
psInsert.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public static void logInUser(ActionEvent event, String username ,String password)
{
Connection connection =null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost:3305/loanproject","root","Ashokp1201");
preparedStatement =connection.prepareStatement("SELECT password FROM users WHERE username= ?");
preparedStatement.setString(1,username);
resultSet = preparedStatement.executeQuery();
if (!resultSet.isBeforeFirst())
{
System.out.println("User not found ");
Alert alert =new Alert(Alert.AlertType.ERROR);
alert.setContentText("Provided credential are incorrect");
alert.show();
}
else{
while (resultSet.next())
{
String retrievedPassword = resultSet.getString("password");
if (retrievedPassword.equals(password))
{
changeScene(event,"logged-in.fxml","Home",username);
}
else {
System.out.println("Passwords did not match!");
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setContentText("The provided credentials are incorrect");
alert.show();
}
}
}
} catch (SQLException e)
{
e.printStackTrace();
}
finally {
if(resultSet != null)
{
try {
resultSet.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
if(preparedStatement != null)
{
try{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
}