Package io.github.coho04.mysql
Class MYSQL
java.lang.Object
io.github.coho04.mysql.MYSQL
- All Implemented Interfaces:
QueryHelper
The MYSQL class provides methods for connecting to a MySQL server and performing operations on the server.
-
Nested Class Summary
Nested classes/interfaces inherited from interface io.github.coho04.mysql.interfaces.QueryHelper
QueryHelper.ResultSetHandler<T>
-
Constructor Summary
ConstructorDescriptionConstructor for creating a MYSQL object with the specified hostname, username, and password.Constructor for creating a MYSQL object with the specified hostname, username, password, and port.Constructs a MYSQL object with the specified hostname, username, password, and port. -
Method Summary
Modifier and TypeMethodDescriptionvoid
callException
(Exception exception) This method is used to handle exceptions.void
close()
Closes the connection to the MySQL server.void
Closes the connection to the MySQL server.void
closeRsAndSt
(ResultSet resultSet, Statement statement) Closes the given ResultSet and Statement objects.void
createDatabase
(String database) This method is used to create a new database.void
createUser
(String username, String password) Creates a user in the MySQL server.void
createUser
(String username, String password, Boolean database) Creates a user in the MySQL server.void
customExecute
(String SQL) Executes the given SQL statement.boolean
existsDatabase
(String databaseName) Checks if a database with the given name exists.boolean
existsUser
(String name) Checks if a user with the given name exists in the MySQL server.This method is used to get the connection to the MySQL server.getDatabase
(String name) Retrieves a Database object with the given name.Retrieves a list of databases from the MySQL server.This method is used to get the ExceptionHandler class associated with the MYSQL class.This method returns the hostname of the MySQL server.getOrCreateDatabase
(String name) Retrieves an existing Database object with the given name or creates a new one if it doesn't exist.getOrCreateUser
(String username, String password) Retrieves an existing User object with the given username or creates a new one if it doesn't exist.int
getPort()
This method is used to retrieve the port of the MySQL server.This method is used to get a User object with the given name.Retrieves the username associated with the MySQL server.getUsers()
Retrieves a list of User objects representing the users in the MySQL server.This method is used to get the version of the MySQL server.void
This method is used to flush the MySQL server privileges.<T extends ExceptionHandler>
voidsetExceptionHandlerClass
(T exceptionHandler) Sets the ExceptionHandler class for handling exceptions.void
setHostname
(String hostname) Sets the hostname for the MYSQL class.void
setPassword
(String password) Sets the password for the MYSQL class.void
setPort
(int port) Sets the port for the MYSQL class.void
setUsername
(String username) Sets the username for the MYSQL class.void
switchDatabase
(Database database) Switches the current database to the specified database.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.github.coho04.mysql.interfaces.QueryHelper
executeQuery, executeQuery, executeUpdate, executeUpdate
-
Constructor Details
-
MYSQL
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
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
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
Sets the password for the MYSQL class.- Parameters:
password
- The password to be set.
-
setHostname
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
Sets the username for the MYSQL class.- Parameters:
username
- The username to be set for the MYSQL class.
-
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
This method returns the hostname of the MySQL server.- Returns:
- The hostname of the MySQL server.
-
getVersion
This method is used to get the version of the MySQL server.- Returns:
- The version of the MySQL server.
-
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
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
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
Executes the given SQL statement.- Parameters:
SQL
- The SQL statement to be executed.
-
getDatabase
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
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
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
Switches the current database to the specified database.- Parameters:
database
- The database to switch to.
-
createUser
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
Creates a user in the MySQL server.- Parameters:
username
- The username of the new user.password
- The password of the new user.
-
getOrCreateUser
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
Retrieves a list of databases from the MySQL server.- Returns:
- a list of Database objects representing the databases in the MySQL server.
-
getUser
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
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
This method is used to get the ExceptionHandler class associated with the MYSQL class.- Returns:
- The ExceptionHandler class instance.
-
closeRsAndSt
Closes the given ResultSet and Statement objects. If any exception occurs during the close operation, it is passed to thecallException(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 thecallException(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 thecallException(Exception)
method. -
getConnect
This method is used to get the connection to the MySQL server.- Returns:
- The connection to the MySQL server.
-
callException
This method is used to handle exceptions.- Parameters:
exception
- The exception to be handled.
-