-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManager.java
More file actions
97 lines (79 loc) · 2.53 KB
/
Manager.java
File metadata and controls
97 lines (79 loc) · 2.53 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
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class Manager {
private Connection con;
private Statement stmt;
public Manager() throws SQLException{
String url = "jdbc:mysql://kc-sce-appdb01.kc.umkc.edu/slnz8b";
String userID = "slnz8b";
String password = "tZrFLVzffV";
try {
Class.forName("com.mysql.jdbc.Driver");
} catch(java.lang.ClassNotFoundException e) {
System.out.println(e);
System.exit(0);
}
con = DriverManager.getConnection(url,userID,password);
stmt = con.createStatement();
}
public void transfer (int fromAccnt, int toAccount, double amount){
try {
con.setAutoCommit(false);
double debitor = (getBalance(fromAccnt) - amount);
double creditor = (getBalance(toAccount) + amount);
String debit = SQLthings.modifyBalance(fromAccnt, debitor);
String credit = SQLthings.modifyBalance(toAccount, creditor);
stmt.executeUpdate(debit);
stmt.executeUpdate(credit);
con.commit();
con.setAutoCommit(true);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e.toString(), "Database update error.", JOptionPane.ERROR_MESSAGE);
}
}
public int getBalance(int accntNum){
int balance = 100;
ResultSet temp;
String tableBalance = SQLthings.getBalance(accntNum);
try {
temp = stmt.executeQuery(tableBalance);
temp.next();
balance = temp.getInt(1);
} catch (SQLException err) {
JOptionPane.showMessageDialog(null, err.toString(), "Database query contained an error.", JOptionPane.ERROR_MESSAGE);
}
return balance;
}
public ArrayList refreshTable() {
ResultSet tempRS = null;
String refresh = SQLthings.populateTable();
ArrayList arr = new ArrayList();
try {
tempRS = stmt.executeQuery(refresh);
java.sql.ResultSetMetaData rsmd = tempRS.getMetaData();
int numberColumns = rsmd.getColumnCount();
boolean next = tempRS.next();
while (next) {
int j = 1;
for (int i = 1; i <= numberColumns; i++) {
String tempString = tempRS.getString(i);
// integer columns
if (i == 1 || i == 3) {
arr.add((tempString));
} else
arr.add((tempString));
}
j++;
next = tempRS.next();
}
} catch (SQLException err) {
JOptionPane.showMessageDialog(null, err.toString(), "Database error.", JOptionPane.ERROR_MESSAGE);
}
return arr;
}
}