From: | Markus Schaber <schabi(at)logix-tt(dot)com> |
---|---|
To: | Bendik Rognlien Johansen <bensmailinglists(at)gmail(dot)com> |
Cc: | pgsql-jdbc(at)postgresql(dot)org |
Subject: | Re: Retrieving arrays |
Date: | 2006-10-11 12:42:24 |
Message-ID: | 452CE6B0.10409@logix-tt.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | 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
From | Date | Subject | |
---|---|---|---|
Next Message | Bendik Rognlien Johansen | 2006-10-11 13:39:41 | Re: Retrieving arrays |
Previous Message | Bendik Rognlien Johansen | 2006-10-11 11:54:10 | Re: Retrieving arrays |