Skip site navigation (1) Skip section navigation (2)

Re: Retrieving arrays

From: Bendik Rognlien Johansen <bensmailinglists(at)gmail(dot)com>
To: Markus Schaber <schabi(at)logix-tt(dot)com>
Cc: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: Retrieving arrays
Date: 2006-10-11 13:39:41
Message-ID: 02302CAD-88BF-445E-A642-40EE4266045B@gmail.com (view raw or flat)
Thread:
Lists: 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


In response to

Responses

pgsql-jdbc by date

Next:From: Markus SchaberDate: 2006-10-11 13:43:09
Subject: Re: Retrieving arrays
Previous:From: Markus SchaberDate: 2006-10-11 12:42:24
Subject: Re: Retrieving arrays

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group