简体   繁体   English

ClassCastException:java.lang.Object无法强制转换为java.lang.Integer

[英]ClassCastException: java.lang.Object cannot be cast to java.lang.Integer

The root of my problem is that I have a method that handles JDBC queries and releases all connections after the query. 我的问题的根源是我有一个方法来处理JDBC查询并在查询后释放所有连接。 A "ResultSet" is passed back to the calling method. “ResultSet”被传递回调用方法。

I have found that I can't simply pass the ResultSet back to the calling method, because with the ResultSet closed, then any attempts to use it get an Already Closed error. 我发现我不能简单地将ResultSet传递回调用方法,因为关闭了ResultSet,然后任何使用它的尝试都会产生一个已经关闭的错误。

So before I close the resources, I loop through the ResultSet and store it in an ArrayList. 因此,在关闭资源之前,我遍历ResultSet并将其存储在ArrayList中。

Because the method handles any query, I don't know what kind of types are being returned. 因为该方法处理任何查询,我不知道返回什么类型的类型。 Therefore the ArrayList stores generic s. 因此,ArrayList存储通用s。

This works except for one field in one table .. in one database, that is an Integer[] field. 这适用于一个表中的一个字段...在一个数据库中,即Integer []字段。

What I get out of there is a JDBC4Array object, and I have a heck of a time getting that to an Integer[] for storing in the ArrayList. 我从那里得到的是一个JDBC4Array对象,我有一段时间将它传递给Integer []以存储在ArrayList中。 I do need it to be an Integer[]. 我确实需要它是一个Integer []。

This is what I have right now... It's after lots of frustrated banjaxxing it. 这就是我现在所拥有的......经过很多挫败的banjaxxing之后。

While looping through the ResultSet, before the connection is closed, I do this: 在循环连接ResultSet之前,在连接关闭之前,我这样做:

            // For every row in the ResultSet
            while (rs.next()) {
                // Initialize a ITILRow for this ResultSet row
                ITILRow row = new ITILRow();

                // For each column in this row, add that object to the ITILRow
                for (int colNum=1; colNum<=numCols; colNum++) {
                    Object o = rs.getObject(colNum);

                    // JDBC4Array is a real pain in the butt
                    ArrayList<Integer> tmpList = new ArrayList<Integer>();
                    if (o != null) {
                        if (o.getClass().getSimpleName().endsWith("Array")) {
                            // At least at this time, these Arrays are all Integer[]
                            Array a = (Array) o;
                            Integer[] ints = (Integer[]) a.getArray();
                            for (Integer i : ints) {
                                tmpList.add(i);
                            }
                            o = tmpList;
                        }
                    }

                    row.add(o);
                }

                // Add the ITILRow to allRows
                allRows.add(row);
            }

Then, in the calling method... 然后,在调用方法中......

    for (ITILRow row : allRows) {
        ...
        ArrayList comps = (ArrayList) row.getObject(5);
        Integer[] argh = (Integer[]) ((ArrayList<Integer>) comps).toArray();

        ...
    }

And I get: 我得到:

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

Help would be appreciated. 帮助将不胜感激。 I've gotten my brain tied into a knot on this one. 我已经把我的大脑绑在了这个结上。

Thanks, 谢谢,

List#toArray() returns an Object array. List#toArray()返回一个Object数组。 Use List#toArray(T[]) instead. 请改用List#toArray(T[])

Integer[] arg = (Integer[]) comps.toArray(new Integer[comps.size()]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException:无法强制转换java.lang.Integer - java.lang.ClassCastException: java.lang.Integer cannot be cast java.lang.ClassCastException:无法转换为java.lang.Integer - java.lang.ClassCastException: cannot be cast to java.lang.Integer Getting java.lang.ClassCastException: java.lang.Integer cannot be cast to [Ljava.lang.Object Error - Getting java.lang.ClassCastException: java.lang.Integer cannot be cast to [Ljava.lang.Object Error java.lang.ClassCastException:无法强制转换为java.lang.Object - java.lang.ClassCastException: cannot be cast to java.lang.Object ClassCastException:无法转换为java.lang.Integer - ClassCastException : cant be cast to java.lang.Integer ClassCastException:无法将ApiVersionImpl强制转换为java.lang.Integer - ClassCastException: ApiVersionImpl cannot be cast to java.lang.Integer java.lang.ClassCastException:java.lang.Long 不能在 java 1.6 中转换为 java.lang.Integer - java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer in java 1.6 java.lang.ClassCastException:无法在Tableau中将java.lang.String强制转换为java.lang.Integer? - java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer in tableau? java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long in hibernate - java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long in hibernate java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.Long - java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.Long
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM