Class SQLConnector

java.lang.Object
com.blackrook.sql.SQLConnector

public class SQLConnector extends Object
Core database JDBC connector object. From this object, representing a potential link to a remote (or local) database, connections can be spawned.
Author:
Matthew Tropiano
  • Constructor Details

    • SQLConnector

      public SQLConnector(String className, String jdbcURL)
      Constructs a new database connector.
      Parameters:
      className - The fully qualified class name of the driver.
      jdbcURL - The JDBC URL to use.
      Throws:
      RuntimeException - if the driver class cannot be found.
    • SQLConnector

      public SQLConnector(String className, String jdbcURL, Properties info)
      Constructs a new database connector.
      Parameters:
      className - The fully qualified class name of the driver.
      jdbcURL - The JDBC URL to use.
      info - the set of Properties to pass along to the JDBC DriverManager.
      Throws:
      RuntimeException - if the driver class cannot be found.
    • SQLConnector

      public SQLConnector(String className, String jdbcURL, String userName, String password)
      Constructs a new database connector.
      Parameters:
      className - The fully qualified class name of the driver.
      jdbcURL - The JDBC URL to use.
      userName - the username.
      password - the password.
      Throws:
      RuntimeException - if the driver class cannot be found.
  • Method Details

    • getJDBCURL

      public String getJDBCURL()
      Returns the full JDBC URL for this specific connector. This differs by implementation and driver.
      Returns:
      the URL
    • getConnection

      public SQLConnection getConnection() throws SQLException
      Returns a new, opened JDBC Connection using the credentials stored with this connector.
      Returns:
      a DriverManager-created connection.
      Throws:
      SQLException - if a connection can't be procured.
      See Also:
    • getConnectionAnd

      public void getConnectionAnd(SQLConnectionConsumer handler) throws SQLException
      Creates a connection, passes it to the provided SQLConnectionConsumer function, then closes it.
      Parameters:
      handler - the consumer function that accepts the retrieved connection.
      Throws:
      SQLException - if a connection cannot be re-created or re-established.
    • getConnectionAnd

      public <R> R getConnectionAnd(SQLConnectionFunction<R> handler) throws SQLException
      Creates a connection, passes it to the provided SQLConnectionFunction, calls it, closes it, and returns the result.
      Type Parameters:
      R - the return type.
      Parameters:
      handler - the consumer function that accepts the retrieved connection and returns a value.
      Returns:
      the return value of the handler function.
      Throws:
      SQLException - if a connection cannot be re-created or re-established.