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

public abstract class GenericDatabase<T> extends Object
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 Details

    • GenericDatabase

      public GenericDatabase(String infoString)
      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

      public ConnectionInfos getConnectionInfos()
    • getConnection

      protected Connection getConnection()
      Retrieves a connection from the connection pool.
      Returns:
      A Connection object from the pool.
    • paramCount

      protected Long paramCount(String query)
      Counts the number of parameter placeholders ('?') in the given SQL query.
      Parameters:
      query - The SQL query string.
      Returns:
      The count of parameter placeholders in the query.
    • 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

      public abstract boolean execute(T statement, Map<Integer,Object>... params)
      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

      protected abstract boolean syncExecute(T statement, Map<Integer,Object>... params)
      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

      protected abstract boolean asyncExecute(T statement, Map<Integer,Object>... params)
      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

      public abstract ResultSet query(T statement, Map<Integer,Object>... params)
      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

      protected abstract ResultSet syncQuery(T statement, Map<Integer,Object>... params)
      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

      protected abstract ResultSet asyncQuery(T statement, Map<Integer,Object>... params)
      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.