-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySQLDBCon.java
More file actions
64 lines (58 loc) · 1.89 KB
/
MySQLDBCon.java
File metadata and controls
64 lines (58 loc) · 1.89 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
import java.sql.*;
import java.util.ArrayList;
public class MySQLDBCon {
public Connection conn;
private boolean isConnected;
private ArrayList<Expense> expenses;
public MySQLDBCon() {
expenses = new ArrayList<>();
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/room_expenses", "room_exp", "12345");
isConnected = true;
} catch (Exception e) {
// System.out.println(e);
isConnected = false;
}
}
public ArrayList<Expense> getAllExpenses() {
try {
Statement stmt = conn.createStatement();
ResultSet res = stmt.executeQuery("SELECT id, amount, description, purchased_at FROM expenses");
while (res.next()) {
Expense exp = new Expense(res.getFloat(2), res.getString(3));
expenses.add(exp);
}
} catch (Exception e) {
// System.out.println(e);
}
return expenses;
}
public int addNewExpense(Expense exp) {
String sql = "INSERT INTO expenses (`description`, `amount`, `purchased_at`, `purchased_by`) VALUES (?,?,NOW(), 1)";
int retID = 0;
try {
PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
stmt.setString(1, exp.getDescription());
stmt.setFloat(2, exp.getAmout());
stmt.executeUpdate();
ResultSet res = stmt.getGeneratedKeys();
if (res.next()) {
retID = res.getInt(1);
}
} catch (Exception e) {
// System.out.println(e);
if(!isConnected){
expenses.add(exp);
}
}
return retID;
}
public void closeConnexction() {
try {
conn.close();
} catch (Exception e) {
System.out.println(e);
}
}
}