5/7/25, 9:43 PM TransactionDb.
java
src/main/java/tham/seven/[Link]
1 /*
2 * Mei Tham
3 * AP CSA
4 * Mar 5, 2025
5 * 7th Period
6 * Database
7 */
8
9 package [Link];
10
11 import [Link];
12 import [Link];
13 import [Link];
14 import [Link];
15 import [Link];
16 import [Link];
17 import [Link];
18 import [Link];
19
20 public class TransactionDb {
21 private static final String URL = "jdbc:mysql://localhost:3306/MasterProject";
22 private static final String USER = "root";
23 private static final String PASSWORD = "thamfamily0";
24 private Connection connection;
25
26 public TransactionDb() {
27 try {
28 connection = [Link](URL, USER, PASSWORD);
29 [Link]("Connected to database successfully.");
30 } catch (SQLException e) {
31 [Link]();
32 }
33 }
34
35 public List<Budget> getAllTransactions() {
36 List<Budget> transactions = new ArrayList<>();
localhost:62762/eb320015-fde1-4e72-b120-11eb2abbfe1b/ 1/3
5/7/25, 9:43 PM [Link]
37 String query = "SELECT * FROM budget"; // Assuming the table name is 'budget', keep it as is for the query
38 try (Statement stmt = [Link](); ResultSet rs = [Link](query)) {
39 while ([Link]()) {
40 [Link](new Budget(
41 [Link]("id"),
42 [Link]("date"),
43 [Link]("amount"),
44 [Link]("category"),
45 [Link]("description"),
46 [Link]("inBudget")
47 ));
48 }
49 } catch (SQLException e) {
50 [Link]();
51 }
52 return transactions;
53 }
54
55 public void addTransaction(String date, double amount, String category, String description, boolean inBudget)
{
56 String query = "INSERT INTO budget (date, amount, category, description, inBudget) VALUES (?, ?, ?, ?,
?)";
57 try (PreparedStatement pstmt = [Link](query)) {
58 [Link](1, date);
59 [Link](2, amount);
60 [Link](3, category);
61 [Link](4, description);
62 [Link](5, inBudget);
63 [Link]();
64 } catch (SQLException e) {
65 [Link]();
66 }
67 }
68
69 public void updateTransaction(int id, String date, double amount, String category, String description, boolean
inBudget) {
70 String query = "UPDATE budget SET date = ?, amount = ?, category = ?, description = ?, inBudget = ? WHERE
id = ?";
localhost:62762/eb320015-fde1-4e72-b120-11eb2abbfe1b/ 2/3
5/7/25, 9:43 PM [Link]
71 try (PreparedStatement pstmt = [Link](query)) {
72 [Link](1, date);
73 [Link](2, amount);
74 [Link](3, category);
75 [Link](4, description);
76 [Link](5, inBudget);
77 [Link](6, id);
78 [Link]();
79 } catch (SQLException e) {
80 [Link]();
81 }
82 }
83
84 public void clearAllTransactions
() {
85 String query = "DELETE FROM budget";
86 try (PreparedStatement pstmt = [Link](query)) {
87 [Link]();
88 [Link]("All transactions cleared for new month.");
89 } catch (SQLException e) {
90 [Link]();
91 }
92 }
93
94
95
96 public void deleteTransaction(int id) {
97 String query = "DELETE FROM budget WHERE id = ?";
98 try (PreparedStatement pstmt = [Link](query)) {
99 [Link](1, id);
100 [Link]();
101 } catch (SQLException e) {
102 [Link]();
103 }
104 }
105 }
106
localhost:62762/eb320015-fde1-4e72-b120-11eb2abbfe1b/ 3/3