Class MYSQL

java.lang.Object
io.github.coho04.mysql.MYSQL
All Implemented Interfaces:
QueryHelper

public class MYSQL extends Object implements QueryHelper
The MYSQL class provides methods for connecting to a MySQL server and performing operations on the server.
  • Constructor Details

    • MYSQL

      public MYSQL(String hostname, String username, String password, int port)
      Constructor for creating a MYSQL object with the specified hostname, username, password, and port.
      Parameters:
      hostname - The hostname of the MySQL server.
      username - The username to connect to the MySQL server.
      password - The password to connect to the MySQL server.
      port - The port of the MySQL server.
    • MYSQL

      public MYSQL(String hostname, String username, String password)
      Constructor for creating a MYSQL object with the specified hostname, username, and password.
      Parameters:
      hostname - The hostname of the MySQL server.
      username - The username to connect to the MySQL server.
      password - The password to connect to the MySQL server.
    • MYSQL

      public MYSQL(String hostname, String username, String password, int port, T exceptionHandlerClass)
      Constructs a MYSQL object with the specified hostname, username, password, and port.
      Type Parameters:
      T - The type of the ExceptionHandler class.
      Parameters:
      hostname - The hostname of the MySQL server.
      username - The username to connect to the MySQL server.
      password - The password to connect to the MySQL server.
      port - The port of the MySQL server.
      exceptionHandlerClass - The ExceptionHandler class for handling exceptions.
  • Method Details

    • setPassword

      public void setPassword(String password)
      Sets the password for the MYSQL class.
      Parameters:
      password - The password to be set.
    • setHostname

      public void setHostname(String hostname)
      Sets the hostname for the MYSQL class.
      Parameters:
      hostname - The hostname to be set.
    • setPort

      public void setPort(int port)
      Sets the port for the MYSQL class.
      Parameters:
      port - The port to be set for the MYSQL class.
    • setUsername

      public void setUsername(String username)
      Sets the username for the MYSQL class.
      Parameters:
      username - The username to be set for the MYSQL class.
    • getUsername

      public String getUsername()
      Retrieves the username associated with the MySQL server.
      Returns:
      The username of the MySQL server.
    • getPort

      public int getPort()
      This method is used to retrieve the port of the MySQL server.
      Returns:
      The port of the MySQL server.
    • getHostname

      public String getHostname()
      This method returns the hostname of the MySQL server.
      Returns:
      The hostname of the MySQL server.
    • getVersion

      public String getVersion()
      This method is used to get the version of the MySQL server.
      Returns:
      The version of the MySQL server.
    • getUsers

      public List<User> getUsers()
      Retrieves a list of User objects representing the users in the MySQL server.
      Returns:
      A list of User objects representing the users in the MySQL server.
    • existsDatabase

      public boolean existsDatabase(String databaseName)
      Checks if a database with the given name exists.
      Parameters:
      databaseName - The name of the database to check.
      Returns:
      true if the database exists, false otherwise.
    • existsUser

      public boolean existsUser(String name)
      Checks if a user with the given name exists in the MySQL server.
      Parameters:
      name - The name of the user to check.
      Returns:
      true if the user exists, false otherwise.
    • customExecute

      public void customExecute(String SQL)
      Executes the given SQL statement.
      Parameters:
      SQL - The SQL statement to be executed.
    • getDatabase

      public Database getDatabase(String name)
      Retrieves a Database object with the given name.
      Parameters:
      name - The name of the database to retrieve.
      Returns:
      The Database object with the given name, or null if it doesn't exist.
    • getOrCreateDatabase

      public Database getOrCreateDatabase(String name)
      Retrieves an existing Database object with the given name or creates a new one if it doesn't exist.
      Parameters:
      name - The name of the database to retrieve or create.
      Returns:
      An instance of the Database class representing the database with the given name.
    • createDatabase

      public void createDatabase(String database)
      This method is used to create a new database.
      Parameters:
      database - The name of the database to be created.
    • onFlushPrivileges

      public void onFlushPrivileges()
      This method is used to flush the MySQL server privileges.
    • switchDatabase

      public void switchDatabase(Database database)
      Switches the current database to the specified database.
      Parameters:
      database - The database to switch to.
    • createUser

      public void createUser(String username, String password, Boolean database)
      Creates a user in the MySQL server.
      Parameters:
      username - The username of the new user.
      password - The password of the new user.
      database - true if a new database should be created with the same name as the username, false otherwise.
    • createUser

      public void createUser(String username, String password)
      Creates a user in the MySQL server.
      Parameters:
      username - The username of the new user.
      password - The password of the new user.
    • getOrCreateUser

      public User getOrCreateUser(String username, String password)
      Retrieves an existing User object with the given username or creates a new one if it doesn't exist.
      Parameters:
      username - The username of the user to retrieve or create.
      password - The password of the user to retrieve or create.
      Returns:
      An instance of the User class representing the user with the given username.
    • getDatabases

      public List<Database> getDatabases()
      Retrieves a list of databases from the MySQL server.
      Returns:
      a list of Database objects representing the databases in the MySQL server.
    • getUser

      public User getUser(String name)
      This method is used to get a User object with the given name.
      Parameters:
      name - The name of the user.
      Returns:
      The User object with the given name.
    • setExceptionHandlerClass

      public <T extends ExceptionHandler> void setExceptionHandlerClass(T exceptionHandler)
      Sets the ExceptionHandler class for handling exceptions.
      Type Parameters:
      T - The type of the ExceptionHandler class.
      Parameters:
      exceptionHandler - The instance of the ExceptionHandler class.
    • getExceptionHandlerClass

      public ExceptionHandler getExceptionHandlerClass()
      This method is used to get the ExceptionHandler class associated with the MYSQL class.
      Returns:
      The ExceptionHandler class instance.
    • closeRsAndSt

      public void closeRsAndSt(ResultSet resultSet, Statement statement)
      Closes the given ResultSet and Statement objects. If any exception occurs during the close operation, it is passed to the callException(Exception) method.
      Parameters:
      resultSet - The ResultSet object to be closed.
      statement - The Statement object to be closed.
    • close

      public void close()
      Closes the connection to the MySQL server. If an exception occurs during the close operation, it will be passed to the callException(Exception) method.
    • closeConnection

      public void closeConnection()
      Closes the connection to the MySQL server. If an exception occurs during the close operation, it will be passed to the callException(Exception) method.
    • getConnect

      public Connection getConnect()
      This method is used to get the connection to the MySQL server.
      Returns:
      The connection to the MySQL server.
    • callException

      public void callException(Exception exception)
      This method is used to handle exceptions.
      Parameters:
      exception - The exception to be handled.