Package net.hectus.sql
Class PostgreConnection
java.lang.Object
net.hectus.sql.PostgreConnection
- Direct Known Subclasses:
AutoCatchingPostgreConnection
Represents a
connection
to a PostgreSQL database with built-in
features for accessing the database with proper values like the player UUID
, etc.-
Constructor Summary
ConstructorDescriptionPostgreConnection
(String ip, int port, String databaseName, String username, String password, String table) Creates a connection to a PostgreSQL database.PostgreConnection
(String url, String username, String password, String table) Creates a connection to a PostgreSQL database. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Inserts a new row with a player and the values into the table.void
changeTable
(String table) Changes what table to get data from and execute all queries on.void
Closes the connection to the database.Get the current connection that's being used.boolean
Checks whether the table contains an entry/row for the player or not.void
Generates a new statement to use for queries.<T> T
executeQuery
(String sql, Object @NotNull ... params) Executes a parameterized SQL query and returns the result.Get a value from a specific player's data, based on a playerUUID
and the column index.Get a value from a specific player's data, based on a playerUUID
and the column name.Object[]
getRowArray
(UUID uuid) Get a whole row from the table based on a playerUUID
.getRowArraysContaining
(Object object, String... checkedColumns) Get all rows of the table containing a specific value in any specified column as a List of arrays, where thelist
represents the rows and the arrays are each row's columns.Get a whole row with column names and values from the table based on a playerUUID
.getRowMapsContaining
(Object object, String... checkedColumns) void
Removed a player's row based on hisUUID
.void
Set a value from a specific player's data to a new value, based on a playerUUID
and the column index.void
Set a value from a specific player's data to a new value, based on a playerUUID
and the column name.table()
Get the current table to get data from.
-
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 usingchangeTable(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 usingchangeTable(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
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
Get the current connection that's being used. No actual use.- Returns:
- The currently used connection to the database.
-
createNewStatement
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
Get the current table to get data from.- Returns:
- The table that's currently being used.
- See Also:
-
changeTable
Changes what table to get data from and execute all queries on.- Parameters:
table
- The new table to get data from.- See Also:
-
executeQuery
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
Get a whole row from the table based on a playerUUID
.- Parameters:
uuid
- TheUUID
of the player to get the row from.- Returns:
- All
objects
of the row as anarray
. - Throws:
SQLException
- if there was an error while executing the query.
-
getRowMap
Get a whole row with column names and values from the table based on a playerUUID
.- Parameters:
uuid
- TheUUID
of the player to get the row from.- Returns:
- All
objects
of the row as amap
. - Throws:
SQLException
- if there was an error while executing the query.
-
getRow
- Parameters:
uuid
- TheUUID
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
Get a value from a specific player's data, based on a playerUUID
and the column name.- Parameters:
uuid
- TheUUID
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
Get a value from a specific player's data, based on a playerUUID
and the column index.- Parameters:
uuid
- TheUUID
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
Set a value from a specific player's data to a new value, based on a playerUUID
and the column name.- Parameters:
uuid
- TheUUID
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
Set a value from a specific player's data to a new value, based on a playerUUID
and the column index.- Parameters:
uuid
- TheUUID
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
Inserts a new row with a player and the values into the table.- Parameters:
uuid
- The player'sUUID
, 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 followingException
.- Throws:
SQLException
- if there was an error while executing the query. Can be caused by invalid values.
-
remove
Removed a player's row based on hisUUID
.- Parameters:
uuid
- The player'sUUID
.- Throws:
SQLException
- if there was an error while executing the query. Can be caused by removing a non-existent player.
-
contains
Checks whether the table contains an entry/row for the player or not.- Parameters:
uuid
- The player'sUUID
.- 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 thelist
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 thelist
represents the rows and themaps
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.
-