Package org.kiwiproject.jdbc
Class KiwiJdbc
- java.lang.Object
-
- org.kiwiproject.jdbc.KiwiJdbc
-
public class KiwiJdbc extends Object
JDBC utilities.
-
-
Constructor Summary
Constructors Constructor Description KiwiJdbc()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static DatedateFromTimestamp(ResultSet rs, String columnName)static DatedateFromTimestamp(Timestamp timestamp)static DoubledoubleValueOrNull(ResultSet rs, String columnName)Simplifies the JDBC silliness whereby getting a double value that is actuallynullreturns zero, and you have to check if the last value was actuallynullusing theResultSet.wasNull()method.static <T extends Enum<T>>
Optional<T>enumValueOrEmpty(ResultSet rs, String columnName, Class<T> enumType)Returns an enum constant of the given type from the specified column in the result set.static <T extends Enum<T>>
TenumValueOrNull(ResultSet rs, String columnName, Class<T> enumType)Returns an enum constant of the given type from the specified column in the result set.static longepochMillisFromTimestamp(ResultSet rs, String columnName)Convert the timestamp column given bycolumnNamein theResultSetto milliseconds from the epoch.static longepochMillisFromTimestamp(Timestamp timestamp)Convert the givenTimestampto milliseconds from the epoch.static InstantinstantFromTimestamp(ResultSet rs, String columnName)static InstantinstantFromTimestamp(Timestamp timestamp)static IntegerintValueOrNull(ResultSet rs, String columnName)Simplifies the JDBC silliness whereby getting an int value that is actuallynullreturns zero, and you have to check if the last value was actuallynullusing theResultSet.wasNull()method.static LocalDateTimelocalDateTimeFromTimestamp(ResultSet rs, String columnName)static LocalDateTimelocalDateTimeFromTimestamp(Timestamp timestamp)Returns aLocalDateTimefrom the givenTimestamp.static LonglongValueOrNull(ResultSet rs, String columnName)Simplifies the JDBC silliness whereby getting a long value that is actuallynullreturns zero, and you have to check if the last value was actuallynullusing theResultSet.wasNull()method.static voidnullSafeSetDateAsTimestamp(PreparedStatement ps, int parameterIndex, Date value)Sets theDatevalue in a null-safe manner by using thePreparedStatement.setNull(int, int)method fornullvalues.static voidnullSafeSetDateAsTimestamp(PreparedStatement ps, int parameterIndex, Date value, int type)Sets theDatevalue in a null-safe manner by using thePreparedStatement.setNull(int, int)method fornullvalues.static voidnullSafeSetDouble(PreparedStatement ps, int parameterIndex, Double value)Sets theDoublevalue in a null-safe manner by using thePreparedStatement.setNull(int, int)method fornullvalues.static voidnullSafeSetDouble(PreparedStatement ps, int parameterIndex, Double value, int type)Sets theDoublevalue in a null-safe manner by using thePreparedStatement.setNull(int, int)method fornullvalues.static voidnullSafeSetInt(PreparedStatement ps, int parameterIndex, Integer value)Sets theIntegervalue in a null-safe manner by using thePreparedStatement.setNull(int, int)method fornullvalues.static voidnullSafeSetInt(PreparedStatement ps, int parameterIndex, Integer value, int type)Sets theIntegervalue in a null-safe manner by using thePreparedStatement.setNull(int, int)method fornullvalues.static voidnullSafeSetLong(PreparedStatement ps, int parameterIndex, Long value)Sets theLongvalue in a null-safe manner by using thePreparedStatement.setNull(int, int)method fornullvalues.static voidnullSafeSetLong(PreparedStatement ps, int parameterIndex, Long value, int type)Sets theLongvalue in a null-safe manner by using thePreparedStatement.setNull(int, int)method fornullvalues.static voidnullSafeSetString(PreparedStatement ps, int parameterIndex, String value)Sets theStringvalue in a null-safe manner by using thePreparedStatement.setNull(int, int)method fornullvalues.static voidnullSafeSetString(PreparedStatement ps, int parameterIndex, String value, int type)Sets theStringvalue in a null-safe manner by using thePreparedStatement.setNull(int, int)method fornullvalues.static voidnullSafeSetTimestamp(PreparedStatement ps, int parameterIndex, Timestamp value)Sets theTimestampvalue in a null-safe manner by using thePreparedStatement.setNull(int, int)method fornullvalues.static voidnullSafeSetTimestamp(PreparedStatement ps, int parameterIndex, Timestamp value, int type)Sets theTimestampvalue in a null-safe manner by using thePreparedStatement.setNull(int, int)method fornullvalues.static TimestamptimestampFromInstant(Instant instant)static TimestamptimestampFromZonedDateTime(ZonedDateTime zonedDateTime)Returns aTimestampfrom the givenZonedDateTime.static ZonedDateTimeutcZonedDateTimeFromEpochMilli(long epochMilli)Returns aZonedDateTimein UTC from the given number of milliseconds since the epoch.static ZonedDateTimeutcZonedDateTimeFromTimestamp(ResultSet rs, String columnName)Returns aZonedDateTimein UTC from the specified column in theResultSet.static ZonedDateTimeutcZonedDateTimeFromTimestamp(Timestamp timestamp)Returns aZonedDateTimein UTC from the specifiedTimestamp.static ZonedDateTimezonedDateTimeFromTimestamp(ResultSet rs, String columnName, ZoneId zoneId)Returns aZonedDateTimein the specified time zone from the specified column in theResultSet.static ZonedDateTimezonedDateTimeFromTimestamp(Timestamp timestamp, ZoneId zoneId)Returns aZonedDateTimein the specified time zone from the givenTimestamp.
-
-
-
Method Detail
-
epochMillisFromTimestamp
public static long epochMillisFromTimestamp(ResultSet rs, String columnName) throws SQLException
Convert the timestamp column given bycolumnNamein theResultSetto milliseconds from the epoch.- Parameters:
rs- the ResultSetcolumnName- the timestamp column name- Returns:
- epoch milliseconds
- Throws:
SQLException- if there is a database problemIllegalArgumentException- if the timestamp returned from the ResultSet is null- See Also:
Instant.toEpochMilli()
-
epochMillisFromTimestamp
public static long epochMillisFromTimestamp(Timestamp timestamp)
Convert the givenTimestampto 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
- Parameters:
rs- the ResultSetcolumnName- the timestamp column name- Returns:
- the converted Date or
nullif the column wasNULL - Throws:
SQLException- if there is a database problem
-
dateFromTimestamp
public static Date dateFromTimestamp(Timestamp timestamp)
- Parameters:
timestamp- the timestamp to convert- Returns:
- a
Dateor null if the timestamp is null
-
instantFromTimestamp
public static Instant instantFromTimestamp(ResultSet rs, String columnName) throws SQLException
- Parameters:
rs- the ResultSetcolumnName- the timestamp column name- Returns:
- the converted Instant or
nullif the column wasNULL - Throws:
SQLException- if there is a database problem
-
instantFromTimestamp
public static Instant instantFromTimestamp(Timestamp timestamp)
- Parameters:
timestamp- the timestamp to convert- Returns:
- an Instant or or
nullif the timestamp isnull
-
localDateTimeFromTimestamp
public static LocalDateTime localDateTimeFromTimestamp(ResultSet rs, String columnName) throws SQLException
- Parameters:
rs- the ResultSetcolumnName- the timestamp column name- Returns:
- the converted LocalDateTime or
nullif the column wasNULL - Throws:
SQLException- if there is a database problem
-
localDateTimeFromTimestamp
public static LocalDateTime localDateTimeFromTimestamp(Timestamp timestamp)
Returns aLocalDateTimefrom the givenTimestamp.- Parameters:
timestamp- the timestamp to convert- Returns:
- a LocalDateTime or or
nullif the timestamp isnull
-
utcZonedDateTimeFromTimestamp
public static ZonedDateTime utcZonedDateTimeFromTimestamp(ResultSet rs, String columnName) throws SQLException
Returns aZonedDateTimein UTC from the specified column in theResultSet.- Parameters:
rs- the ResultSetcolumnName- the timestamp column name- Returns:
- a UTC ZonedDateTime or
nullif the column wasNULL - Throws:
SQLException- if there is a database problem
-
zonedDateTimeFromTimestamp
public static ZonedDateTime zonedDateTimeFromTimestamp(ResultSet rs, String columnName, ZoneId zoneId) throws SQLException
Returns aZonedDateTimein the specified time zone from the specified column in theResultSet.- Parameters:
rs- the ResultSetcolumnName- the timestamp column namezoneId- the time zone ID- Returns:
- a ZonedDateTime in the specified zone, or
nullif the column wasNULL - Throws:
SQLException- if there is a database problem
-
utcZonedDateTimeFromTimestamp
public static ZonedDateTime utcZonedDateTimeFromTimestamp(Timestamp timestamp)
Returns aZonedDateTimein UTC from the specifiedTimestamp.- Parameters:
timestamp- the timestamp to convert- Returns:
- a UTC ZonedDateTime or
nullif the timestamp isnull
-
utcZonedDateTimeFromEpochMilli
public static ZonedDateTime utcZonedDateTimeFromEpochMilli(long epochMilli)
Returns aZonedDateTimein 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 aZonedDateTimein the specified time zone from the givenTimestamp.- Parameters:
timestamp- the timestamp to convertzoneId- the time zone ID- Returns:
- a ZonedDateTime in the specified zone, or
nullif the timestamp isnull
-
timestampFromInstant
public static Timestamp timestampFromInstant(Instant instant)
- Parameters:
instant- the Instant to convert- Returns:
- an Instant or
nullif the instant isnull
-
timestampFromZonedDateTime
public static Timestamp timestampFromZonedDateTime(ZonedDateTime zonedDateTime)
Returns aTimestampfrom the givenZonedDateTime.- Parameters:
zonedDateTime- the ZonedDateTime to convert- Returns:
- a ZonedDateTime or
nullif the zonedDateTime isnull
-
longValueOrNull
public static Long longValueOrNull(ResultSet rs, String columnName) throws SQLException
Simplifies the JDBC silliness whereby getting a long value that is actuallynullreturns zero, and you have to check if the last value was actuallynullusing theResultSet.wasNull()method.- Parameters:
rs- the ResultSetcolumnName- the column name- Returns:
- a
Longornull - 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 actuallynullreturns zero, and you have to check if the last value was actuallynullusing theResultSet.wasNull()method.- Parameters:
rs- the ResultSetcolumnName- the column name- Returns:
- an
Integerornull - 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 actuallynullreturns zero, and you have to check if the last value was actuallynullusing theResultSet.wasNull()method.- Parameters:
rs- the ResultSetcolumnName- the column name- Returns:
- a
Longornull - Throws:
SQLException- if there is a database problem
-
enumValueOrNull
public static <T extends Enum<T>> T enumValueOrNull(ResultSet rs, String columnName, Class<T> enumType) throws SQLException
Returns an enum constant of the given type from the specified column in the result set.- Type Parameters:
T- the enum type parameter- Parameters:
rs- the ResultSetcolumnName- the column nameenumType- the enum class- Returns:
- an enum constant of type
enumTypeornullif the database value was NULL - Throws:
SQLException- if there is a database problemIllegalArgumentException- if the value from the ResultSet is an invalid enum constant- See Also:
Enum.valueOf(Class, String)
-
enumValueOrEmpty
public static <T extends Enum<T>> Optional<T> enumValueOrEmpty(ResultSet rs, String columnName, Class<T> enumType) throws SQLException
Returns an enum constant of the given type from the specified column in the result set.- Type Parameters:
T- the enum type parameter- Parameters:
rs- the ResultSetcolumnName- the column nameenumType- the enum class- Returns:
- an enum constant of type
enumTypeor an emptyOptionalif the database value was NULL - Throws:
SQLException- if there is a database problemIllegalArgumentException- if the value from the ResultSet is an invalid enum constant- See Also:
Enum.valueOf(Class, String)
-
nullSafeSetTimestamp
public static void nullSafeSetTimestamp(PreparedStatement ps, int parameterIndex, Timestamp value) throws SQLException
Sets theTimestampvalue in a null-safe manner by using thePreparedStatement.setNull(int, int)method fornullvalues. UsesTypes.TIMESTAMPas the SQL type.- Parameters:
ps- the PreparedStatement to set the value onparameterIndex- the positional index of the value in the SQL statementvalue- the value to set, ornull- Throws:
SQLException- if there is a database problem
-
nullSafeSetTimestamp
public static void nullSafeSetTimestamp(PreparedStatement ps, int parameterIndex, Timestamp value, int type) throws SQLException
Sets theTimestampvalue in a null-safe manner by using thePreparedStatement.setNull(int, int)method fornullvalues. Specify the exact SQL timestamp type using one of the values inTypes.- Parameters:
ps- the PreparedStatement to set the value onparameterIndex- the positional index of the value in the SQL statementvalue- the value to set, ornulltype- the SQL type to use when setting anullvalue- 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 theDatevalue in a null-safe manner by using thePreparedStatement.setNull(int, int)method fornullvalues. UsesTypes.TIMESTAMPas the SQL type.- Parameters:
ps- the PreparedStatement to set the value onparameterIndex- the positional index of the value in the SQL statementvalue- the value to set, ornull- Throws:
SQLException- if there is a database problem
-
nullSafeSetDateAsTimestamp
public static void nullSafeSetDateAsTimestamp(PreparedStatement ps, int parameterIndex, Date value, int type) throws SQLException
Sets theDatevalue in a null-safe manner by using thePreparedStatement.setNull(int, int)method fornullvalues. Specify the exact SQL timestamp type using one of the values inTypes.- Parameters:
ps- the PreparedStatement to set the value onparameterIndex- the positional index of the value in the SQL statementvalue- the value to set, ornulltype- the SQL type to use when setting anullvalue- Throws:
SQLException- if there is a database problem- See Also:
Types
-
nullSafeSetInt
public static void nullSafeSetInt(PreparedStatement ps, int parameterIndex, Integer value) throws SQLException
Sets theIntegervalue in a null-safe manner by using thePreparedStatement.setNull(int, int)method fornullvalues. UsesTypes.INTEGERas the SQL type.- Parameters:
ps- the PreparedStatement to set the value onparameterIndex- the positional index of the value in the SQL statementvalue- the value to set, ornull- Throws:
SQLException- if there is a database problem
-
nullSafeSetInt
public static void nullSafeSetInt(PreparedStatement ps, int parameterIndex, Integer value, int type) throws SQLException
Sets theIntegervalue in a null-safe manner by using thePreparedStatement.setNull(int, int)method fornullvalues. Specify the exact SQL int type using one of the values inTypes.- Parameters:
ps- the PreparedStatement to set the value onparameterIndex- the positional index of the value in the SQL statementvalue- the value to set, ornulltype- the SQL type to use when setting anullvalue- 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 theLongvalue in a null-safe manner by using thePreparedStatement.setNull(int, int)method fornullvalues. UsesTypes.BIGINTas the SQL type.- Parameters:
ps- the PreparedStatement to set the value onparameterIndex- the positional index of the value in the SQL statementvalue- the value to set, ornull- Throws:
SQLException- if there is a database problem
-
nullSafeSetLong
public static void nullSafeSetLong(PreparedStatement ps, int parameterIndex, Long value, int type) throws SQLException
Sets theLongvalue in a null-safe manner by using thePreparedStatement.setNull(int, int)method fornullvalues. Specify the exact SQL long type using one of the values inTypes.- Parameters:
ps- the PreparedStatement to set the value onparameterIndex- the positional index of the value in the SQL statementvalue- the value to set, ornulltype- the SQL type to use when setting anullvalue- 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 theDoublevalue in a null-safe manner by using thePreparedStatement.setNull(int, int)method fornullvalues. UsesTypes.DOUBLEas the SQL type.- Parameters:
ps- the PreparedStatement to set the value onparameterIndex- the positional index of the value in the SQL statementvalue- the value to set, ornull- Throws:
SQLException- if there is a database problem
-
nullSafeSetDouble
public static void nullSafeSetDouble(PreparedStatement ps, int parameterIndex, Double value, int type) throws SQLException
Sets theDoublevalue in a null-safe manner by using thePreparedStatement.setNull(int, int)method fornullvalues. Specify the exact SQL double type using one of the values inTypes.- Parameters:
ps- the PreparedStatement to set the value onparameterIndex- the positional index of the value in the SQL statementvalue- the value to set, ornulltype- the SQL type to use when setting anullvalue- 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 theStringvalue in a null-safe manner by using thePreparedStatement.setNull(int, int)method fornullvalues. UsesTypes.VARCHARas the SQL type.- Parameters:
ps- the PreparedStatement to set the value onparameterIndex- the positional index of the value in the SQL statementvalue- the value to set, ornull- Throws:
SQLException- if there is a database problem
-
nullSafeSetString
public static void nullSafeSetString(PreparedStatement ps, int parameterIndex, String value, int type) throws SQLException
Sets theStringvalue in a null-safe manner by using thePreparedStatement.setNull(int, int)method fornullvalues. Specify the exact SQL string type using one of the values inTypes.- Parameters:
ps- the PreparedStatement to set the value onparameterIndex- the positional index of the value in the SQL statementvalue- the value to set, ornulltype- the SQL type as an int, seeSQLType- Throws:
SQLException- if there is a database problem- See Also:
SQLType,JDBCType
-
-