-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReminderManager.java
More file actions
55 lines (48 loc) · 2.1 KB
/
Copy pathReminderManager.java
File metadata and controls
55 lines (48 loc) · 2.1 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
import java.sql.*;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.*;
public class ReminderManager {
private Timer timer;
public void startReminderChecker() {
timer = new Timer(true);
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
checkReminders();
}
}, 0, 30000);
}
private void checkReminders() {
String sql = "SELECT * FROM reminders WHERE is_completed = FALSE AND reminder_datetime <= NOW()";
try (Connection conn = DatabaseConnection.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
String title = rs.getString("title");
String message = rs.getString("message");
int id = rs.getInt("reminder_id");
JOptionPane.showMessageDialog(null, message, "REMINDER: " + title, JOptionPane.INFORMATION_MESSAGE);
String update = "UPDATE reminders SET is_completed = TRUE WHERE reminder_id = ?";
try (PreparedStatement pstmt = conn.prepareStatement(update)) {
pstmt.setInt(1, id);
pstmt.executeUpdate();
}
}
} catch (SQLException e) {
System.out.println("Error: " + e.getMessage());
}
}
public static void addReminder(String title, String datetime, String message) {
String sql = "INSERT INTO reminders (title, reminder_datetime, message) VALUES (?, ?, ?)";
try (Connection conn = DatabaseConnection.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, title);
pstmt.setString(2, datetime);
pstmt.setString(3, message);
pstmt.executeUpdate();
JOptionPane.showMessageDialog(null, "Reminder added!");
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Error: " + e.getMessage());
}
}
}