In my database I have a table with smallint[] values. I want to obtain all the values inside this array that match with an id I pass through argument, but when I execute the query it runs me a ClassCastException. What could be the problem?
I think I'm missing something while casting the array value to Integer[].
I've found very few tutorials that teach how to get data from a PostgreSQL array. so I don't know what I'm doing wrong.
Can someone help me?
This is the code, the println and the foreach are only used for """debug""" function.
Integer[] getImportanzaEvento(String nomeCentro) {
Integer[] valImportanza = null;
String qGetImportanzaEvento = "SELECT importanza FROM eventi_avversi WHERE nomecentro = ?";
PreparedStatement pstmt;
ResultSet rs;
try {
openConnection();
pstmt = connection.prepareStatement(qGetImportanzaEvento);
pstmt.setString(1, nomeCentro);
rs = pstmt.executeQuery();
while(rs.next()) {
Array importanza = rs.getArray(1);
valImportanza = (Integer[])importanza.getArray();
for(Integer s : valImportanza)
System.out.println(s);
return valImportanza;
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
closeConnection();
}
return valImportanza;
}
This is the exception:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: class [Ljava.lang.Short; cannot be cast to class [Ljava.lang.Integer; ([Ljava.lang.Short; and [Ljava.lang.Integer; are in module java.base of loader 'bootstrap')
at server.DB_Engine.getImportanzaEvento(DB_Engine.java:858)
at server.ServerImpl.getImportanzaEvento(ServerImpl.java:308)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:359)
at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200)
at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196)
SELECT unnest(importanza) FROM eventi_avversi WHERE nomecentro = ?