Search code examples
javasqlarrayshsqldb

How can i get non String Array field from SQL query with multiple rows


I have SQL (HSQLDB) table with VALS DOUBLE ARRAY[2000] field I use query that returns multiple rows with VALS field If i try to get array as

Array array = rs.getArray("VALS");
Double[] vals = (Double[]) array.getArray();

i get

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Double;

I can get values with double conversion from Object to String and then parsing String to Double like:

List<Double> values = new ArrayList<Double>();
for (Object o: (Object[])array.getArray()) {
    values.add(Double.parseDouble(o.toString()));
}

But it looks like heavy overhead

Is there any way to get digits from Array SQL field without String conversion or multiple single-row queries? In debugger rs.getArray() shows me a perfect JDBCArray of digital values ARRAY[0.0E0,0.0E0,0.0E0,0.0E0,0.0E0 .... ]


Solution

  • You don't need to convert to String and back. Just cast:

    List<Double> values = new ArrayList<Double>(); 
    for (Object o: (Object[])array.getArray()) {
        values.add((Double) o); 
    }