0

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)
4
  • perhaps getting the array as rows would simplify your code a bit : SELECT unnest(importanza) FROM eventi_avversi WHERE nomecentro = ? Commented Aug 2, 2021 at 11:00
  • @JimJones ok, I'll try it, thanks Commented Aug 2, 2021 at 11:11
  • 2
    Please edit the question including the complete error message, and the stack trace if possible. Maybe it contains a hint about what you are doing wrong. Commented Aug 2, 2021 at 11:14
  • 1
    It seems that you obtain a Short[] from the query and java does not cast directly short to int as you can see in the answers provided here stackoverflow.com/q/33868133/2553194 If you need an Integer[], you've got to convert explicitly and manually the Short[] to Integer[]. This can be achieved easily using Stream operations. Commented Aug 2, 2021 at 12:06

1 Answer 1

1

I've tryed to convert the Short array into an int array like a user says in the comment and it works.

This is the code:

...
while(rs.next()) {
    Array arrImportanza = rs.getArray("importanza");
    Short[] s = (Short[])arrImportanza.getArray();
    importanza = new int[s.length];
    for(int i=0; i<s.length; i++)
        importanza[i] = (int) s[i];
    return importanza;
}
...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.