Class KiwiJdbc


  • public class KiwiJdbc
    extends Object
    JDBC utilities.
    • Constructor Detail

      • KiwiJdbc

        public KiwiJdbc()
    • Method Detail

      • epochMillisFromTimestamp

        public static long epochMillisFromTimestamp​(ResultSet rs,
                                                    String columnName)
                                             throws SQLException
        Convert the timestamp column given by columnName in the ResultSet to milliseconds from the epoch.
        Parameters:
        rs - the ResultSet
        columnName - the timestamp column name
        Returns:
        epoch milliseconds
        Throws:
        SQLException - if there is a database problem
        IllegalArgumentException - if the timestamp returned from the ResultSet is null
        See Also:
        Instant.toEpochMilli()
      • epochMillisFromTimestamp

        public static long epochMillisFromTimestamp​(Timestamp timestamp)
        Convert the given Timestamp to milliseconds from the epoch.
        Parameters:
        timestamp - the timestamp to convert
        Returns:
        epoch milliseconds
        Throws:
        IllegalArgumentException - if timestamp is null
        See Also:
        Instant.toEpochMilli()
      • dateFromTimestamp

        public static Date dateFromTimestamp​(ResultSet rs,
                                             String columnName)
                                      throws SQLException
        Returns a Date from the specified Timestamp column in the given ResultSet.
        Parameters:
        rs - the ResultSet
        columnName - the timestamp column name
        Returns:
        the converted Date or null if the column was NULL
        Throws:
        SQLException - if there is a database problem
      • dateFromTimestamp

        public static Date dateFromTimestamp​(Timestamp timestamp)
        Returns a Date from the given Timestamp.
        Parameters:
        timestamp - the timestamp to convert
        Returns:
        a Date or null if the timestamp is null
      • instantFromTimestamp

        public static Instant instantFromTimestamp​(ResultSet rs,
                                                   String columnName)
                                            throws SQLException
        Returns a Instant from the specified Timestamp column in the given ResultSet.
        Parameters:
        rs - the ResultSet
        columnName - the timestamp column name
        Returns:
        the converted Instant or null if the column was NULL
        Throws:
        SQLException - if there is a database problem
      • instantFromTimestamp

        public static Instant instantFromTimestamp​(Timestamp timestamp)
        Returns an Instant from the given Timestamp.
        Parameters:
        timestamp - the timestamp to convert
        Returns:
        an Instant or or null if the timestamp is null
      • localDateTimeFromTimestamp

        public static LocalDateTime localDateTimeFromTimestamp​(ResultSet rs,
                                                               String columnName)
                                                        throws SQLException
        Returns a LocalDateTime from the specified Timestamp column in the given ResultSet.
        Parameters:
        rs - the ResultSet
        columnName - the timestamp column name
        Returns:
        the converted LocalDateTime or null if the column was NULL
        Throws:
        SQLException - if there is a database problem
      • localDateTimeFromTimestamp

        public static LocalDateTime localDateTimeFromTimestamp​(Timestamp timestamp)
        Returns a LocalDateTime from the given Timestamp.
        Parameters:
        timestamp - the timestamp to convert
        Returns:
        a LocalDateTime or or null if the timestamp is null
      • utcZonedDateTimeFromTimestamp

        public static ZonedDateTime utcZonedDateTimeFromTimestamp​(ResultSet rs,
                                                                  String columnName)
                                                           throws SQLException
        Returns a ZonedDateTime in UTC from the specified column in the ResultSet.
        Parameters:
        rs - the ResultSet
        columnName - the timestamp column name
        Returns:
        a UTC ZonedDateTime or null if the column was NULL
        Throws:
        SQLException - if there is a database problem
      • zonedDateTimeFromTimestamp

        public static ZonedDateTime zonedDateTimeFromTimestamp​(ResultSet rs,
                                                               String columnName,
                                                               ZoneId zoneId)
                                                        throws SQLException
        Returns a ZonedDateTime in the specified time zone from the specified column in the ResultSet.
        Parameters:
        rs - the ResultSet
        columnName - the timestamp column name
        zoneId - the time zone ID
        Returns:
        a ZonedDateTime in the specified zone, or null if the column was NULL
        Throws:
        SQLException - if there is a database problem
      • utcZonedDateTimeFromTimestamp

        public static ZonedDateTime utcZonedDateTimeFromTimestamp​(Timestamp timestamp)
        Returns a ZonedDateTime in UTC from the specified Timestamp.
        Parameters:
        timestamp - the timestamp to convert
        Returns:
        a UTC ZonedDateTime or null if the timestamp is null
      • utcZonedDateTimeFromEpochMilli

        public static ZonedDateTime utcZonedDateTimeFromEpochMilli​(long epochMilli)
        Returns a ZonedDateTime in UTC from the given number of milliseconds since the epoch.

        Note that this method will always have zero as the nano-of-second.

        Parameters:
        epochMilli - number of milliseconds since the epoch
        Returns:
        a UTC ZonedDateTime
      • zonedDateTimeFromTimestamp

        public static ZonedDateTime zonedDateTimeFromTimestamp​(Timestamp timestamp,
                                                               ZoneId zoneId)
        Returns a ZonedDateTime in the specified time zone from the given Timestamp.
        Parameters:
        timestamp - the timestamp to convert
        zoneId - the time zone ID
        Returns:
        a ZonedDateTime in the specified zone, or null if the timestamp is null
      • timestampFromInstant

        public static Timestamp timestampFromInstant​(Instant instant)
        Returns a Timestamp from the given Instant.
        Parameters:
        instant - the Instant to convert
        Returns:
        an Instant or null if the instant is null
      • timestampFromZonedDateTime

        public static Timestamp timestampFromZonedDateTime​(ZonedDateTime zonedDateTime)
        Returns a Timestamp from the given ZonedDateTime.
        Parameters:
        zonedDateTime - the ZonedDateTime to convert
        Returns:
        a ZonedDateTime or null if the zonedDateTime is null
      • longValueOrNull

        public static Long longValueOrNull​(ResultSet rs,
                                           String columnName)
                                    throws SQLException
        Simplifies the JDBC silliness whereby getting a long value that is actually null returns zero, and you have to check if the last value was actually null using the ResultSet.wasNull() method.
        Parameters:
        rs - the ResultSet
        columnName - the column name
        Returns:
        a Long or null
        Throws:
        SQLException - if there is a database problem
      • intValueOrNull

        public static Integer intValueOrNull​(ResultSet rs,
                                             String columnName)
                                      throws SQLException
        Simplifies the JDBC silliness whereby getting an int value that is actually null returns zero, and you have to check if the last value was actually null using the ResultSet.wasNull() method.
        Parameters:
        rs - the ResultSet
        columnName - the column name
        Returns:
        an Integer or null
        Throws:
        SQLException - if there is a database problem
      • doubleValueOrNull

        public static Double doubleValueOrNull​(ResultSet rs,
                                               String columnName)
                                        throws SQLException
        Simplifies the JDBC silliness whereby getting a double value that is actually null returns zero, and you have to check if the last value was actually null using the ResultSet.wasNull() method.
        Parameters:
        rs - the ResultSet
        columnName - the column name
        Returns:
        a Long or null
        Throws:
        SQLException - if there is a database problem
      • nullSafeSetTimestamp

        public static void nullSafeSetTimestamp​(PreparedStatement ps,
                                                int parameterIndex,
                                                Timestamp value,
                                                int type)
                                         throws SQLException
        Sets the Timestamp value in a null-safe manner by using the PreparedStatement.setNull(int, int) method for null values. Specify the exact SQL timestamp type using one of the values in Types.
        Parameters:
        ps - the PreparedStatement to set the value on
        parameterIndex - the positional index of the value in the SQL statement
        value - the value to set, or null
        type - the SQL type to use when setting a null value
        Throws:
        SQLException - if there is a database problem
        See Also:
        Types
      • nullSafeSetDateAsTimestamp

        public static void nullSafeSetDateAsTimestamp​(PreparedStatement ps,
                                                      int parameterIndex,
                                                      Date value)
                                               throws SQLException
        Sets the Date value in a null-safe manner by using the PreparedStatement.setNull(int, int) method for null values. Uses Types.TIMESTAMP as the SQL type.
        Parameters:
        ps - the PreparedStatement to set the value on
        parameterIndex - the positional index of the value in the SQL statement
        value - the value to set, or null
        Throws:
        SQLException - if there is a database problem
      • nullSafeSetDateAsTimestamp

        public static void nullSafeSetDateAsTimestamp​(PreparedStatement ps,
                                                      int parameterIndex,
                                                      Date value,
                                                      int type)
                                               throws SQLException
        Sets the Date value in a null-safe manner by using the PreparedStatement.setNull(int, int) method for null values. Specify the exact SQL timestamp type using one of the values in Types.
        Parameters:
        ps - the PreparedStatement to set the value on
        parameterIndex - the positional index of the value in the SQL statement
        value - the value to set, or null
        type - the SQL type to use when setting a null value
        Throws:
        SQLException - if there is a database problem
        See Also:
        Types
      • nullSafeSetInt

        public static void nullSafeSetInt​(PreparedStatement ps,
                                          int parameterIndex,
                                          Integer value,
                                          int type)
                                   throws SQLException
        Sets the Integer value in a null-safe manner by using the PreparedStatement.setNull(int, int) method for null values. Specify the exact SQL int type using one of the values in Types.
        Parameters:
        ps - the PreparedStatement to set the value on
        parameterIndex - the positional index of the value in the SQL statement
        value - the value to set, or null
        type - the SQL type to use when setting a null value
        Throws:
        SQLException - if there is a database problem
        See Also:
        Types
      • nullSafeSetLong

        public static void nullSafeSetLong​(PreparedStatement ps,
                                           int parameterIndex,
                                           Long value)
                                    throws SQLException
        Sets the Long value in a null-safe manner by using the PreparedStatement.setNull(int, int) method for null values. Uses Types.BIGINT as the SQL type.
        Parameters:
        ps - the PreparedStatement to set the value on
        parameterIndex - the positional index of the value in the SQL statement
        value - the value to set, or null
        Throws:
        SQLException - if there is a database problem
      • nullSafeSetLong

        public static void nullSafeSetLong​(PreparedStatement ps,
                                           int parameterIndex,
                                           Long value,
                                           int type)
                                    throws SQLException
        Sets the Long value in a null-safe manner by using the PreparedStatement.setNull(int, int) method for null values. Specify the exact SQL long type using one of the values in Types.
        Parameters:
        ps - the PreparedStatement to set the value on
        parameterIndex - the positional index of the value in the SQL statement
        value - the value to set, or null
        type - the SQL type to use when setting a null value
        Throws:
        SQLException - if there is a database problem
        See Also:
        Types
      • nullSafeSetDouble

        public static void nullSafeSetDouble​(PreparedStatement ps,
                                             int parameterIndex,
                                             Double value)
                                      throws SQLException
        Sets the Double value in a null-safe manner by using the PreparedStatement.setNull(int, int) method for null values. Uses Types.DOUBLE as the SQL type.
        Parameters:
        ps - the PreparedStatement to set the value on
        parameterIndex - the positional index of the value in the SQL statement
        value - the value to set, or null
        Throws:
        SQLException - if there is a database problem
      • nullSafeSetDouble

        public static void nullSafeSetDouble​(PreparedStatement ps,
                                             int parameterIndex,
                                             Double value,
                                             int type)
                                      throws SQLException
        Sets the Double value in a null-safe manner by using the PreparedStatement.setNull(int, int) method for null values. Specify the exact SQL double type using one of the values in Types.
        Parameters:
        ps - the PreparedStatement to set the value on
        parameterIndex - the positional index of the value in the SQL statement
        value - the value to set, or null
        type - the SQL type to use when setting a null value
        Throws:
        SQLException - if there is a database problem
        See Also:
        Types
      • nullSafeSetString

        public static void nullSafeSetString​(PreparedStatement ps,
                                             int parameterIndex,
                                             String value)
                                      throws SQLException
        Sets the String value in a null-safe manner by using the PreparedStatement.setNull(int, int) method for null values. Uses Types.VARCHAR as the SQL type.
        Parameters:
        ps - the PreparedStatement to set the value on
        parameterIndex - the positional index of the value in the SQL statement
        value - the value to set, or null
        Throws:
        SQLException - if there is a database problem
      • nullSafeSetString

        public static void nullSafeSetString​(PreparedStatement ps,
                                             int parameterIndex,
                                             String value,
                                             int type)
                                      throws SQLException
        Sets the String value in a null-safe manner by using the PreparedStatement.setNull(int, int) method for null values. Specify the exact SQL string type using one of the values in Types.
        Parameters:
        ps - the PreparedStatement to set the value on
        parameterIndex - the positional index of the value in the SQL statement
        value - the value to set, or null
        type - the SQL type as an int, see SQLType
        Throws:
        SQLException - if there is a database problem
        See Also:
        SQLType, JDBCType