-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserDAO.java
More file actions
140 lines (128 loc) · 5.52 KB
/
Copy pathUserDAO.java
File metadata and controls
140 lines (128 loc) · 5.52 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
import java.sql.*;
public class UserDAO {
private static final String DB_URL = "jdbc:mysql://localhost:3306/romeobank";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "Bhargav@2208";
// Establish a database connection
private Connection connect() throws SQLException {
return DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
}
// Add a new user to the database
public void addUser(String username, String password, int age, String email) {
String sql = "INSERT INTO users (username, password, age, email, balance) VALUES (?, ?, ?, ?, ?)";
try (Connection conn = connect(); PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, username);
stmt.setString(2, password);
stmt.setInt(3, age);
stmt.setString(4, email);
stmt.setDouble(5, 0.0); // Initial balance is 0.0
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
// Authenticate a user by username and password
public int authenticateUser(String username, String password) {
String sql = "SELECT id FROM users WHERE username = ? AND password = ?";
try (Connection conn = connect(); PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, username);
stmt.setString(2, password);
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
return rs.getInt("id");
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return -1; // Authentication failed
}
// Get the balance of a user account
public double getBalance(int accountId) {
String sql = "SELECT balance FROM users WHERE id = ?";
try (Connection conn = connect(); PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, accountId);
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
return rs.getDouble("balance");
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return -1; // Account not found
}
// Deposit an amount to a user's account
public void deposit(int accountId, double amount) {
String sql = "UPDATE users SET balance = balance + ? WHERE id = ?";
try (Connection conn = connect(); PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setDouble(1, amount);
stmt.setInt(2, accountId);
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
// Withdraw an amount from a user's account
public boolean withdraw(int accountId, double amount) {
String sql = "UPDATE users SET balance = balance - ? WHERE id = ? AND balance >= ?";
try (Connection conn = connect(); PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setDouble(1, amount);
stmt.setInt(2, accountId);
stmt.setDouble(3, amount);
int rowsUpdated = stmt.executeUpdate();
return rowsUpdated > 0; // True if withdrawal was successful
} catch (SQLException e) {
e.printStackTrace();
}
return false; // Withdrawal failed
}
// Get user by username
public int getUserByUsername(String username) {
String sql = "SELECT id FROM users WHERE username = ?";
try (Connection conn = connect(); PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, username);
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
return rs.getInt("id");
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return -1; // User not found
}
// Transfer funds from one user to another
public boolean transferFunds(int fromAccountId, int toAccountId, double amount) {
String withdrawSql = "UPDATE users SET balance = balance - ? WHERE id = ? AND balance >= ?";
String depositSql = "UPDATE users SET balance = balance + ? WHERE id = ?";
try (Connection conn = connect()) {
conn.setAutoCommit(false); // Start transaction
try (PreparedStatement withdrawStmt = conn.prepareStatement(withdrawSql);
PreparedStatement depositStmt = conn.prepareStatement(depositSql)) {
// Withdraw funds from sender
withdrawStmt.setDouble(1, amount);
withdrawStmt.setInt(2, fromAccountId);
withdrawStmt.setDouble(3, amount);
int rowsUpdated = withdrawStmt.executeUpdate();
if (rowsUpdated == 0) {
conn.rollback(); // Rollback if insufficient balance
return false;
}
// Deposit funds to receiver
depositStmt.setDouble(1, amount);
depositStmt.setInt(2, toAccountId);
depositStmt.executeUpdate();
conn.commit(); // Commit transaction
return true;
} catch (SQLException e) {
conn.rollback(); // Rollback transaction on error
e.printStackTrace();
return false;
}
} catch (SQLException e) {
e.printStackTrace();
}
return false; // Transfer failed
}
}