Class GenericDatabase<T>
java.lang.Object
br.net.dd.netherwingcore.database.common.GenericDatabase<T>
- Type Parameters:
T- The type of database statement used in execute and query methods.
- Direct Known Subclasses:
LoginDatabase
GenericDatabase is an abstract class that provides a template for database operations.
It manages the connection pool and provides methods for connecting, disconnecting,
preparing statements, and executing queries.
-
Constructor Summary
ConstructorsConstructorDescriptionGenericDatabase(String infoString) Constructs a GenericDatabase instance with the provided connection information string. -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract booleanasyncExecute(T statement, Map<Integer, Object>... params) Asynchronous execution of a database statement.protected abstract ResultSetasyncQuery(T statement, Map<Integer, Object>... params) Asynchronous query execution.booleanconnect()Establishes a connection to the database by initializing the connection pool.booleanDisconnects from the database by closing all connections in the pool.abstract booleanExecutes a database statement.protected ConnectionRetrieves a connection from the connection pool.protected final PreparedStatementgetPreparedStatement(String query, Map<Integer, Object>... params) Prepares a SQL statement with the given query and parameters.protected LongparamCount(String query) Counts the number of parameter placeholders ('?')abstract ResultSetExecutes a query and returns the result set.protected abstract booleansyncExecute(T statement, Map<Integer, Object>... params) Synchronous execution of a database statement.protected abstract ResultSetSynchronous query execution.
-
Constructor Details
-
GenericDatabase
Constructs a GenericDatabase instance with the provided connection information string.- Parameters:
infoString- A semicolon-separated string containing database connection details. Format: "host;port;username;password;databaseName"- Throws:
IllegalArgumentException- if the infoString is null or empty.
-
-
Method Details
-
connect
public boolean connect()Establishes a connection to the database by initializing the connection pool.- Returns:
- true if the connection was successfully established, false if already connected.
-
disconnect
public boolean disconnect()Disconnects from the database by closing all connections in the pool.- Returns:
- true if the disconnection was successful, false if there was no active connection.
-
getConnectionInfos
-
getConnection
Retrieves a connection from the connection pool.- Returns:
- A Connection object from the pool.
-
paramCount
-
getPreparedStatement
@SafeVarargs protected final PreparedStatement getPreparedStatement(String query, Map<Integer, Object>... params) Prepares a SQL statement with the given query and parameters.- Parameters:
query- The SQL query string with placeholders for parameters.params- A variable number of maps containing parameter indices and their corresponding values.- Returns:
- A PreparedStatement object with the parameters set, or null if the parameter count does not match.
-
execute
Executes a database statement.- Parameters:
statement- The database statement to execute.params- A variable number of maps containing parameter indices and their corresponding values.- Returns:
- true if the execution was successful, false otherwise.
-
syncExecute
Synchronous execution of a database statement.- Parameters:
statement- The database statement to execute.params- A variable number of maps containing parameter indices and their corresponding values.- Returns:
- true if the execution was successful, false otherwise.
-
asyncExecute
Asynchronous execution of a database statement.- Parameters:
statement- The database statement to execute.params- A variable number of maps containing parameter indices and their corresponding values.- Returns:
- true if the execution was successful, false otherwise.
-
query
Executes a query and returns the result set.- Parameters:
statement- The database statement to query.params- A variable number of maps containing parameter indices and their corresponding values.- Returns:
- A ResultSet containing the results of the query.
-
syncQuery
Synchronous query execution.- Parameters:
statement- The database statement to query.params- A variable number of maps containing parameter indices and their corresponding values.- Returns:
- A ResultSet containing the results of the query.
-
asyncQuery
Asynchronous query execution.- Parameters:
statement- The database statement to query.params- A variable number of maps containing parameter indices and their corresponding values.- Returns:
- A ResultSet containing the results of the query.
-