Package adams.db

Class SQLUtils


  • public class SQLUtils
    extends Object
    Helper class for SQL related operations.
    Author:
    FracPete (fracpete at waikato dot ac dot nz)
    • Field Detail

      • LOGGER

        protected static Logger LOGGER
        the static logger.
    • Constructor Detail

      • SQLUtils

        public SQLUtils()
    • Method Detail

      • isNumeric

        public static boolean isNumeric​(int colType)
        Checks whether the given column type is numeric.
        Parameters:
        colType - the column type
        Returns:
        true if numeric
      • isInteger

        public static boolean isInteger​(int colType)
        Checks whether the given column type is an integer type.
        Parameters:
        colType - the column type
        Returns:
        true if an integer type
      • isString

        public static boolean isString​(int colType)
        Checks whether the given column type represents strings.
        Parameters:
        colType - the column type
        Returns:
        true if a string type
      • isDate

        public static boolean isDate​(int colType)
        Checks whether the given column type represents a date-like type.
        Parameters:
        colType - the column type
        Returns:
        true if a date-like type
      • close

        public static void close​(Statement s)
        Close this statement to avoid memory leaks.
        Parameters:
        s - the statement to close
      • free

        public static void free​(Blob blob)
        Frees the memory associated with the blob object.
        Parameters:
        blob - the blob to free up, ignored if null
      • free

        public static void free​(Clob clob)
        Frees the memory associated with the clob object.
        Parameters:
        clob - the clob to free up, ignored if null
      • closeAll

        public static void closeAll​(ResultSet r)
        Close objects related to this ResultSet. Important because some (most,all?) jdbc drivers do not clean up after themselves, resulting in memory leaks.
        Parameters:
        r - The ResultSet to clean up after
      • closeAll

        public static void closeAll​(SimpleResultSet r)
        Close objects related to this ResultSet.
        Parameters:
        r - The ResultSet to clean up after
      • booleanToTinyInt

        public static int booleanToTinyInt​(boolean b)
        MySQL boolean to tinyint.
        Parameters:
        b - boolean
        Returns:
        tiny int value
      • tinyIntToBoolean

        public static boolean tinyIntToBoolean​(int i)
        MySQL tinyint to boolean.
        Parameters:
        i - tiny int
        Returns:
        boolean
      • backquote

        public static String backquote​(BaseRegExp s)
        Backquotes the regular expression and ensures that it is surrounded by single quotes.
        Parameters:
        s - the regular expression to backquote and enclose
        Returns:
        the processed string
      • backquote

        public static String backquote​(String s)
        Backquotes the string and ensures that it is surrounded by single quotes.
        Parameters:
        s - the string to backquote and enclose
        Returns:
        the processed string
      • getColumnTypes

        public static int[] getColumnTypes​(ResultSet rs)
                                    throws SQLException
        Determines the SQL column types used in the provided resultset.
        Parameters:
        rs - the resultset to inspect
        Returns:
        the SQL column types
        Throws:
        SQLException - if querying the meta-data fails
        See Also:
        Types
      • getColumnTypes

        public static int[] getColumnTypes​(ResultSetMetaData rs)
                                    throws SQLException
        Determines the SQL column types used in the provided resultset.
        Parameters:
        rs - the metadata resultset to inspect
        Returns:
        the SQL column types
        Throws:
        SQLException - if querying the meta-data fails
        See Also:
        Types
      • getColumnNames

        public static String[] getColumnNames​(ResultSet rs)
                                       throws SQLException
        Determines the SQL column names used in the provided resultset.
        Parameters:
        rs - the resultset to inspect
        Returns:
        the SQL column names (or label if present)
        Throws:
        SQLException - if querying the meta-data fails
      • getColumnNames

        public static String[] getColumnNames​(ResultSetMetaData rs)
                                       throws SQLException
        Determines the SQL column names used in the provided resultset.
        Parameters:
        rs - the meta-data resultset to inspect
        Returns:
        the SQL column names (or label if present)
        Throws:
        SQLException - if querying the meta-data fails