Class JDBCConnection

    • Constructor Summary

      Constructors 
      Constructor Description
      JDBCConnection()
      Create a new JDBCConnection object.
      JDBCConnection​(java.lang.String driver, java.lang.String url)
      Create a new JDBCConnection object with specific driver and URL
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void beginTransaction()
      Open a transaction on the database connection
      void close()
      Implement autocloseable.
      void commitTransaction()
      Finalize the transaction by committing all the changes and closing the transaction.
      void configure​(Config config)
      Implement Configurable
      protected java.sql.Connection connect()
      Connect to the database.
      java.sql.Connection getConnection()  
      java.lang.String getDriver()  
      java.lang.String getUrl()  
      void rollbackTransaction()
      Undo all of the changes made during the current transaction
      void setDriver​(java.lang.String driver)  
      void setUrl​(java.lang.String url)  
      void shutdown()
      Shutdown the database connection.
      void startup()
      Initialize the database connection.
      java.sql.Connection verifyConnection()
      Check whether database connection is closed, and reconnect if needed.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • JDBCConnection

        public JDBCConnection()
        Create a new JDBCConnection object.
      • JDBCConnection

        public JDBCConnection​(java.lang.String driver,
                              java.lang.String url)
        Create a new JDBCConnection object with specific driver and URL
        Parameters:
        driver - String of driver
        url - String of URL
    • Method Detail

      • close

        public void close()
                   throws java.lang.Exception
        Implement autocloseable. Calls shutdown().
        Specified by:
        close in interface java.lang.AutoCloseable
        Throws:
        java.lang.Exception - Exception
      • configure

        public void configure​(Config config)
                       throws java.lang.Exception
        Implement Configurable
        Specified by:
        configure in interface Configurable
        Overrides:
        configure in class DefaultConfigurable
        Parameters:
        config - Config to set driver and URL in
        Throws:
        java.lang.Exception - Exception
      • connect

        protected java.sql.Connection connect()
                                       throws java.lang.Exception
        Connect to the database. Sub-classes determine how connection is made.
        Returns:
        the connection.
        Throws:
        java.lang.Exception - if unable to connect.
      • startup

        public void startup()
                     throws java.lang.Exception
        Initialize the database connection. Sub-classes should call super.startup(), before preparing any statements.
        Specified by:
        startup in interface Configurable
        Overrides:
        startup in class DefaultConfigurable
        Throws:
        java.lang.Exception - if error occurs
      • shutdown

        public void shutdown()
                      throws java.lang.Exception
        Shutdown the database connection. Sub-classes should close any prepared statements (catching any exceptions), and then call super.shutdown() to close the database connection.
        Specified by:
        shutdown in interface Configurable
        Overrides:
        shutdown in class DefaultConfigurable
        Throws:
        java.lang.Exception - if error occurs
      • beginTransaction

        public void beginTransaction()
                              throws java.lang.Exception
        Open a transaction on the database connection
        Throws:
        java.lang.Exception - if error occurs
      • commitTransaction

        public void commitTransaction()
                               throws java.lang.Exception
        Finalize the transaction by committing all the changes and closing the transaction.
        Throws:
        java.lang.Exception - if error occurs
      • rollbackTransaction

        public void rollbackTransaction()
                                 throws java.lang.Exception
        Undo all of the changes made during the current transaction
        Throws:
        java.lang.Exception - if error occurs
      • getConnection

        public java.sql.Connection getConnection()
        Returns:
        current connection object, or null if not connected.
      • verifyConnection

        public java.sql.Connection verifyConnection()
                                             throws java.lang.Exception
        Check whether database connection is closed, and reconnect if needed. Executes the query "select 1" using the current database connection. If this doesn't succeed, reinitializes the database connection by calling shutdown() then startup().
        Returns:
        Valid connection object.
        Throws:
        java.lang.Exception - if unable to (re)connect.
      • getDriver

        public java.lang.String getDriver()
        Returns:
        driver
      • setDriver

        public void setDriver​(java.lang.String driver)
        Parameters:
        driver - Driver to set
      • getUrl

        public java.lang.String getUrl()
        Returns:
        URL
      • setUrl

        public void setUrl​(java.lang.String url)
        Parameters:
        url - URL to set