-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJdbcAccountRepository.java
More file actions
104 lines (93 loc) · 3.97 KB
/
JdbcAccountRepository.java
File metadata and controls
104 lines (93 loc) · 3.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
package com.example;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JdbcAccountRepository implements AccountRepository {
private final DataSource dataSource;
/**
* Creates a JdbcAccountRepository backed by the provided DataSource.
*/
public JdbcAccountRepository(DataSource dataSource) {
this.dataSource = dataSource;
}
/**
* Checks whether an account exists for the given username and password.
*
* @param username the account name to authenticate
* @param password the password to match for the account
* @return `true` if an account with the specified credentials exists, `false` otherwise
* @throws SQLException if a database access error occurs
*/
@Override
public boolean findByNameAndPassword(String username, String password) throws SQLException {
String sql = "SELECT user_id FROM account WHERE name = ? AND password = ?";
// Använder try-with-resources för att säkerställa att Connection stängs
try (Connection connection = dataSource.getConnection();
PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.setString(1, username);
stmt.setString(2, password);
try (ResultSet rs = stmt.executeQuery()) {
return rs.next();
}
}
}
/**
* Inserts a new account record with the provided personal and credential fields.
*
* @param firstName the account holder's first name
* @param lastName the account holder's last name
* @param ssn the account holder's social security number
* @param password the account password to store
* @param username the account username
* @return the number of rows inserted (1 if the account was created, 0 otherwise)
* @throws SQLException if a database access error occurs
*/
@Override
public int create(String firstName, String lastName, String ssn, String password, String username) throws SQLException {
String sql = "INSERT INTO account (first_name, last_name, ssn, password, name) VALUES (?, ?, ?, ?, ?)";
try (Connection connection = dataSource.getConnection();
PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.setString(1, firstName);
stmt.setString(2, lastName);
stmt.setString(3, ssn);
stmt.setString(4, password);
stmt.setString(5, username);
return stmt.executeUpdate();
}
}
/**
* Update the password for the account with the given user ID.
*
* @param userId the ID of the user whose password will be updated
* @param newPassword the new password value to set
* @return the number of rows affected by the update
* @throws SQLException if a database access error occurs
*/
@Override
public int updatePassword(long userId, String newPassword) throws SQLException {
String sql = "UPDATE account SET password = ? WHERE user_id = ?";
try (Connection connection = dataSource.getConnection();
PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.setString(1, newPassword);
stmt.setLong(2, userId);
return stmt.executeUpdate();
}
}
/**
* Deletes the account with the given user ID.
*
* @param userId the identifier of the account to delete
* @return the number of rows deleted (0 if no matching account was found)
* @throws SQLException if a database access error occurs
*/
@Override
public int delete(long userId) throws SQLException {
String sql = "DELETE FROM account WHERE user_id = ?";
try (Connection connection = dataSource.getConnection();
PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.setLong(1, userId);
return stmt.executeUpdate();
}
}
}