Class MySQLClient

java.lang.Object
fr.hytale.loader.datastorage.MySQLClient

public class MySQLClient extends Object
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
  • Constructor Details

    • MySQLClient

      public MySQLClient(String host, int port, String database, String username, String password)
      Creates a new MySQL client.
      Parameters:
      host - The MySQL server hostname or IP
      port - The MySQL server port (default: 3306)
      database - The database name
      username - The MySQL username
      password - 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 IP
      port - The MySQL server port
      database - The database name to create
      username - The MySQL username
      password - 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

      public List<Map<String,Object>> query(String sql, Object... params)
      Executes an SQL query and returns results.
      Parameters:
      sql - The SQL query
      params - Query parameters
      Returns:
      List of rows, where each row is a Map of column-value pairs
    • execute

      public int execute(String sql, Object... params)
      Executes an SQL statement (INSERT, UPDATE, DELETE, CREATE, etc.).
      Parameters:
      sql - The SQL statement
      params - Statement parameters
      Returns:
      Number of rows affected, or 0 if error
    • executeBatch

      public int[] executeBatch(String sql, List<Object[]> paramsList)
      Executes a batch of SQL statements.
      Parameters:
      sql - The SQL statement
      paramsList - List of parameter arrays
      Returns:
      Array of update counts
    • insert

      public long insert(String sql, Object... params)
      Executes an INSERT and returns the generated key.
      Parameters:
      sql - The INSERT statement
      params - Insert parameters
      Returns:
      The generated key, or -1 if error
    • queryOne

      public Map<String,Object> queryOne(String sql, Object... params)
      Executes a query and returns a single result.
      Parameters:
      sql - The SQL query
      params - Query parameters
      Returns:
      A map of column-value pairs, or null if no result
    • queryValue

      public Object queryValue(String sql, Object... params)
      Executes a query and returns a single value.
      Parameters:
      sql - The SQL query
      params - Query parameters
      Returns:
      The value, or null if no result
    • tableExists

      public boolean tableExists(String tableName)
      Checks if a table exists.
      Parameters:
      tableName - The table name
      Returns:
      true if table exists, false otherwise
    • transaction

      public boolean transaction(MySQLClient.SQLStatement... statements)
      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