Re: Retrieving arrays - Mailing list pgsql-jdbc
From | Bendik Rognlien Johansen |
---|---|
Subject | Re: Retrieving arrays |
Date | |
Msg-id | 02302CAD-88BF-445E-A642-40EE4266045B@gmail.com Whole thread Raw |
In response to | Re: Retrieving arrays (Markus Schaber <schabi@logix-tt.com>) |
Responses |
Re: Retrieving arrays
|
List | pgsql-jdbc |
Hello, yes you are right, I don't really understand Generics that well. But it is a little clearer now :-) Anyway, I was hoping there would be a clean way to get arrays from the result set, Generics or not, using a single method. I would like to avoid: getStringArray, getIntegerArray etc. The reason I need to do this is that I use two different drivers for PostgreSQL with the same code, and they handle arrays differently. Thanks! On Oct 11, 2006, at 2:42 PM, Markus Schaber wrote: > 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 by date: