-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthService.java
More file actions
48 lines (41 loc) · 1.36 KB
/
AuthService.java
File metadata and controls
48 lines (41 loc) · 1.36 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
package banking.service;
import banking.model.User;
import java.sql.*;
public class AuthService {
private Connection conn;
public AuthService(Connection conn) {
this.conn = conn;
}
public boolean registerUser(String username, String password) {
try {
String query = "INSERT INTO users (username, password) VALUES (?, ?)";
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setString(1, username);
stmt.setString(2, password);
stmt.executeUpdate();
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public User login(String username, String password) {
try {
String sql = "SELECT * FROM users WHERE username=? AND password=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, password);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
return new User(
rs.getInt("user_id"),
rs.getString("username"),
rs.getString("role")
);
}
} catch (SQLException e) {
e.printStackTrace();
}
return null; // login failed
}
}