-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccountService.java
More file actions
38 lines (32 loc) · 1.06 KB
/
AccountService.java
File metadata and controls
38 lines (32 loc) · 1.06 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
package banking.service;
import java.sql.*;
public class AccountService {
private Connection conn;
public AccountService(Connection conn) {
this.conn = conn;
}
public double getBalance(int accountId) {
try {
String sql = "SELECT balance FROM accounts WHERE account_id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, accountId);
ResultSet rs = ps.executeQuery();
if (rs.next()) return rs.getDouble("balance");
} catch (SQLException e) {
e.printStackTrace();
}
return 0.0;
}
public boolean deposit(int accountId, double amount) {
try {
String sql = "UPDATE accounts SET balance = balance + ? WHERE account_id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setDouble(1, amount);
ps.setInt(2, accountId);
return ps.executeUpdate() > 0;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
}