Class PostgreConnection

java.lang.Object
net.hectus.sql.PostgreConnection
Direct Known Subclasses:
AutoCatchingPostgreConnection

public class PostgreConnection extends Object
Represents a connection to a PostgreSQL database with built-in features for accessing the database with proper values like the player UUID, etc.
  • Constructor Details

    • PostgreConnection

      public PostgreConnection(String url, String username, String password, String table) throws SQLException
      Creates a connection to a PostgreSQL database.
      Parameters:
      url - The URL to the database (jdbc:postgresql://ip:port/database_name)
      username - Username of the account to access the database with.
      password - Password of the account to access the database with.
      table - The table in the database that will accessed. Can be changed later on using changeTable(String).
      Throws:
      SQLException - if the connection wasn't successful, which is likely due to a wrong URL or wrong credentials.
      See Also:
    • PostgreConnection

      public PostgreConnection(String ip, int port, String databaseName, String username, String password, String table) throws SQLException
      Creates a connection to a PostgreSQL database.
      Parameters:
      ip - The IP of the database. Can be localhost.
      port - The port that the database runs on. PostgreSQL default is 5432.
      databaseName - The database to name, this should connect to.
      username - Username of the account to access the database with.
      password - Password of the account to access the database with.
      table - The table in the database that will accessed. Can be changed later on using changeTable(String).
      Throws:
      SQLException - if the connection wasn't successful, which is likely due to a wrong URL or wrong credentials.
      See Also:
  • Method Details

    • closeConnection

      public void closeConnection() throws SQLException
      Closes the connection to the database. Recommended to use this before shutting down the program, to not cause any connection issues with the database.
      Throws:
      SQLException - if there was an issue while closing the connection or the connection is already closed.
    • connection

      public Connection connection()
      Get the current connection that's being used. No actual use.
      Returns:
      The currently used connection to the database.
    • createNewStatement

      public void createNewStatement() throws SQLException
      Generates a new statement to use for queries. Can be useful for clearing some memory.
      Throws:
      SQLException - if there was an error while creating the new statement.
    • table

      public String table()
      Get the current table to get data from.
      Returns:
      The table that's currently being used.
      See Also:
    • changeTable

      public void changeTable(String table)
      Changes what table to get data from and execute all queries on.
      Parameters:
      table - The new table to get data from.
      See Also:
    • executeQuery

      public <T> T executeQuery(String sql, Object @NotNull ... params) throws SQLException
      Executes a parameterized SQL query and returns the result. When the result has multiple values, it will choose the first column index.
      Type Parameters:
      T - The type of the result to be returned.
      Parameters:
      sql - The parameterized query to be executed.
      params - The parameters that should be set into the sql query input.
      Returns:
      The result of the query execution, or null if it's empty.
      Throws:
      SQLException - if there was an error while executing the query.
    • getRowArray

      public Object[] getRowArray(UUID uuid) throws SQLException
      Get a whole row from the table based on a player UUID.
      Parameters:
      uuid - The UUID of the player to get the row from.
      Returns:
      All objects of the row as an array.
      Throws:
      SQLException - if there was an error while executing the query.
    • getRowMap

      public Map<String,Object> getRowMap(UUID uuid) throws SQLException
      Get a whole row with column names and values from the table based on a player UUID.
      Parameters:
      uuid - The UUID of the player to get the row from.
      Returns:
      All objects of the row as a map.
      Throws:
      SQLException - if there was an error while executing the query.
    • getRow

      public ResultSet getRow(UUID uuid) throws SQLException
      Get the result of getting a row based on a player UUID.
      Parameters:
      uuid - The UUID of the player to get the row from.
      Returns:
      The result of the query.
      Throws:
      SQLException - if there was an error while executing the query.
    • get

      public Object get(UUID uuid, String column) throws SQLException
      Get a value from a specific player's data, based on a player UUID and the column name.
      Parameters:
      uuid - The UUID of the player to get the value from.
      column - The column/value's name.
      Returns:
      The value in the specified field.
      Throws:
      SQLException - if there was an error while executing the query.
    • get

      public Object get(UUID uuid, int column) throws SQLException
      Get a value from a specific player's data, based on a player UUID and the column index.
      Parameters:
      uuid - The UUID of the player to get the value from.
      column - The column/value's index (starting at 1).
      Returns:
      The value in the specified field.
      Throws:
      SQLException - if there was an error while executing the query.
    • set

      public void set(UUID uuid, String column, Object newValue) throws SQLException
      Set a value from a specific player's data to a new value, based on a player UUID and the column name.
      Parameters:
      uuid - The UUID of the player to get the value from.
      column - The column/value's name.
      newValue - The new value for the specified field.
      Throws:
      SQLException - if there was an error while executing the query.
    • set

      public void set(UUID uuid, int column, Object newValue) throws SQLException
      Set a value from a specific player's data to a new value, based on a player UUID and the column index.
      Parameters:
      uuid - The UUID of the player to get the value from.
      column - The column/value's index (starting at 1).
      newValue - The new value for the specified field.
      Throws:
      SQLException - if there was an error while executing the query.
    • add

      public void add(UUID uuid, Object @NotNull ... values) throws SQLException
      Inserts a new row with a player and the values into the table.
      Parameters:
      uuid - The player's UUID, which is the first column.
      values - All other values for the new row. Has to be valid according to database settings, otherwise will throw the following Exception.
      Throws:
      SQLException - if there was an error while executing the query. Can be caused by invalid values.
    • remove

      public void remove(UUID uuid) throws SQLException
      Removed a player's row based on his UUID.
      Parameters:
      uuid - The player's UUID.
      Throws:
      SQLException - if there was an error while executing the query. Can be caused by removing a non-existent player.
    • contains

      public boolean contains(UUID uuid) throws SQLException
      Checks whether the table contains an entry/row for the player or not.
      Parameters:
      uuid - The player's UUID.
      Returns:
      true if the table contains the player's UUID, false otherwise.
      Throws:
      SQLException - if there was an error while executing the query.
    • getRowArraysContaining

      public List<Object[]> getRowArraysContaining(Object object, String... checkedColumns) throws SQLException
      Get all rows of the table containing a specific value in any specified column as a List of arrays, where the list represents the rows and the arrays are each row's columns.
      Parameters:
      object - What object must be contained in at least one of the rows.
      checkedColumns - All columns that are checked for the object.
      Returns:
      A list containing all columns as an array.
      Throws:
      SQLException - if there was an error while executing the query.
    • getRowMapsContaining

      public List<Map<String,Object>> getRowMapsContaining(Object object, String... checkedColumns) throws SQLException
      Get all rows of the table containing a specific value in any specified column as a List of arrays, where the list represents the rows and the maps are each row's columns with their name first and then their value.
      Parameters:
      object - What object must be contained in at least one of the rows.
      checkedColumns - All columns that are checked for the object.
      Returns:
      A map containing all columns with their names and values.
      Throws:
      SQLException - if there was an error while executing the query.