Class DBUtils


  • public class DBUtils
    extends Object
    Utility methods for connecting to and working with databases.
    Author:
    Jerry Vos
    • Constructor Detail

      • DBUtils

        public DBUtils()
    • Method Detail

      • getDataSource

        public static DataSource getDataSource​(DBConnectionInfo info)
                                        throws ClassNotFoundException
        Gets a data source pointing to a database. The data source is a PoolingDataSource with a connection pool that will automatically grow when all of its members are in use and another is requested.
        Parameters:
        connectionString - the connection string for the database (its URL)
        userName - the username (if necessary)
        password - the username (if necessary)
        driver - the driver to be used for connecting to the database
        Returns:
        a PoolingDataSource
        Throws:
        ClassNotFoundException - when the driver can't be loaded
      • doesTableExist

        public static boolean doesTableExist​(Connection con,
                                             String tableName)
                                      throws SQLException
        Checks if the table of the specified name exists.
        Parameters:
        con - the connection to the database
        tableName - the name of the table
        Returns:
        if the table exists or not
        Throws:
        SQLException - when there is a problem querying the database
      • getColumnNames

        public static List<String> getColumnNames​(Connection con,
                                                  String tableName)
                                           throws SQLException
        Retrieves the names of the columns of the specified table.
        Parameters:
        con - the connection to the database
        tableName - the name of the database's table
        Returns:
        a list of column names
        Throws:
        SQLException
      • validateConnectionSettings

        public static boolean validateConnectionSettings​(DBConnectionInfo info,
                                                         String infoMessage)