Class MySQLClient
java.lang.Object
fr.hytale.loader.datastorage.MySQLClient
MySQL client for managing remote MySQL database connections.
This class provides a high-level API for interacting with MySQL databases, including connection pooling via HikariCP and common SQL operations.
Example Usage:
// Connect to MySQL
MySQLClient mysql = new MySQLClient("localhost", 3306, "minecraft", "root", "password");
mysql.connect();
// Create table
mysql.execute("CREATE TABLE IF NOT EXISTS players (uuid VARCHAR(36), name VARCHAR(16), coins INT)");
// Insert data
mysql.execute("INSERT INTO players VALUES (?, ?, ?)", uuid, name, 1000);
// Query data
List<Map<String, Object>> results = mysql.query("SELECT * FROM players WHERE uuid = ?", uuid);
// Close connection
mysql.disconnect();
- Since:
- 1.0.5
- Version:
- 1.0.6
- Author:
- HytaleLoader
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classHelper class for transaction statements. -
Constructor Summary
ConstructorsConstructorDescriptionMySQLClient(String host, int port, String database, String username, String password) Creates a new MySQL client. -
Method Summary
Modifier and TypeMethodDescriptionbooleanconnect()Connects to the MySQL server with connection pooling.static booleancreateDatabaseIfNotExists(String host, int port, String database, String username, String password) Creates a database if it doesn't exist.voidDisconnects from the MySQL server and closes the connection pool.intExecutes an SQL statement (INSERT, UPDATE, DELETE, CREATE, etc.).int[]executeBatch(String sql, List<Object[]> paramsList) Executes a batch of SQL statements.com.zaxxer.hikari.HikariDataSourceGets direct access to the HikariCP data source.longExecutes an INSERT and returns the generated key.booleanChecks if the client is connected to MySQL.Executes an SQL query and returns results.Executes a query and returns a single result.queryValue(String sql, Object... params) Executes a query and returns a single value.booleantableExists(String tableName) Checks if a table exists.booleantransaction(MySQLClient.SQLStatement... statements) Executes multiple SQL statements in a transaction.
-
Constructor Details
-
MySQLClient
Creates a new MySQL client.- Parameters:
host- The MySQL server hostname or IPport- The MySQL server port (default: 3306)database- The database nameusername- The MySQL usernamepassword- The MySQL password
-
-
Method Details
-
createDatabaseIfNotExists
public static boolean createDatabaseIfNotExists(String host, int port, String database, String username, String password) Creates a database if it doesn't exist.This is a utility method to create a database before connecting. Call this before creating a MySQLClient instance.
- Parameters:
host- The MySQL server hostname or IPport- The MySQL server portdatabase- The database name to createusername- The MySQL usernamepassword- The MySQL password- Returns:
- true if database was created or already exists, false on error
-
connect
public boolean connect()Connects to the MySQL server with connection pooling.- Returns:
- true if connected successfully, false otherwise
-
disconnect
public void disconnect()Disconnects from the MySQL server and closes the connection pool. -
isConnected
public boolean isConnected()Checks if the client is connected to MySQL.- Returns:
- true if connected, false otherwise
-
query
-
execute
-
executeBatch
-
insert
-
queryOne
-
queryValue
-
tableExists
Checks if a table exists.- Parameters:
tableName- The table name- Returns:
- true if table exists, false otherwise
-
transaction
Executes multiple SQL statements in a transaction.- Parameters:
statements- Array of SQL statements with their parameters- Returns:
- true if transaction succeeded, false otherwise
-
getDataSource
public com.zaxxer.hikari.HikariDataSource getDataSource()Gets direct access to the HikariCP data source.Use this for advanced operations. Remember to close connections properly.
- Returns:
- The HikariCP data source
-