98 lines
3.7 KiB
Java
98 lines
3.7 KiB
Java
package vip.fubuki.playersync.util;
|
|
|
|
import vip.fubuki.playersync.config.JdbcConfig;
|
|
|
|
import java.sql.*;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import com.mojang.logging.LogUtils;
|
|
|
|
public class JDBCsetUp {
|
|
|
|
private static final Logger LOGGER = LogUtils.getLogger();
|
|
|
|
/**
|
|
* Returns a connection to the MySQL server.
|
|
* @param selectDatabase if true, the returned URL includes the configured database name.
|
|
* @return a Connection object with the database explicitly selected.
|
|
* @throws SQLException if a database access error occurs.
|
|
*/
|
|
public static Connection getConnection(boolean selectDatabase) throws SQLException {
|
|
String dbName = JdbcConfig.DATABASE_NAME.get();
|
|
// Build the base URL
|
|
String url = "jdbc:mysql://" + JdbcConfig.HOST.get() + ":" + JdbcConfig.PORT.get();
|
|
if (selectDatabase && dbName != null && !dbName.isEmpty()) {
|
|
url += "/" + dbName;
|
|
}
|
|
url += "?useUnicode=true&characterEncoding=utf-8&useSSL=" + JdbcConfig.USE_SSL.get()
|
|
+ "&serverTimezone=UTC&allowPublicKeyRetrieval=true";
|
|
Connection conn = DriverManager.getConnection(url, JdbcConfig.USERNAME.get(), JdbcConfig.PASSWORD.get());
|
|
// Ensure that the connection uses the desired database by explicitly issuing "USE dbName"
|
|
if (selectDatabase && dbName != null && !dbName.isEmpty()) {
|
|
try (Statement st = conn.createStatement()) {
|
|
st.execute("USE " + dbName);
|
|
}
|
|
}
|
|
return conn;
|
|
}
|
|
|
|
// Default connection always includes the database.
|
|
public static Connection getConnection() throws SQLException {
|
|
return getConnection(true);
|
|
}
|
|
|
|
/**
|
|
* Executes a query using a connection that includes the database.
|
|
*/
|
|
public static QueryResult executeQuery(String sql) throws SQLException {
|
|
LOGGER.trace(sql);
|
|
Connection connection = getConnection(); // With database selected (and "USE" already run)
|
|
PreparedStatement queryStatement = connection.prepareStatement(sql);
|
|
ResultSet resultSet = queryStatement.executeQuery();
|
|
return new QueryResult(connection, resultSet);
|
|
}
|
|
|
|
/**
|
|
* Executes an update using a connection that includes the database.
|
|
*/
|
|
public static void executeUpdate(String sql) throws SQLException {
|
|
LOGGER.trace(sql);
|
|
try (Connection connection = getConnection()) { // With database selected
|
|
try (PreparedStatement updateStatement = connection.prepareStatement(sql)) {
|
|
updateStatement.executeUpdate();
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Executes an update using a connection that does NOT include a default database.
|
|
* This method is used for commands like "CREATE DATABASE IF NOT EXISTS ..."
|
|
*/
|
|
public static void executeUpdate(String sql, int dummy) throws SQLException {
|
|
LOGGER.trace(sql);
|
|
try (Connection connection = getConnection(false)) { // Without default database
|
|
try (PreparedStatement updateStatement = connection.prepareStatement(sql)) {
|
|
updateStatement.executeUpdate();
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* A helper method for updates with parameters.
|
|
*/
|
|
public static void update(String sql, String... argument) throws SQLException {
|
|
LOGGER.trace(sql);
|
|
try (Connection connection = getConnection()) { // With database selected
|
|
PreparedStatement updateStatement = connection.prepareStatement(sql);
|
|
for (int i = 0; i < argument.length; i++) {
|
|
updateStatement.setString(i + 1, argument[i]);
|
|
}
|
|
updateStatement.executeUpdate();
|
|
}
|
|
}
|
|
|
|
public record QueryResult(Connection connection, ResultSet resultSet) {
|
|
}
|
|
}
|