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 Details

    • LOG_QUERIES

      public static boolean LOG_QUERIES
      Constant 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

      public ResultSet execute​(String query) throws SQLException
      Executes a DQL statement on the database without Java parameters.
      Parameters:
      query - The query to be executed.
      Returns:
      The ResultSet containing the result from the DQL statement.
      Throws:
      SQLException - if the query is malformed or cannot be executed.
    • execute

      public ResultSet execute​(String query, Object... params) throws SQLException
      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 ResultSet containing the result from the DQL statement.
      Throws:
      SQLException - if the query is malformed or cannot be executed.
    • update

      public int update​(String query) throws SQLException
      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

      public int update​(String query, Object... params) throws SQLException
      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:
      True if a connection exists, false if not. This method will return false if an exception occurs.
    • close

      public void close()
      Closes the connection to the database.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • callFunction

      public <T> Optional<T> callFunction​(Class<T> returnType, String functionName, Object... arguments) throws SQLException
      Calls 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 the SQLException in 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 a Consumer to 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 a Consumer to the returned value.
      Type Parameters:
      T - The functions return type.
      Parameters:
      exceptionHandler - The exception handler which handles the SQLException in 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 a Queryable to 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 Queryable to 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 a Queryable to 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 AsyncQueryable to fetch the data in the desired format.
    • underlyingConnection

      public Connection underlyingConnection()
      Returns:
      The Connection object this class uses to operate on.