-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseManager.java
More file actions
71 lines (65 loc) · 2.51 KB
/
Copy pathDatabaseManager.java
File metadata and controls
71 lines (65 loc) · 2.51 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
package com.example.securefile;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
* DatabaseManager for MySQL usage.
* Reads connection info from ConfigManager (db.url, db.user, db.password).
*
* This implementation returns a fresh Connection on each getConnection() call.
* Callers should use try-with-resources to close the Connection.
* For production, consider replacing with a connection pool (HikariCP).
*/
public class DatabaseManager {
private static String url;
private static String user;
private static String pass;
/**
* Initialize DB config and ensure tables exist.
* This method acquires a temporary connection to create tables and then closes it.
*/
public static void init(String unusedPath) {
try {
ConfigManager cfg = ConfigManager.loadDefault();
url = cfg.get("db.url");
user = cfg.get("db.user");
pass = cfg.get("db.password");
if (url == null || user == null) {
throw new RuntimeException("Please configure db.url and db.user in config/config.properties");
}
// create tables using a temporary connection
try (Connection temp = DriverManager.getConnection(url, user, pass)) {
ensureTables(temp);
}
} catch (SQLException e) {
throw new RuntimeException("DB init failed: " + e.getMessage(), e);
}
}
private static void ensureTables(Connection conn) throws SQLException {
String createUsers = "CREATE TABLE IF NOT EXISTS accounts (" +
"id INT AUTO_INCREMENT PRIMARY KEY," +
"username VARCHAR(255) UNIQUE NOT NULL," +
"password_hash TEXT NOT NULL," +
"role VARCHAR(50) NOT NULL DEFAULT 'user'" +
")";
try (Statement st = conn.createStatement()) {
st.execute(createUsers);
}
}
/**
* Returns a new Connection. Caller is responsible for closing it.
*/
public static Connection getConnection() throws SQLException {
if (url == null || user == null) {
throw new IllegalStateException("DatabaseManager not initialized. Call DatabaseManager.init(...) first.");
}
return DriverManager.getConnection(url, user, pass);
}
/**
* No-op for this implementation.
*/
public static void close() {
// Nothing to close since we return fresh connections per call.
}
}