· 9 years ago · Jan 19, 2017, 06:44 PM
1//Loads an already existing database or creates one if one does not already exist and then loads it.
2 public Database() throws SQLException{
3
4 connection = DriverManager.getConnection(dbUrl, dbUsername, dbPassword);
5
6 //Create the database if it doesn't already exist.
7 main.getLogger().debug("Attempting to create a database, if one does not exist yet.");
8
9 String makeDatabaseCommand = "CREATE DATABASE IF NOT EXISTS ?";
10 PreparedStatement statement = connection.prepareStatement(makeDatabaseCommand);
11 statement.setString(1, dbName);
12
13 main.getLogger().debug("Running prepared statement " + statement.toString());
14 statement.executeUpdate();
15
16
17 statement.close();
18 connection.commit();
19
20 main.getLogger().debug("Previous command ran successfully and we will begin structuring the database (if necessary).");
21
22 main.getLogger().debug("Attempting to structure database in case it does not have the required tables yet.");
23
24 String command = "CREATE TABLE IF NOT EXISTS ? ( "
25 + "? ?," //UUID of the player; Used as the ID
26 + "? ?)"; //Pre-shared key we generate at registration for TOTP Authentication
27
28 PreparedStatement tableCreationStatement = connection.prepareStatement(command);
29 tableCreationStatement.setString(1, userTableName);
30 tableCreationStatement.setString(2, userTableIDColumn);
31 tableCreationStatement.setString(3, userTableIDColumnType);
32 tableCreationStatement.setString(4, userTableTotpPSKColumn);
33 tableCreationStatement.setString(5, userTableTotpPSKColumnType);
34
35 //Full command should look something like: CREATE TABLE IF NOT EXISTS EA_TOTP_TABLE (USERNAME TEXT, PSK TEXT)
36
37 main.getLogger().debug("Running command: " + tableCreationStatement.toString());
38
39 tableCreationStatement.executeUpdate();
40
41 tableCreationStatement.close();
42 connection.commit();
43
44 }