Repository navigation
Expand file tree
/
Copy pathDatabaseManager.java
More file actions
103 lines (93 loc) · 3.66 KB
/
Copy pathDatabaseManager.java
File metadata and controls
103 lines (93 loc) · 3.66 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
import java.sql.*;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
public class DatabaseManager {
private static DatabaseManager instance;
private Connection connection;
private static final String DB_URL = "jdbc:sqlite:expenses.db";
private DatabaseManager() {
try {
connection = DriverManager.getConnection(DB_URL);
createTable();
} catch (SQLException e) {
System.err.println("Database connection error: " + e.getMessage());
}
}
public static DatabaseManager getInstance() {
if (instance == null) {
instance = new DatabaseManager();
}
return instance;
}
private void createTable() throws SQLException {
String sql = "CREATE TABLE IF NOT EXISTS expenses (id INTEGER PRIMARY KEY AUTOINCREMENT, category TEXT NOT NULL, amount REAL NOT NULL, description TEXT, date TEXT NOT NULL)";
try (Statement stmt = connection.createStatement()) {
stmt.execute(sql);
}
}
public void addExpense(Expense expense) throws SQLException {
String sql = "INSERT INTO expenses (category, amount, description, date) VALUES (?, ?, ?, ?)";
try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
pstmt.setString(1, expense.getCategory());
pstmt.setDouble(2, expense.getAmount());
pstmt.setString(3, expense.getDescription());
pstmt.setString(4, expense.getDate().toString());
pstmt.executeUpdate();
}
}
public List<Expense> getAllExpenses() throws SQLException {
List<Expense> expenses = new ArrayList<>();
String sql = "SELECT * FROM expenses ORDER BY date DESC";
try (Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
expenses.add(new Expense(
rs.getInt("id"),
rs.getString("category"),
rs.getDouble("amount"),
rs.getString("description"),
LocalDate.parse(rs.getString("date"))
));
}
}
return expenses;
}
public void updateExpense(Expense expense) throws SQLException {
String sql = "UPDATE expenses SET category = ?, amount = ?, description = ?, date = ? WHERE id = ?";
try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
pstmt.setString(1, expense.getCategory());
pstmt.setDouble(2, expense.getAmount());
pstmt.setString(3, expense.getDescription());
pstmt.setString(4, expense.getDate().toString());
pstmt.setInt(5, expense.getId());
pstmt.executeUpdate();
}
}
public void deleteExpense(int id) throws SQLException {
String sql = "DELETE FROM expenses WHERE id = ?";
try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
pstmt.setInt(1, id);
pstmt.executeUpdate();
}
}
public double getTotalExpenses() throws SQLException {
String sql = "SELECT SUM(amount) as total FROM expenses";
try (Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
if (rs.next()) {
return rs.getDouble("total");
}
}
return 0.0;
}
public void close() {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
System.err.println("Error closing connection: " + e.getMessage());
}
}
}