-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deposit.java
44 lines (36 loc) · 1.69 KB
/
Deposit.java
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
import javax.swing.JOptionPane;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Deposit {
static void deposit(int accountNumber){
String depositValueString = JOptionPane.showInputDialog(null, "Type the deposit's value");
int depositValue = Integer.parseInt(depositValueString);
try {
String sql = "SELECT * from account";
Statement stt = Connect.connection.createStatement();
ResultSet res = stt.executeQuery(sql);
while(res.next()){
int account_number = res.getInt(1);
double currentBalance = res.getDouble("balance");
if(account_number == accountNumber){
double newBalance = currentBalance + depositValue;
String setNewBalance = "UPDATE account SET balance = ? WHERE id = %d";
String formattedString = String.format(setNewBalance, accountNumber);
PreparedStatement preparedStatement = Connect.connection.prepareStatement(formattedString);
preparedStatement.setDouble(1, newBalance);
int response = preparedStatement.executeUpdate();
if(response > 0){
Main.accounBalance = newBalance;
String dialog = "%d balance updated!";
JOptionPane.showMessageDialog(null, String.format(dialog, accountNumber));
}
}
}
} catch (SQLException e) {
e.printStackTrace();
}
Main.isLogged();
}
}