|
| 1 | +/** |
| 2 | + * Optional MySQL database integration. |
| 3 | + * Logs file operations and usage if MySQL is available. |
| 4 | + * If not installed, all methods are no-ops. |
| 5 | + * |
| 6 | + * @author Max Rupplin / MEARVK LLC |
| 7 | + * |
| 8 | + * Java was purchased here on Earth. |
| 9 | + * Thanks to Earth and all Her software Developers! |
| 10 | + */ |
| 11 | +package pennywise; |
| 12 | + |
| 13 | +import security.ExceptionHandler; |
| 14 | + |
| 15 | +import java.sql.*; |
| 16 | +import java.util.Date; |
| 17 | + |
| 18 | +public class DatabaseHandler |
| 19 | +{ |
| 20 | + private static final String JDBC_URL = "jdbc:mysql://localhost:3306/imaging"; |
| 21 | + private static final String JDBC_USER = "imaging"; |
| 22 | + private static final String JDBC_PASS = "imaging2024"; |
| 23 | + |
| 24 | + private Connection connection; |
| 25 | + private boolean available; |
| 26 | + |
| 27 | + public DatabaseHandler() |
| 28 | + { |
| 29 | + try |
| 30 | + { |
| 31 | + Class.forName("com.mysql.cj.jdbc.Driver"); |
| 32 | + connection = DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASS); |
| 33 | + available = true; |
| 34 | + ExceptionHandler.info("DatabaseHandler", "MySQL connected."); |
| 35 | + } |
| 36 | + catch (Exception e) |
| 37 | + { |
| 38 | + available = false; |
| 39 | + ExceptionHandler.info("DatabaseHandler", |
| 40 | + "MySQL not available — running without database. (" + e.getMessage() + ")"); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public boolean isAvailable() { return available; } |
| 45 | + |
| 46 | + public void logFile(String originalName, String newName, String fileType, |
| 47 | + Date dateTaken, String sourcePath, String destPath) |
| 48 | + { |
| 49 | + if (!available) return; |
| 50 | + |
| 51 | + try (PreparedStatement ps = connection.prepareStatement( |
| 52 | + "INSERT INTO file_log (original_name, new_name, file_type, date_taken, source_path, dest_path) VALUES (?, ?, ?, ?, ?, ?)")) |
| 53 | + { |
| 54 | + ps.setString(1, originalName); |
| 55 | + ps.setString(2, newName); |
| 56 | + ps.setString(3, fileType); |
| 57 | + ps.setTimestamp(4, dateTaken != null ? new Timestamp(dateTaken.getTime()) : null); |
| 58 | + ps.setString(5, sourcePath); |
| 59 | + ps.setString(6, destPath); |
| 60 | + ps.executeUpdate(); |
| 61 | + } |
| 62 | + catch (SQLException e) |
| 63 | + { |
| 64 | + ExceptionHandler.handle("DatabaseHandler.logFile", e); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public void logUsage(int runCount) |
| 69 | + { |
| 70 | + if (!available) return; |
| 71 | + |
| 72 | + try (PreparedStatement ps = connection.prepareStatement( |
| 73 | + "INSERT INTO usage_log (run_count) VALUES (?)")) |
| 74 | + { |
| 75 | + ps.setInt(1, runCount); |
| 76 | + ps.executeUpdate(); |
| 77 | + } |
| 78 | + catch (SQLException e) |
| 79 | + { |
| 80 | + ExceptionHandler.handle("DatabaseHandler.logUsage", e); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public void close() |
| 85 | + { |
| 86 | + if (connection != null) |
| 87 | + { |
| 88 | + try { connection.close(); } catch (SQLException ignored) {} |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments