The root of my problem is that I have a method that handles JDBC queries and releases all connections after the query. A "ResultSet" is passed back to the calling method.
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.
So before I close the resources, I loop through the ResultSet and store it in an ArrayList.
Because the method handles any query, I don't know what kind of types are being returned. Therefore the ArrayList stores generic s.
This works except for one field in one table .. in one database, that is an Integer[] field.
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. I do need it to be an Integer[].
This is what I have right now... It's after lots of frustrated banjaxxing it.
While looping through the ResultSet, before the connection is closed, I do this:
// 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. Use List#toArray(T[])
instead.
Integer[] arg = (Integer[]) comps.toArray(new Integer[comps.size()]);