如何从具有多行的SQL查询中获取非String Array字段

时间:2023-02-05 19:58:12

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

我有VALS DOUBLE ARRAY [2000]字段的SQL(HSQLDB)表我使用查询返回VALS字段的多行如果我尝试将数组作为

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:

我可以通过从Object到String的双重转换获取值,然后将String解析为Double,如:

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 .... ]

有没有办法从Array SQL字段获取数字而没有String转换或多个单行查询?在调试器中,rs.getArray()向我展示了一个完美的数字值JDBCArray ARRAY [0.0E0,0.0E0,0.0E0,0.0E0,0.0E0 ....]

2 个解决方案

#1


1  

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

您不需要转换为String并返回。刚演员:

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

#2


1  

Try the code below:

请尝试以下代码:

Double[] vals = Arrays.stream(array.getArray()) .map(Double::valueOf) .toArray(Double[]::new);

Double [] vals = Arrays.stream(array.getArray())。map(Double :: valueOf).toArray(Double [] :: new);

#1


1  

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

您不需要转换为String并返回。刚演员:

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

#2


1  

Try the code below:

请尝试以下代码:

Double[] vals = Arrays.stream(array.getArray()) .map(Double::valueOf) .toArray(Double[]::new);

Double [] vals = Arrays.stream(array.getArray())。map(Double :: valueOf).toArray(Double [] :: new);