Class DBConnection

java.lang.Object
com.github.collinalpert.java2db.database.DBConnection
All Implemented Interfaces:
Closeable, AutoCloseable

public class DBConnection
extends Object
implements Closeable
Author:
Collin Alpert
  • Field Details

    • HOST

      public static String HOST
      Specifies the hostname/ip address of the database.
    • DATABASE

      public static String DATABASE
      Specifies the name of the database to connect to.
    • USERNAME

      public static String USERNAME
      Specifies the username to log in on the database with.
    • PASSWORD

      public static String PASSWORD
      Specifies the password to log in on the database with.
    • PORT

      public static int PORT
      Specifies the port to connect to the database on. This property is optional. If not specified, it will be set to 3306, the default port of MySQL.
    • TIMEOUT

      public static int TIMEOUT
      Specifies the login timeout to the database in seconds.
    • 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 long 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 long 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.
      Returns:
      The 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)
    • callFunctionAsync

      public <T> CompletableFuture<Void> callFunctionAsync​(Consumer<SQLException> exceptionHandler, Consumer<? super Optional<T>> callback, Class<T> returnType, String functionName, Object... arguments)
    • callStoredProcedure

      public <T> Queryable<T> callStoredProcedure​(Class<T> returnType, String storedProcedureName, Object... arguments)
    • callStoredProcedureAsync

      public <T> AsyncQueryable<T> callStoredProcedureAsync​(Class<T> returnType, String storedProcedureName, Object... arguments)
    • underlyingConnection

      public Connection underlyingConnection()