-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBConnection.java
More file actions
82 lines (68 loc) · 2.42 KB
/
DBConnection.java
File metadata and controls
82 lines (68 loc) · 2.42 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package database;
import java.sql.*;
/**
* Manages the SQLite database connection and schema initialization.
*
* Dependencies: sqlite-jdbc-3.x.x.jar (add to classpath)
* Download: https://github.com/xerial/sqlite-jdbc/releases
*/
public class DBConnection {
private static final String DB_URL = "jdbc:sqlite:notevault.db";
private static Connection connection;
/**
* Returns the singleton database connection.
*/
public static Connection getConnection() {
try {
Class.forName("org.sqlite.JDBC"); // IMPORTANT
return DriverManager.getConnection("jdbc:sqlite:notevault.db");
} catch (Exception e) {
throw new RuntimeException("Failed to connect to database", e);
}
}
/**
* Creates the schema if it doesn't exist.
* Call this once at startup from Main.java.
*/
public static void initialize() {
String usersTable = "CREATE TABLE IF NOT EXISTS users (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT," +
"username TEXT NOT NULL UNIQUE," +
"password_hash TEXT NOT NULL," +
"first_name TEXT NOT NULL," +
"last_name TEXT NOT NULL," +
"created_at DATETIME DEFAULT (datetime('now'))" +
");";
String notesTable = "CREATE TABLE IF NOT EXISTS notes (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT," +
"user_id INTEGER NOT NULL," +
"title TEXT NOT NULL," +
"body TEXT NOT NULL," +
"category TEXT DEFAULT 'Personal'," +
"created_at DATETIME DEFAULT (datetime('now'))," +
"updated_at DATETIME DEFAULT (datetime('now'))," +
"FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE" +
");";
try (Connection conn = getConnection();
Statement stmt = conn.createStatement()) {
stmt.execute("PRAGMA journal_mode=WAL;");
stmt.execute(usersTable);
stmt.execute(notesTable);
System.out.println("INIT FIX APPLIED");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* Closes the connection (call on app shutdown).
*/
public static void close() {
try {
if (connection != null && !connection.isClosed()) {
connection.close();
}
} catch (SQLException e) {
System.err.println("Error closing connection: " + e.getMessage());
}
}
}