I have a query that is hitting a table that has an array column. When I am processing my records I would like to take the array column's data and treat it like a string array and grab the first value. I had assumed I could do the following:
while (resultSet.next()) {
val clients = resultSet.getArray("clients")
println(clients[0])
}
But when I do this I get the error:
Error:(34, 3) Kotlin: Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: @InlineOnly public operator inline fun <@OnlyInputTypes K, V> Map.get(key: Int): ??? defined in kotlin.collections @SinceKotlin public operator fun MatchGroupCollection.get(name: String): MatchGroup? defined in kotlin.text
getArray
returns type Array!
so I had assumed I could access values by index. What do I have to do to access this array of values?
Actually,
getArray
returns ajava.sql.Array
.This is not the same as the
kotlin.Array
type.You can use
getArray
on thejava.sql.Array
, which would simply be translated toarray
in Kotlin:to get the actual array as an
Object
/Any
.You can cast it to an
Array<out Any?>
:or to the desired type directly, though you may get some
ClassCastException
s as a result.To stay type-safe, you may want something like
This will:
null
if the array does not containString
snull
if the array is emptyString
if the array contains aString