I've got a mysql query that predefines a column in the query that is meant to be an integer type:
SELECT id, 1 AS item_id from table WHERE other_id = 1;
When I retrieve this statement in Java I get a ClassCastException:
java.lang.Float cannot be cast to java.lang.Integer
I've tried casting the value to an integer using the UNSIGNED type:
SELECT id, CAST(1 AS UNSIGNED) AS item_id from table WHERE other_id = 1;
But that gives me the same type of error:
java.math.BigInteger cannot be cast to java.lang.Integer
I'm not sure if this is something I need to fix in Java or in the actual sql statement, but i'd like to get the value back as an Integer.
The only place in Java I can imagine in causing the problem could be that I am bringing the value in as an object because i'm storing the record as a hashmap and don't know the exact column type. Unfortunately I can't change this.
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
while(resultSet.next()) {
HashMap<String, Object> row = new HashMap<String, Object>();
for(int i=1; i<=columnCount; i++) {
row.put(metaData.getColumnName(i), resultSet.getObject(i));
}
list.add(row);
}
Float(which seemed to be what you were getting), you can callintValue()to get anint.resultSet.getIntnot work when anintis what I was expecting. I don't know what you're doing to get ambiguously typed values.int value = ((Number)rs.getObject("field")).intValue().