the_big_one/src/main/java/jesse/keeblarcraft/ConfigMgr/SQLConfig.java

121 lines
4.0 KiB
Java

package jesse.keeblarcraft.ConfigMgr;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import jesse.keeblarcraft.Utils.CommonStructures.Pair;
public class SQLConfig {
private static SQLConfig static_inst;
private String dbName;
private String dbUser;
private String dbPass;
private String dbAddr;
private Boolean connected = false;
private Connection conn = null; // Actual connection object
public static SQLConfig GetInstance() {
if (static_inst == null) {
static_inst = new SQLConfig();
}
return static_inst;
}
public SQLConfig() {
connected = false;
try {
// According to some random online tutorial; this loads the driver!
Class.forName("com.mysql.cj.jdbc.Driver");
connected = true;
} catch (Exception e) {
System.out.println("Could not find the proper SQL JDBC Drivers. Cannot connect to SQL Config");
e.printStackTrace();
}
if (connected) {
try {
dbName = GeneralConfig.GetInstance().GetSQLDatabase();
dbUser = GeneralConfig.GetInstance().GetSQLUsername();
dbPass = GeneralConfig.GetInstance().GetSQLPassword();
dbAddr = "jdbc:mysql://" + GeneralConfig.GetInstance().GetSQLAddress() + "/" + dbName;
conn = DriverManager.getConnection(dbAddr, dbUser, dbPass);
Statement stmnt = conn.createStatement();
String testSql = "SELECT * FROM test_table";
ResultSet rs = stmnt.executeQuery(testSql);
System.out.println("Printing out result set from test query");
while (rs.next()) {
System.out.println("[RS]: " + rs.getString("test_name"));
}
} catch (SQLException e) {}
}
}
private ResultSet RunCommand(String sqlCommand) {
ResultSet cmdResult = null;
if (conn != null) {
try {
Statement statement = conn.createStatement();
cmdResult = statement.executeQuery(sqlCommand);
} catch (Exception e) {}
}
return cmdResult;
}
// Re-attempt the connection
public Boolean Connect() {
if (conn != null) {
try {
conn.close();
} catch (Exception e) {}
}
Boolean success = false;
try {
conn = DriverManager.getConnection(dbAddr, dbUser, dbPass);
success = true;
} catch (Exception e) {}
return success;
}
public Boolean IsConnected() {
return conn != null && this.connected;
}
private Boolean TableExists(String name) {
boolean tableExists = false;
try (ResultSet rs = conn.getMetaData().getTables(null, null, name, null)) {
while (rs.next()) {
String tName = rs.getString("TABLE_NAME");
if (tName != null && tName.equals(name)) {
tableExists = true;
break;
}
}
} catch (SQLException e) {}
return tableExists;
}
// Might fix heap pollution decay in future with enum types or something. For now we assume the user isn't horrifically stupid and will give a SQL-able type
public Boolean CreateTable(String tableName, Pair<String, Object>... columnPairs) {
Boolean success = false;
if (!TableExists(tableName.toUpperCase())) {
String sqlCommand = "CREATE TABLE " + tableName.toUpperCase() + "(";
for (Pair<String, Object> colPair : columnPairs) {
sqlCommand = sqlCommand + " " + colPair.GetKey() + " " + String.valueOf(colPair.GetValue()) + ",";
}
System.out.println("DEBUG STATEMENT, SQL STATEMENT: " + sqlCommand);
ResultSet rs = RunCommand(sqlCommand);
}
return success;
}
}