From 3b07c687c4ea31c22a9fac1dd9aec014e6273762 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Sat, 22 Oct 2022 16:37:32 +0200 Subject: [PATCH] Implement pronouns table --- .../wtf/beatrice/nounspicker/NounsPicker.java | 2 +- .../nounspicker/utils/DatabaseManager.java | 36 ++++++++++++++++--- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/main/java/wtf/beatrice/nounspicker/NounsPicker.java b/src/main/java/wtf/beatrice/nounspicker/NounsPicker.java index 19e1385..036ea4f 100644 --- a/src/main/java/wtf/beatrice/nounspicker/NounsPicker.java +++ b/src/main/java/wtf/beatrice/nounspicker/NounsPicker.java @@ -53,7 +53,7 @@ public class NounsPicker extends JavaPlugin logger.log("Connecting to database..."); String dbFilePath = getDataFolder().getAbsolutePath() + File.separator + "db.sqlite"; DatabaseManager dbManager = new DatabaseManager(dbFilePath); - if(dbManager.initialize()) + if(dbManager.connect() && dbManager.initDb()) { logger.log("Database connection initialized!"); } else { diff --git a/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java b/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java index 55e9415..f66cbc9 100644 --- a/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java +++ b/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java @@ -3,6 +3,7 @@ package wtf.beatrice.nounspicker.utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; +import java.sql.Statement; public class DatabaseManager { @@ -18,10 +19,25 @@ public class DatabaseManager logger = new ConsoleLogger(getClass()); } - public boolean initialize() + public boolean connect() { String url = sqliteURL.replace("%path%", dbPath); + if(!close()) return false; + + try { + dbConnection = DriverManager.getConnection(url); + logger.log("Database connection established!"); + return true; + } catch (SQLException e) { + logger.err(e.getMessage()); + return false; + } + + } + + public boolean close() + { if (dbConnection != null) { try { @@ -37,16 +53,26 @@ public class DatabaseManager dbConnection = null; } + return true; + } - try { - dbConnection = DriverManager.getConnection(url); - logger.log("Database connection established!"); - return true; + public boolean initDb() + { + String sql = "CREATE TABLE IF NOT EXISTS pronouns (" + + "player text NOT NULL," + + "main_pronoun text," + + "secondary_pronoun text" + + ");"; + + try (Statement stmt = dbConnection.createStatement()) { + // create a new table + stmt.execute(sql); } catch (SQLException e) { logger.err(e.getMessage()); return false; } + return true; } }