Implement pronouns table
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
ccc45333f8
commit
3b07c687c4
@ -53,7 +53,7 @@ public class NounsPicker extends JavaPlugin
|
|||||||
logger.log("Connecting to database...");
|
logger.log("Connecting to database...");
|
||||||
String dbFilePath = getDataFolder().getAbsolutePath() + File.separator + "db.sqlite";
|
String dbFilePath = getDataFolder().getAbsolutePath() + File.separator + "db.sqlite";
|
||||||
DatabaseManager dbManager = new DatabaseManager(dbFilePath);
|
DatabaseManager dbManager = new DatabaseManager(dbFilePath);
|
||||||
if(dbManager.initialize())
|
if(dbManager.connect() && dbManager.initDb())
|
||||||
{
|
{
|
||||||
logger.log("Database connection initialized!");
|
logger.log("Database connection initialized!");
|
||||||
} else {
|
} else {
|
||||||
|
@ -3,6 +3,7 @@ package wtf.beatrice.nounspicker.utils;
|
|||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.DriverManager;
|
import java.sql.DriverManager;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Statement;
|
||||||
|
|
||||||
public class DatabaseManager
|
public class DatabaseManager
|
||||||
{
|
{
|
||||||
@ -18,10 +19,25 @@ public class DatabaseManager
|
|||||||
logger = new ConsoleLogger(getClass());
|
logger = new ConsoleLogger(getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean initialize()
|
public boolean connect()
|
||||||
{
|
{
|
||||||
String url = sqliteURL.replace("%path%", dbPath);
|
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)
|
if (dbConnection != null)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
@ -37,16 +53,26 @@ public class DatabaseManager
|
|||||||
dbConnection = null;
|
dbConnection = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
try {
|
|
||||||
dbConnection = DriverManager.getConnection(url);
|
|
||||||
logger.log("Database connection established!");
|
|
||||||
return true;
|
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) {
|
} catch (SQLException e) {
|
||||||
logger.err(e.getMessage());
|
logger.err(e.getMessage());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user