import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class RecordWatcher {
private static final String DB_URL = "your_database_url"; // Replace with your database URL
private static final String DB_USER = "your_username"; // Replace with your username
private static final String DB_PASSWORD = "your_password"; // Replace with your password
private static final String TABLE_NAME = "your_table_name"; // Replace with your table name
private static final String PRIMARY_KEY_COLUMN = "id"; // Replace with your primary key column
private static List<Map<String, Object>> lastRecords; // Store the last known records
private static final int CHECK_INTERVAL_SECONDS = 5; // Check every 5 seconds
private static final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
public static void main(String[] args) {
// Initialize the lastRecords list
lastRecords = new ArrayList<>();
// Start the scheduler
scheduler.scheduleAtFixedRate(RecordWatcher::checkForChanges, 0, CHECK_INTERVAL_SECONDS, TimeUnit.SECONDS);
}
private static void checkForChanges() {
try (Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
// Get all records from the table
String sql = "SELECT * FROM " + TABLE_NAME;
try (ResultSet resultSet = connection.createStatement().executeQuery(sql)) {
List<Map<String, Object>> currentRecords = new ArrayList<>();
// Convert ResultSet to List of Maps
while (resultSet.next()) {
Map<String, Object> record = new HashMap<>();
for (int i = 1; i <= resultSet.getPriorCount(); i++) {
record.put(resultSet.getMetaData().getColumnName(i), resultSet.getObject(i));
}
currentRecords.add(record);
}
// Check for changes
if (!currentRecords.equals(lastRecords)) {
System.out.println("Changes detected!");
// Process the changes here (e.g., log them, send notifications)
processChanges(lastRecords, currentRecords);
lastRecords = currentRecords; // Update lastRecords
}
} catch (SQLException e) {
logError("Error accessing database: " + e.getMessage());
}
} catch (SQLException e) {
logError("Error connecting to database: " + e.getMessage());
}
}
private static void processChanges(List<Map<String, Object>> oldRecords, List<Map<String, Object>> newRecords) {
// Example: Print the changes
System.out.println("Old records size: " + oldRecords.size());
System.out.println("New records size: " + newRecords.size());
//Find added records
List<Map<String, Object>> addedRecords = new ArrayList<>();
for (Map<String, Object> record : newRecords) {
if (!oldRecords.contains(record)) {
addedRecords.add(record);
}
}
if (addedRecords.size() > 0) {
System.out.println("Added records:");
for (Map<String, Object> record : addedRecords) {
System.out.println(record);
}
}
//Find removed records
List<Map<String, Object>> removedRecords = new ArrayList<>();
for (Map<String, Object> record : oldRecords) {
if (!newRecords.contains(record)) {
removedRecords.add(record);
}
}
if (removedRecords.size() > 0) {
System.out.println("Removed records:");
for (Map<String, Object> record : removedRecords) {
System.out.println(record);
}
}
}
private static void logError(String message) {
System.err.println("Error: " + message);
}
}
Add your comment