Class Database
java.lang.Object
Database
Database helper utilities used by the application for H2 persistence.
This class centralizes connection handling, simple CRUD helpers and transaction execution helpers. It is intentionally minimal and synchronous to match the small desktop application's usage pattern.
H2 database details
- JDBC URL:
jdbc:h2:./app_home/print_jobs— relative to the repository root; the actual data file isapp_home/print_jobs.mv.db. - Default credentials: user
sawith an empty password (used intentionally for local desktop deployments; secure or change in production). - Backups: The project includes
DatabaseBackupwhich copies the MV store file intoapp_home/backups/. Prefer creating a backup before performing operations that modify the database. - Recovery: Use
DatabaseBackup.restoreBackup(String,String)to restore an archived file intoapp_home/print_jobs.mv.db. Also seerecoverDatabase()which attempts to restore the most recent backup found in the backups folder. - Concurrency: This application uses embedded H2 file mode. Avoid opening the same database file from multiple JVMs concurrently — prefer the single-process desktop usage pattern. When necessary, configure a networked H2 server or use a separate DB server.
- Lock timeout:
connectWithTimeout()sets a 5s lock timeout to reduce UI hangs when a lock is held by another process.
- Since:
- 1.0.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceFunctional interface used by transaction helpers.static interfaceBackwards-compatible alias used in some places in the codebase. -
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateDatabase()Private constructor to prevent instantiation of this utility class. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddLastPrintedDates(int projectId, List<String> dates) Add a collection of last-printed dates for a project in a single transaction.static Connectionconnect()Open a JDBC connection using the configured URL and default credentials.static ConnectionOpen a JDBC connection and configure a shorter lock timeout to reduce UI hangs when another process holds database locks.static voidexecuteTransaction(Database.DatabaseOperation operations) Execute code inside a transaction.static voidSimilar toexecuteTransaction(DatabaseOperation)but logs the error before rethrowing so callers may avoid duplicating logging logic.static intinsertProject(String name, String projectType, String filePath, String description) Insert a new project record and return the generated primary key.static booleanQuick corruption test: attempt a simple query and report failure.static ResultSetloadProjectById(int projectId) Load project record by id.static voidRecover from the most recent backup found in `app_home/backups/`.static voidupdateProject(int projectId, String name, String projectType, String recipient, String tags, String description) Update a project's metadata in the database.static voidApply simple schema updates to ensure optional columns exist.static voidBasic integrity check ensuring required tables exist.
-
Field Details
-
URL
-
-
Constructor Details
-
Database
private Database()Private constructor to prevent instantiation of this utility class.
-
-
Method Details
-
connect
Open a JDBC connection using the configured URL and default credentials.- Returns:
- a new
Connection - Throws:
SQLException- if a connection cannot be established
-
connectWithTimeout
Open a JDBC connection and configure a shorter lock timeout to reduce UI hangs when another process holds database locks.- Returns:
- a configured
Connection - Throws:
SQLException- if a connection cannot be established
-
insertProject
public static int insertProject(String name, String projectType, String filePath, String description) throws SQLException Insert a new project record and return the generated primary key.- Parameters:
name- the project nameprojectType- the project type stringfilePath- absolute filesystem path to project filesdescription- free-text description- Returns:
- generated id for the new project
- Throws:
SQLException- if insertion fails Example:int id = Database.insertProject("Box", "Prototype", "/tmp/box", "Student project");
-
addLastPrintedDates
Add a collection of last-printed dates for a project in a single transaction. Dates must be provided as strings parsable to a timestamp using the pattern yyyy-MM-dd (time portion is set to midnight).- Parameters:
projectId- the project primary keydates- list of date strings in yyyy-MM-dd format- Throws:
SQLException- on DB errors
-
executeTransaction
Execute code inside a transaction. Theoperationslambda receives aConnectionwhich it must use for all statements to ensure they participate in the same transaction.- Parameters:
operations- lambda executed within a transaction- Throws:
SQLException- if the execution or commit fails
-
executeTransactionWithRollback
public static void executeTransactionWithRollback(Database.DatabaseOperation operations) throws SQLException Similar toexecuteTransaction(DatabaseOperation)but logs the error before rethrowing so callers may avoid duplicating logging logic.- Parameters:
operations- transactional operations- Throws:
SQLException- on failure
-
verifyDatabaseIntegrity
public static void verifyDatabaseIntegrity()Basic integrity check ensuring required tables exist. Any missing table will be logged viaErrorHandler. -
isDatabaseCorrupted
public static boolean isDatabaseCorrupted()Quick corruption test: attempt a simple query and report failure.- Returns:
- true when corruption is detected, false otherwise
-
recoverDatabase
public static void recoverDatabase()Recover from the most recent backup found in `app_home/backups/`. This method attempts to locate, verify and restore the latest backup file. -
updateSchema
public static void updateSchema()Apply simple schema updates to ensure optional columns exist. This is idempotent and safe to call at startup. -
loadProjectById
Load project record by id. The returned ResultSet is connected to an openConnectionwhich the caller is responsible for closing. Callers should ensure they close both the ResultSet and the underlying connection.- Parameters:
projectId- project primary key- Returns:
- ResultSet positioned before first row
- Throws:
SQLException- on failure
-
updateProject
public static void updateProject(int projectId, String name, String projectType, String recipient, String tags, String description) throws SQLException Update a project's metadata in the database.- Parameters:
projectId- id of the project to updatename- updated project nameprojectType- updated typerecipient- updated recipienttags- comma-separated tags stringdescription- updated description- Throws:
SQLException- on failure
-