Class DBConnection
java.lang.Object
com.github.collinalpert.java2db.database.DBConnection
- All Implemented Interfaces:
Closeable,AutoCloseable
public class DBConnection extends Object implements Closeable
Wrapper around
Connection which eases use of connecting to a database and running queries.
Also supports running functions and stored procedures.- Author:
- Collin Alpert
-
Field Summary
Fields Modifier and Type Field Description static booleanLOG_QUERIESConstant which determines if the queries generated by Java2DB will be logged in the console. -
Constructor Summary
Constructors Constructor Description DBConnection(ConnectionConfiguration configuration)DBConnection(Connection underlyingConnection) -
Method Summary
Modifier and Type Method Description <T> Optional<T>callFunction(Class<T> returnType, String functionName, Object... arguments)Calls an SQL function.<T> CompletableFuture<Optional<T>>callFunctionAsync(Class<T> returnType, String functionName, Object... arguments)Calls an SQL function asynchronously.<T> CompletableFuture<Void>callFunctionAsync(Class<T> returnType, Consumer<? super Optional<T>> callback, String functionName, Object... arguments)Calls an SQL function asynchronously and applies aConsumerto the returned value.<T> CompletableFuture<Optional<T>>callFunctionAsync(Consumer<SQLException> exceptionHandler, Class<T> returnType, String functionName, Object... arguments)Calls an SQL function asynchronously.<T> CompletableFuture<Void>callFunctionAsync(Consumer<SQLException> exceptionHandler, Class<T> returnType, Consumer<? super Optional<T>> callback, String functionName, Object... arguments)Calls an SQL function asynchronously and applies aConsumerto the returned value.<T> Queryable<T>callStoredProcedure(Class<T> returnType, String storedProcedureName, Object... arguments)Calls a stored procedure and returns aQueryableto fetch the data in the desired format.<T> AsyncQueryable<T>callStoredProcedureAsync(Class<T> returnType, String storedProcedureName, Object... arguments)Calls a stored procedure asynchronously and returns aQueryableto fetch the data in the desired format.voidclose()Closes the connection to the database.ResultSetexecute(String query)Executes a DQL statement on the database without Java parameters.ResultSetexecute(String query, Object... params)Executes a DQL statement on the database with Java parameters.booleanisOpen()Determines if a connection to the database still exists or not.booleanisValid()Checks if the connection is valid/successful.ConnectionunderlyingConnection()intupdate(String query)This command is used for any DDL/DML queries.intupdate(String query, Object... params)This command is used for any DDL/DML queries with Java parameters.
-
Field Details
-
LOG_QUERIES
public static boolean LOG_QUERIESConstant which determines if the queries generated by Java2DB will be logged in the console.
-
-
Constructor Details
-
Method Details
-
isValid
public boolean isValid()Checks if the connection is valid/successful.- Returns:
- True if connection was successful, false if not.
-
execute
Executes a DQL statement on the database without Java parameters.- Parameters:
query- The query to be executed.- Returns:
- The
ResultSetcontaining the result from the DQL statement. - Throws:
SQLException- if the query is malformed or cannot be executed.
-
execute
Executes a DQL statement on the database with Java parameters.- Parameters:
query- The query to be executed.params- The Java parameters to be inserted into the query.- Returns:
- The
ResultSetcontaining the result from the DQL statement. - Throws:
SQLException- if the query is malformed or cannot be executed.
-
update
This command is used for any DDL/DML queries.- Parameters:
query- The query to be executed.- Returns:
- the last generated ID. This return value should only be used with INSERT statements.
- Throws:
SQLException- if the query is malformed or cannot be executed.
-
update
This command is used for any DDL/DML queries with Java parameters.- Parameters:
query- The query to be executed.params- The Java parameters to be inserted into the query.- Returns:
- the last generated ID. This return value should only be used with INSERT statements.
- Throws:
SQLException- if the query is malformed or cannot be executed.
-
isOpen
public boolean isOpen()Determines if a connection to the database still exists or not.- Returns:
Trueif a connection exists,falseif not. This method will returnfalseif an exception occurs.
-
close
public void close()Closes the connection to the database.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
callFunction
public <T> Optional<T> callFunction(Class<T> returnType, String functionName, Object... arguments) throws SQLExceptionCalls an SQL function.- Type Parameters:
T- The functions return type.- Parameters:
returnType- The Java equivalent of the SQL datatype the function returns.functionName- The name of the function.arguments- The arguments to be supplied to the function, if any.- Returns:
- The return value of the function, as a Java datatype.
- Throws:
SQLException- In case there is an error communicating with the database, i.e. the function does not exist.
-
callFunctionAsync
public <T> CompletableFuture<Optional<T>> callFunctionAsync(Consumer<SQLException> exceptionHandler, Class<T> returnType, String functionName, Object... arguments)Calls an SQL function asynchronously.- Type Parameters:
T- The functions return type.- Parameters:
exceptionHandler- The exception handler which handles theSQLExceptionin case something goes wrong.returnType- The Java type which the result will be mapped into. It needs to have a parameterless constructor available.functionName- The name of the function to call.arguments- The arguments to supply to the function, if any.- Returns:
- The return value of the function, as a Java datatype.
-
callFunctionAsync
public <T> CompletableFuture<Optional<T>> callFunctionAsync(Class<T> returnType, String functionName, Object... arguments)Calls an SQL function asynchronously. If an exception occurs, it gets printed to the console.- Type Parameters:
T- The functions return type.- Parameters:
returnType- The Java type which the result will be mapped into. It needs to have a parameterless constructor available.functionName- The name of the function to call.arguments- The arguments to supply to the function, if any.- Returns:
- The return value of the function, as a Java datatype.
-
callFunctionAsync
public <T> CompletableFuture<Void> callFunctionAsync(Class<T> returnType, Consumer<? super Optional<T>> callback, String functionName, Object... arguments)Calls an SQL function asynchronously and applies aConsumerto the returned value.- Type Parameters:
T- The functions return type.- Parameters:
returnType- The Java type which the result will be mapped into. It needs to have a parameterless constructor available.callback- A consumer which specifies which action to perform once the function has been called.functionName- The name of the function to call.arguments- The arguments to supply to the function, if any.- Returns:
- The return value of the function, as a Java datatype.
-
callFunctionAsync
public <T> CompletableFuture<Void> callFunctionAsync(Consumer<SQLException> exceptionHandler, Class<T> returnType, Consumer<? super Optional<T>> callback, String functionName, Object... arguments)Calls an SQL function asynchronously and applies aConsumerto the returned value.- Type Parameters:
T- The functions return type.- Parameters:
exceptionHandler- The exception handler which handles theSQLExceptionin case something goes wrong.returnType- The Java type which the result will be mapped into. If it is a complex type, it needs to have a parameterless constructor available.callback- A consumer which specifies which action to perform once the function has been called.functionName- The name of the function to call.arguments- The arguments to supply to the function, if any.- Returns:
- The return value of the function, as a Java datatype.
-
callStoredProcedure
public <T> Queryable<T> callStoredProcedure(Class<T> returnType, String storedProcedureName, Object... arguments)Calls a stored procedure and returns aQueryableto fetch the data in the desired format.- Type Parameters:
T- The Java type to be returned?- Parameters:
returnType- The Java type which the result will be mapped into. If it is a complex type, it needs to have a parameterless constructor available.storedProcedureName- The name of the stored procedure to call.arguments- The arguments to pass to the stored procedure.- Returns:
- A
Queryableto fetch the data in the desired format.
-
callStoredProcedureAsync
public <T> AsyncQueryable<T> callStoredProcedureAsync(Class<T> returnType, String storedProcedureName, Object... arguments)Calls a stored procedure asynchronously and returns aQueryableto fetch the data in the desired format.- Type Parameters:
T- The Java type to be returned?- Parameters:
returnType- The Java type which the result will be mapped into. If it is a complex type, it needs to have a parameterless constructor available.storedProcedureName- The name of the stored procedure to call.arguments- The arguments to pass to the stored procedure.- Returns:
- An
AsyncQueryableto fetch the data in the desired format.
-
underlyingConnection
- Returns:
- The
Connectionobject this class uses to operate on.
-