Class PostgresDB

    • Field Detail

      • POSTGRES_DEFAULT_PORT

        public static final int POSTGRES_DEFAULT_PORT
        The default port number used by PostgreSQL.
        See Also:
        Constant Field Values
      • POSTGRES_DEFAULT_USERNAME

        public static final java.lang.String POSTGRES_DEFAULT_USERNAME
        The default username used by PostgreSQL.
        See Also:
        Constant Field Values
    • Constructor Detail

      • PostgresDB

        protected PostgresDB()
        Provides a convenient constructor for DBDatabases that have configuration details hardwired or are able to automatically retrieve the details.

        This constructor creates an empty DBDatabase with only the default settings, in particular with no driver, URL, username, password, or DBDefinition

        Most programmers should not call this constructor directly. Instead you should define a no-parameter constructor that supplies the details for creating an instance using a more complete constructor.

        DBDatabase encapsulates the knowledge of the database, in particular the syntax of the database in the DBDefinition and the connection details from a DataSource.

        See Also:
        DBDefinition
      • PostgresDB

        public PostgresDB​(javax.sql.DataSource ds)
                   throws java.sql.SQLException
        Creates a PostgreSQL connection for the DataSource.
        Parameters:
        ds - ds
        Throws:
        java.sql.SQLException - database errors
      • PostgresDB

        public PostgresDB​(DatabaseConnectionSettings dcs)
                   throws java.sql.SQLException
        Creates a PostgreSQL connection for the DatabaseConnectionSettings.
        Parameters:
        dcs - dcs
        Throws:
        java.sql.SQLException - database errors
      • PostgresDB

        public PostgresDB​(java.lang.String jdbcURL,
                          java.lang.String username,
                          java.lang.String password)
                   throws java.sql.SQLException
        Creates a PostgreSQL connection for the JDBC URL, username, and password.
        Parameters:
        jdbcURL - jdbcURL
        username - username
        password - password
        Throws:
        java.sql.SQLException - database errors
      • PostgresDB

        public PostgresDB​(java.lang.String hostname,
                          int port,
                          java.lang.String databaseName,
                          java.lang.String username,
                          java.lang.String password)
                   throws java.sql.SQLException
        Creates a PostgreSQL connection to the server on the port supplied, using the username and password supplied.
        Parameters:
        hostname - hostname
        port - port
        databaseName - databaseName
        username - username
        password - password
        Throws:
        java.sql.SQLException - database errors
      • PostgresDB

        public PostgresDB​(java.lang.String hostname,
                          int port,
                          java.lang.String databaseName,
                          java.lang.String username,
                          java.lang.String password,
                          java.lang.String urlExtras)
                   throws java.sql.SQLException
        Creates a PostgreSQL connection to the server on the port supplied, using the username and password supplied.

        Extra parameters to be added to the JDBC URL can be included in the urlExtras parameter.

        Parameters:
        hostname - hostname
        password - password
        databaseName - databaseName
        port - port
        username - username
        urlExtras - urlExtras
        Throws:
        java.sql.SQLException - database errors
      • PostgresDB

        public PostgresDB​(java.lang.String databaseName,
                          java.lang.String username,
                          java.lang.String password,
                          java.lang.String urlExtras)
                   throws java.sql.SQLException
        Creates a PostgreSQL connection to local computer("localhost") on the default port(5432) using the username and password supplied.

        Extra parameters to be added to the JDBC URL can be included in the urlExtras parameter.

        Parameters:
        databaseName - databaseName
        username - username
        password - password
        urlExtras - urlExtras
        Throws:
        java.sql.SQLException - database errors
    • Method Detail

      • clone

        public DBDatabase clone()
                         throws java.lang.CloneNotSupportedException
        Description copied from class: DBDatabase
        Clones the DBDatabase.

        Support DBvolution at Patreon

        Overrides:
        clone in class DBDatabase
        Returns:
        a clone of the DBDatabase.
        Throws:
        java.lang.CloneNotSupportedException - not likely
      • loadFromCSVFile

        public int loadFromCSVFile​(DBRow table,
                                   java.io.File file,
                                   java.lang.String delimiter,
                                   java.lang.String nullValue,
                                   java.lang.String escapeCharacter,
                                   java.lang.String quoteCharacter)
                            throws java.sql.SQLException
        Assumes that the database and application are on the the same machine.
        Parameters:
        table - the table to be loaded
        file - the file to load data from
        delimiter - the separator between the values of each row
        nullValue - the string that represents NULL in this file.
        escapeCharacter - the character that escapes special values
        quoteCharacter - the character the surrounds strings.

        Support DBvolution at Patreon

        Returns:
        either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing 1 Database exceptions may be thrown
        Throws:
        java.sql.SQLException - database exceptions may be thrown.
      • createDatabase

        public void createDatabase​(java.lang.String databaseName)
                            throws java.sql.SQLException
        Create a new database/schema on this database server.

        Generally requires all sorts of privileges and is best performed by database administrator (DBA).

        Parameters:
        databaseName - the name of the new database
        Throws:
        java.sql.SQLException - database exceptions may be thrown
      • createUser

        public void createUser​(java.lang.String username,
                               java.lang.String password)
                        throws java.sql.SQLException
        Create a new database/schema on this database server.

        Generally requires all sorts of privileges and is best performed by database administrator (DBA).

        Parameters:
        username - The user to be created
        password - the password the user will use.
        Throws:
        java.sql.SQLException - database exceptions may be throwns.
      • addDatabaseSpecificFeatures

        protected void addDatabaseSpecificFeatures​(java.sql.Statement stmnt)
                                            throws java.sql.SQLException
        Description copied from class: DBDatabase
        Used By Subclasses To Inject Datatypes, Functions, Etc Into the Database.
        Specified by:
        addDatabaseSpecificFeatures in class DBDatabase
        Parameters:
        stmnt - the statement to use when adding features, DO NOT CLOSE THIS STATEMENT.
        Throws:
        java.sql.SQLException - database exceptions may occur
        See Also:
        PostgresDB, H2DB, SQLiteDB, OracleDB, MSSQLServerDB, MySQLDB
      • addFeatureToFixException

        public DBDatabase.ResponseToException addFeatureToFixException​(java.lang.Exception exp)
                                                                throws java.lang.Exception
        Used to add features in a just-in-time manner.

        During a statement the database may throw an exception because a feature has not yet been added. Use this method to parse the exception and install the required feature.

        The statement will be automatically run after this method exits.

        Overrides:
        addFeatureToFixException in class DBDatabase
        Parameters:
        exp - the exception throw by the database that may need fixing
        Returns:
        the suggested response to this exception
        Throws:
        java.sql.SQLException - accessing the database may cause exceptions
        SQLException - accessing the database may cause exceptions
        java.lang.Exception