Skip to main content

DB2 type support

foundations-jdbc covers IBM DB2's type system, including DECFLOAT, double-byte character types (GRAPHIC/VARGRAPHIC/DBCLOB), XML, and ROWID.

Integer types

DB2 TypeJava TypeNotes
SMALLINTShort16-bit integer
INTEGER / INTInteger32-bit integer
BIGINTLong64-bit integer
val smallType: Db2Type<Short> = Db2Types.smallint
val intType: Db2Type<Int> = Db2Types.integer
val bigType: Db2Type<Long> = Db2Types.bigint

Fixed-point types

DB2 TypeJava TypeNotes
DECIMAL / NUMERICBigDecimalExact numeric
DECIMAL(p,s)BigDecimalWith precision and scale
DECFLOATBigDecimalDB2-specific decimal floating point (16 or 34 digits)
val decType: Db2Type<BigDecimal> = Db2Types.decimal
val preciseType: Db2Type<BigDecimal> = Db2Types.decimalOf(10, 2)
val decfloatType: Db2Type<BigDecimal> = Db2Types.decfloat
val decfloat16: Db2Type<BigDecimal> = Db2Types.decfloatOf(16)

Floating-point types

DB2 TypeJava TypeNotes
REALFloatSingle precision
DOUBLE / FLOATDoubleDouble precision
val realType: Db2Type<Float> = Db2Types.real
val doubleType: Db2Type<Double> = Db2Types.double_

Boolean

DB2 TypeJava TypeNotes
BOOLEANBooleanNative since DB2 11.1
val boolType: Db2Type<Boolean> = Db2Types.boolean_

String types (single-byte)

DB2 TypeJava TypeNotes
CHAR / CHARACTERStringFixed-length
CHAR(n)StringFixed-length with size
VARCHARStringVariable-length
VARCHAR(n)StringVariable-length with size
CLOBStringCharacter Large Object
val charType: Db2Type<String> = Db2Types.char_
val char10: Db2Type<String> = Db2Types.char_Of(10)
val varcharType: Db2Type<String> = Db2Types.varchar
val varchar255: Db2Type<String> = Db2Types.varcharOf(255)
val clobType: Db2Type<String> = Db2Types.clob

String types (double-byte)

DB2 has dedicated types for double-byte character set (DBCS) strings.

DB2 TypeJava TypeNotes
GRAPHICStringFixed-length DBCS
GRAPHIC(n)StringFixed-length DBCS with size
VARGRAPHICStringVariable-length DBCS
VARGRAPHIC(n)StringVariable-length DBCS with size
DBCLOBStringDouble-byte CLOB
val graphicType: Db2Type<String> = Db2Types.graphic
val graphic10: Db2Type<String> = Db2Types.graphicOf(10)
val vargraphicType: Db2Type<String> = Db2Types.vargraphic
val dbclobType: Db2Type<String> = Db2Types.dbclob
note

DB2's JSON_OBJECT does not support GRAPHIC, VARGRAPHIC, or DBCLOB types. JSON serialization is not available for these types.

Binary types

DB2 TypeJava TypeNotes
BINARYbyte[]Fixed-length binary
BINARY(n)byte[]Fixed-length binary with size
VARBINARYbyte[]Variable-length binary
VARBINARY(n)byte[]Variable-length binary with size
BLOBbyte[]Binary Large Object
val binaryType: Db2Type<ByteArray> = Db2Types.binary
val binary16: Db2Type<ByteArray> = Db2Types.binaryOf(16)
val varbinaryType: Db2Type<ByteArray> = Db2Types.varbinary
val blobType: Db2Type<ByteArray> = Db2Types.blob
note

DB2's JSON_OBJECT does not support BINARY, VARBINARY, or BLOB types. JSON serialization is not available for these types.

Date/time types

DB2 TypeJava TypeNotes
DATELocalDateNaive date, no zone
TIMELocalTimeNaive time, no zone
TIMESTAMPLocalDateTimeNaive timestamp, no zone
TIMESTAMP(p)LocalDateTimeNaive timestamp with fractional-second precision
val dateType: Db2Type<LocalDate> = Db2Types.date
val timeType: Db2Type<LocalTime> = Db2Types.time
val tsType: Db2Type<LocalDateTime> = Db2Types.timestamp
val ts6: Db2Type<LocalDateTime> = Db2Types.timestampOf(6)
DB2 has no zone-preserving timestamp type

All of DB2's temporal types are naive wall-clock values: the stored bytes represent a date/time as written, with no associated zone or offset. LocalDate, LocalTime, and LocalDateTime are the Java types that match this "no-zone" semantic exactly.

DB2 does have a TIMESTAMP WITH TIME ZONE syntax in some environments, but support is limited (LUW configurations don't enable it without explicit setup) and the library does not expose a dedicated mapping for it. For application-level "point in time" values, normalize to UTC before writing to a TIMESTAMP column, or store BIGINT epoch milliseconds.

Instant, OffsetDateTime, and ZonedDateTime would misrepresent the stored value. None of their semantics match a naive wall-clock.

Special types

DB2 TypeJava TypeNotes
XMLXmlNative XML support
ROWIDbyte[]DB2 row identifier
val xmlType: Db2Type<Xml> = Db2Types.xml
val rowidType: Db2Type<ByteArray> = Db2Types.rowid
note

DB2's JSON_OBJECT does not support the XML type. JSON serialization is not available for XML columns.

Nullable types

Any type can be made nullable using .opt():

val notNull: Db2Type<Int> = Db2Types.integer
val nullable: Db2Type<Int?> = Db2Types.integer.opt()

Custom domain types

Wrap base types with custom Java types using transform:

// Wrapper type
data class ProductId(val value: Long)

// Create Db2Type from bigint
val productIdType: Db2Type<ProductId> = Db2Types.bigint.transform(::ProductId, ProductId::value)