The main problem is that you do not know how many columns will be in any given query, and therefore you do not know how many types of parameters the data structure should have, and it is impossible to abstract the number of type parameters.
However, there is a data structure that exists in different versions for different numbers of parameters of the type: tuple. (For example, Tuple2, Tuple3, etc.). You can define parameterized display functions for different numbers of parameters that return tuples as follows:
def toDataStructure2[T1, T2](rs: ResultSet)(c1: String, c2: String) = (rs.getObject(c1).asInstanceOf[T1], rs.getObject(c2).asInstanceOf[T2]) def toDataStructure3[T1, T2, T3](rs: ResultSet)(c1: String, c2: String, c3: String) = (rs.getObject(c1).asInstanceOf[T1], rs.getObject(c2).asInstanceOf[T2], rs.getObject(c3).asInstanceOf[T3])
You will need to define them for the number of columns that you expect to have in your tables (max. 22).
It depends, of course, on the fact that using getObject and casting it to a specific type is safe.
In your example, you can use the resulting tuple as follows:
val (a, b) = toDataStructure2[Int, String](javaResultSet)("a", "b")
source share