Database Component
com.eu.habbo.core.database.DatabaseComponent
The DatabaseComponent class in the Arcturus project is responsible for managing the database connection and executing SQL statements. This documentation will guide you on how to access and use the different methods provided by this component.
Accessing the Database Component
To use the Database Component, you need to create an instance of it and pass the ConfigComponent as a constructor parameter. Here's an example of how you can do it:
DatabaseComponent db = new DatabaseComponent(config);Methods
The DatabaseComponent class provides the following methods:
execute
boolean execute(String sql, SQLExceptionRunnable... exceptionRunnable)Executes the provided sql statement. Returns true if the first result is a ResultSet object, false if the first result is an update count or there is no result. Optionally, you can add exception handlers to handle any SQLException that may occur.
statement
void statement(String sql, PreparedStatementRunnable runnable, SQLExceptionRunnable... exceptionRunnable)Executes the provided sql statement with secure parameters. The PreparedStatementRunnable allows you to access the PreparedStatement and set the secure parameters. Optionally, you can add exception handlers to handle any SQLException that may occur.
statement with ResultSet
void statement(String sql, PreparedStatementRunnableWithResultSet runnable, SQLExceptionRunnable... exceptionRunnable)Executes the provided sql statement and retrieves the ResultSet object. The PreparedStatementRunnableWithResultSet allows you to access the PreparedStatement and set the secure parameters. Optionally, you can add exception handlers to handle any SQLException that may occur.
Secure Parameters
To prevent SQL injection attacks, you should always use secure parameters when executing SQL statements. The PreparedStatement interface allows you to specify and set parameters using the setX methods. Here's an example of how you can use secure parameters:
db.statement(
"UPDATE users SET `rank` = ? WHERE id = ? LIMIT 1",
statement -> {
statement.setInt(1, rankId);
statement.setInt(2, userId);
statement.execute();
}
);Exceptions Handling
To handle exceptions that may occur during database operations, you can provide exception handlers to the method calls. The SQLExceptionRunnable interface allows you to define code to handle the SQLException that occurs. Here's an example of how you can handle exceptions:
db.execute(
"SELECT 1 + 1;",
exception -> {
// Handle the SQLException here
}
);Note: Please refer to the code comments for detailed explanations and use cases of each method.
Example Usage
Here's an example of how you can use the DatabaseComponent methods to execute SQL statements:
// Execute a simple SQL statement
boolean outcome = db.execute("SELECT 1 + 1;");
// Execute a statement with secure parameters
db.statement(
"UPDATE users SET `rank` = ? WHERE id = ? LIMIT 1",
statement -> {
statement.setInt(1, rankId);
statement.setInt(2, userId);
statement.execute();
}
);
// Get the ResultSet from a statement
db.statement(
"SELECT * FROM `emulator_settings`",
statement -> {
try (ResultSet set = statement.executeQuery()) {
while (set.next()) {
this.settings.put(set.getString("key"), set.getString("value"));
}
}
}
);
// Handle exceptions
db.execute(
"SELECT 1 + 1;",
exception -> {
// Handle the exception here
}
);