1. import java.sql.*;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.concurrent.Executors;
  7. import java.util.concurrent.ScheduledExecutorService;
  8. import java.util.concurrent.TimeUnit;
  9. public class RecordWatcher {
  10. private static final String DB_URL = "your_database_url"; // Replace with your database URL
  11. private static final String DB_USER = "your_username"; // Replace with your username
  12. private static final String DB_PASSWORD = "your_password"; // Replace with your password
  13. private static final String TABLE_NAME = "your_table_name"; // Replace with your table name
  14. private static final String PRIMARY_KEY_COLUMN = "id"; // Replace with your primary key column
  15. private static List<Map<String, Object>> lastRecords; // Store the last known records
  16. private static final int CHECK_INTERVAL_SECONDS = 5; // Check every 5 seconds
  17. private static final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
  18. public static void main(String[] args) {
  19. // Initialize the lastRecords list
  20. lastRecords = new ArrayList<>();
  21. // Start the scheduler
  22. scheduler.scheduleAtFixedRate(RecordWatcher::checkForChanges, 0, CHECK_INTERVAL_SECONDS, TimeUnit.SECONDS);
  23. }
  24. private static void checkForChanges() {
  25. try (Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
  26. // Get all records from the table
  27. String sql = "SELECT * FROM " + TABLE_NAME;
  28. try (ResultSet resultSet = connection.createStatement().executeQuery(sql)) {
  29. List<Map<String, Object>> currentRecords = new ArrayList<>();
  30. // Convert ResultSet to List of Maps
  31. while (resultSet.next()) {
  32. Map<String, Object> record = new HashMap<>();
  33. for (int i = 1; i <= resultSet.getPriorCount(); i++) {
  34. record.put(resultSet.getMetaData().getColumnName(i), resultSet.getObject(i));
  35. }
  36. currentRecords.add(record);
  37. }
  38. // Check for changes
  39. if (!currentRecords.equals(lastRecords)) {
  40. System.out.println("Changes detected!");
  41. // Process the changes here (e.g., log them, send notifications)
  42. processChanges(lastRecords, currentRecords);
  43. lastRecords = currentRecords; // Update lastRecords
  44. }
  45. } catch (SQLException e) {
  46. logError("Error accessing database: " + e.getMessage());
  47. }
  48. } catch (SQLException e) {
  49. logError("Error connecting to database: " + e.getMessage());
  50. }
  51. }
  52. private static void processChanges(List<Map<String, Object>> oldRecords, List<Map<String, Object>> newRecords) {
  53. // Example: Print the changes
  54. System.out.println("Old records size: " + oldRecords.size());
  55. System.out.println("New records size: " + newRecords.size());
  56. //Find added records
  57. List<Map<String, Object>> addedRecords = new ArrayList<>();
  58. for (Map<String, Object> record : newRecords) {
  59. if (!oldRecords.contains(record)) {
  60. addedRecords.add(record);
  61. }
  62. }
  63. if (addedRecords.size() > 0) {
  64. System.out.println("Added records:");
  65. for (Map<String, Object> record : addedRecords) {
  66. System.out.println(record);
  67. }
  68. }
  69. //Find removed records
  70. List<Map<String, Object>> removedRecords = new ArrayList<>();
  71. for (Map<String, Object> record : oldRecords) {
  72. if (!newRecords.contains(record)) {
  73. removedRecords.add(record);
  74. }
  75. }
  76. if (removedRecords.size() > 0) {
  77. System.out.println("Removed records:");
  78. for (Map<String, Object> record : removedRecords) {
  79. System.out.println(record);
  80. }
  81. }
  82. }
  83. private static void logError(String message) {
  84. System.err.println("Error: " + message);
  85. }
  86. }

Add your comment