Re: Retrieving arrays

Поиск
Список
Период
Сортировка
От Markus Schaber
Тема Re: Retrieving arrays
Дата
Msg-id 452CE6B0.10409@logix-tt.com
обсуждение исходный текст
Ответ на Retrieving arrays  (Bendik Rognlien Johansen <bensmailinglists@gmail.com>)
Ответы Re: Retrieving arrays
Список pgsql-jdbc
Hi, bendik,

Bendik Rognlien Johansen wrote:

> I wrote this method to read arrays from my result set.
>
> public class MyClass {
>   public <T> T[] getArray(ResultSet rs, String column) throws Exception {
>     if(rs.getArray(column) != null) {
>       return (T[]) rs.getArray(column).getArray();
>     }
>     return null;
>   }
> }

It seems that you misunderstand the concepts of Generics.

Your method will call the getArray() method, and then try to cast (not
convert) whatever this method returns into an T[]. And, due to erasure,
this cast is not even done in MyClass.getArray(), but in the code that
calls MyClass.getArray().

So getArray() does not even know about T, it just returns what its
mapping of the PostgreSQL types to java tells it to.

http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html should be
a good reading, and in case of any doubts,
http://java.sun.com/docs/books/jls/index.html

> I call i like this:
>
> String[] values = MyClass.<String>getArray(rs, "myStringArrayColumn"));
>
>  This works fine. ( "myStringArrayColumn" is of type character
> varying(64)[])

Yes, it works fine. ResultSet.getArray maps the varchar[] to a String[],
and then your 'String[] values=' assignment casts that to String[],
which works fine.

> But, when I call:
>
> Integer[] values = MyClass.<Integer>getArray(rs, "myIntegerArrayColumn"));
> ( "myIntegerArrayColumn" is of type integer[])
>
> I get a:
> java.lang.ClassCastException: [I

This one fails. ResultSet.getArray maps the integer[] to a int[], not to
an Integer[]. And so, the cast will fail.


HTH,
Markus


--
Markus Schaber | Logical Tracking&Tracing International AG
Dipl. Inf.     | Software Development GIS

Fight against software patents in Europe! www.ffii.org
www.nosoftwarepatents.org

В списке pgsql-jdbc по дате отправления:

Предыдущее
От: Bendik Rognlien Johansen
Дата:
Сообщение: Re: Retrieving arrays
Следующее
От: Bendik Rognlien Johansen
Дата:
Сообщение: Re: Retrieving arrays