Package me.despical.commons.database
Class MySQLDatabase
java.lang.Object
me.despical.commons.database.MySQLDatabase
- Version:
- 1.0.0
- Author:
- Despical
Created at 30.05.2020
-
Constructor Summary
ConstructorsConstructorDescriptionMySQLDatabase(String user, String password, String jdbcUrl) Initializes the database using a full JDBC URL.MySQLDatabase(String user, String password, String host, String database, int port) Initializes the database using detailed connection properties.MySQLDatabase(org.bukkit.configuration.file.FileConfiguration configuration) MySQLDatabase(org.bukkit.configuration.file.FileConfiguration configuration, String userPath, String passwordPath, String jdbcUrlPath) MySQLDatabase(org.bukkit.plugin.java.JavaPlugin plugin, String fileName) Initializes the database using a custom config file name within a plugin's folder.MySQLDatabase(org.bukkit.plugin.java.JavaPlugin plugin, org.bukkit.configuration.file.FileConfiguration config) Initializes the database using a plugin's FileConfiguration. -
Method Summary
Modifier and TypeMethodDescriptionvoidexecutePreparedUpdate(String query, Object... params) SECURE: Executes an update query using a PreparedStatement to prevent SQL injection.voidexecuteUpdate(String query) Deprecated.Retrieves a database connection from the connection pool.voidSets a new logger instance.voidSafely shuts down the connection pool.
-
Constructor Details
-
MySQLDatabase
Initializes the database using a custom config file name within a plugin's folder.- Parameters:
plugin- The JavaPlugin instance.fileName- The name of the config file (e.g., "database").
-
MySQLDatabase
public MySQLDatabase(org.bukkit.plugin.java.JavaPlugin plugin, org.bukkit.configuration.file.FileConfiguration config) Initializes the database using a plugin's FileConfiguration. Uses the plugin's built-in logger.- Parameters:
plugin- The JavaPlugin instance, used to get the logger.config- The FileConfiguration containing database credentials.
-
MySQLDatabase
public MySQLDatabase(org.bukkit.configuration.file.FileConfiguration configuration) -
MySQLDatabase
-
MySQLDatabase
Initializes the database using a full JDBC URL. Uses a default standalone logger.- Parameters:
user- The database username.password- The database password.jdbcUrl- The full JDBC connection URL.
-
MySQLDatabase
Initializes the database using detailed connection properties. Uses a default standalone logger.- Parameters:
user- The database username.password- The database password.host- The database host address.database- The name of the database.port- The port number for the database.
-
-
Method Details
-
executeUpdate
Deprecated.UseexecutePreparedUpdate(java.lang.String, java.lang.Object...)to prevent SQL injection vulnerabilities.WARNING: This method is NOT SAFE and is vulnerable to SQL INJECTION. Only use this for static queries that do not involve user input (e.g., CREATE TABLE).- Parameters:
query- The SQL query to execute.
-
executePreparedUpdate
SECURE: Executes an update query using a PreparedStatement to prevent SQL injection. USE THIS METHOD for INSERT, UPDATE, or DELETE operations.- Parameters:
query- The SQL query to execute (e.g., "INSERT INTO players (uuid, name) VALUES (?, ?)")params- The parameters to be set for the '?' placeholders, in order.
-
shutdownConnPool
public void shutdownConnPool()Safely shuts down the connection pool. -
getConnection
Retrieves a database connection from the connection pool.- Returns:
- A SQL Connection object.
- Throws:
SQLException- If a connection cannot be obtained.
-
setLogger
Sets a new logger instance.- Parameters:
logger- The new logger to use.
-
executePreparedUpdate(java.lang.String, java.lang.Object...)to prevent SQL injection vulnerabilities.