pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bendik Rognlien Johansen <bensmailinglists@gmail.com>
To: Markus Schaber <schabi@logix-tt.com>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: Retrieving arrays
Date: Wed, 11 Oct 2006 15:39:41 +0200
Message-ID: <02302CAD-88BF-445E-A642-40EE4266045B@gmail.com> (raw)
In-Reply-To: <452CE6B0.10409@logix-tt.com>
References: <533747B3-BC3C-4D07-A2B8-A3C86B85C718@gmail.com>
	<452CE6B0.10409@logix-tt.com>

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




view thread (11+ messages)  latest in thread

Message-ID: <02302CAD-88BF-445E-A642-40EE4266045B@gmail.com>
Permalink:  ../02302CAD-88BF-445E-A642-40EE4266045B@gmail.com/
Also on:    postgresql.org/message-id/02302CAD-88BF-445E-A642-40EE4266045B@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: bensmailinglists@gmail.com, schabi@logix-tt.com
  Subject: Re: Retrieving arrays
  In-Reply-To: <02302CAD-88BF-445E-A642-40EE4266045B@gmail.com>
  References: <533747B3-BC3C-4D07-A2B8-A3C86B85C718@gmail.com> <452CE6B0.10409@logix-tt.com> <02302CAD-88BF-445E-A642-40EE4266045B@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox