Attempt to fix database not being set

This commit is contained in:
zontreck 2024-04-23 23:48:28 -07:00
parent a3d4ae321e
commit b534287c51
2 changed files with 7 additions and 7 deletions

View file

@ -53,7 +53,7 @@ mod_name=Zontreck's Library Mod
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=GPLv3 mod_license=GPLv3
# The mod version. See https://semver.org/ # The mod version. See https://semver.org/
mod_version=1201.13.042324.2339 mod_version=1201.13.042324.2345
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. # The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources. # This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html # See https://maven.apache.org/guides/mini/guide-naming-conventions.html

View file

@ -33,36 +33,36 @@ public class DatabaseWrapper {
public static void start() { public static void start() {
instance = new DatabaseWrapper(); instance = new DatabaseWrapper();
try { try {
instance.connect(ServerConfig.database.host, ServerConfig.database.user, ServerConfig.database.password); instance.connect(ServerConfig.database.host, ServerConfig.database.user, ServerConfig.database.password, ServerConfig.database.database);
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
public void connect(String url, String username, String password) throws SQLException { public void connect(String url, String username, String password, String database) throws SQLException {
try { try {
// Try MariaDB JDBC driver // Try MariaDB JDBC driver
Class.forName("org.mariadb.jdbc.Driver"); Class.forName("org.mariadb.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mariadb://" + url, username, password); connection = DriverManager.getConnection("jdbc:mariadb://" + url + "/" + database, username, password);
} catch (ClassNotFoundException | SQLException e) { } catch (ClassNotFoundException | SQLException e) {
// MariaDB not found or failed to connect, try MySQL // MariaDB not found or failed to connect, try MySQL
LibZontreck.LOGGER.warn("Failed to connect via MariaDB: " + e.getMessage() + "; Attempting to fall back to mysql"); LibZontreck.LOGGER.warn("Failed to connect via MariaDB: " + e.getMessage() + "; Attempting to fall back to mysql");
try { try {
Class.forName("com.mysql.cj.jdbc.Driver"); Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://" + url, username, password); connection = DriverManager.getConnection("jdbc:mysql://" + url + "/" + database, username, password);
} catch (ClassNotFoundException | SQLException ex) { } catch (ClassNotFoundException | SQLException ex) {
// MySQL not found or failed to connect, try SQLite // MySQL not found or failed to connect, try SQLite
try { try {
Class.forName("com.mysql.jdbc.Driver"); Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://" + url, username, password); connection = DriverManager.getConnection("jdbc:mysql://" + url + "/" + database, username, password);
} catch (ClassNotFoundException | SQLException ex1) { } catch (ClassNotFoundException | SQLException ex1) {
LibZontreck.LOGGER.warn("Failed to connect via MySQL: " + e.getMessage() + "; " + ex1.getMessage() + "; Attempting to fall back to sqlite"); LibZontreck.LOGGER.warn("Failed to connect via MySQL: " + e.getMessage() + "; " + ex1.getMessage() + "; Attempting to fall back to sqlite");
try { try {
Class.forName("org.sqlite.JDBC"); Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:" + url); connection = DriverManager.getConnection("jdbc:sqlite:" + database + ".db");
} catch (ClassNotFoundException | SQLException exc) { } catch (ClassNotFoundException | SQLException exc) {
LibZontreck.LOGGER.error("Failed to connect via SQLite: " + e.getMessage() + "; If you require the use of the block queues, please check the above warnings for explanation on cause. It could be that you do not have the relevant JDBC installed in your server mods list."); LibZontreck.LOGGER.error("Failed to connect via SQLite: " + e.getMessage() + "; If you require the use of the block queues, please check the above warnings for explanation on cause. It could be that you do not have the relevant JDBC installed in your server mods list.");
} }