forked from elenaxenouu/BookIt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserDAO.java
More file actions
149 lines (123 loc) · 2.97 KB
/
Copy pathUserDAO.java
File metadata and controls
149 lines (123 loc) · 2.97 KB
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
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
* UserDAO provides all the necessary methods related to users.
*
* @author
*
*/
public class UserDAO {
/**
* This method returns a List with all Users
*
* @return List<User>
*/
public List<User> getUsers() throws Exception {
List<User> users = new ArrayList<User>();
DB db = new DB();
Connection con = null;
String query = "SELECT * FROM users;";
try {
con = db.getConnection();
PreparedStatement stmt = con.prepareStatement(query);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
users.add (new User(rs.getString("name"),
rs.getInt("availableHours"),
rs.getService("selectedService")
));
}
rs.close();
stmt.close();
db.close();
return users;
} catch (Exception e) {
throw new Exception(e.getMessage());
} finally {
try {
db.close();
} catch (Exception e) {
}
}
} //End of getUsers
/**
* Search user by username
*
* @param username, String
* @return User, the User object or null
* @throws Exception
*/
public User findUser(String username) throws Exception {
DB db = new DB();
Connection con = null;
String query = "SELECT * FROM users_ex3_8220148_2024_2025 WHERE username=?;";
try {
con = db.getConnection();
PreparedStatement stmt = con.prepareStatement(query);
stmt.setString(1, username);
ResultSet rs = stmt.executeQuery();
if (!rs.next()) {
rs.close();
stmt.close();
db.close();
return null;
}
User user = new User(rs.getString("firstname"),
rs.getString("lastname"),
rs.getString("email"),
rs.getString("username"),
rs.getString("password"));
rs.close();
stmt.close();
db.close();
return user;
} catch (Exception e) {
throw new Exception(e.getMessage());
}finally {
try {
db.close();
}catch (Exception e) {
}
}
} //End of findUser
/**
* This method is used to authenticate a user.
*
* @param username, String
* @param password, String
* @return User, the User object
* @throws Exception, if the credentials are not valid
*/
public User authenticate(String username, String password) throws Exception {
DB db = new DB();
Connection con = null;
String query = "SELECT * FROM users WHERE username=? AND password=?;";
try {
con = db.getConnection();
PreparedStatement stmt =con.prepareStatement(query);
stmt.setString(1, username);
stmt.setString(2, password);
ResultSet rs = stmt.executeQuery();
if (!rs.next()) {
rs.close();
stmt.close();
db.close();
throw new Exception("Wrong username or password");
}
User user = new User(rs.getString("name"),
rs.getString("password"));
rs.close();
stmt.close();
db.close();
return user;
} catch (Exception e) {
throw new Exception(e.getMessage());
}finally {
try {
db.close();
}catch (Exception e) {
}
}
} //End of authenticate
} //End of class