2

Hello to everyone i'm stucked with this simple problem.

i'm trying to load an integer array from my Postgres DB.

My Code:

Array a = rs.getArray("my_field");

int[] b = (int[])a.getArray();

The Exception Displayed:

java.lang.ClassCastException: class [Ljava.lang.Integer; cannot be cast to class [I ([Ljava.lang.Integer; and [I are in module java.base of loader 'bootstrap')

How can i convert java.sql.Array into an int[] array or into an arraylist?

1
  • 1
    Unrelated to your question, but: Postgres 9.3 is no longer supported you should plan an upgrade as soon as possible. Commented Oct 28, 2020 at 11:00

1 Answer 1

1

You need to cast it to an array of Integer:

Array a = rs.getArray("my_field");
Integer[] b = (Integer[])a.getArray();

Note that a will be null if the column is null in the database, so you should check that before calling a.getArray().

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.