Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 2F3B59FB3A2 for ; Wed, 11 Oct 2006 10:39:55 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 24361-05 for ; Wed, 11 Oct 2006 10:39:51 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from ug-out-1314.google.com (ug-out-1314.google.com [66.249.92.171]) by postgresql.org (Postfix) with ESMTP id ECCC59FB3A0 for ; Wed, 11 Oct 2006 10:39:49 -0300 (ADT) Received: by ug-out-1314.google.com with SMTP id 30so96909ugc for ; Wed, 11 Oct 2006 06:39:46 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:in-reply-to:references:mime-version:content-type:message-id:cc:content-transfer-encoding:from:subject:date:to:x-mailer; b=mWxIKxNtzXArxRcDj4b305vAKdT/RvSxmUstJ7DzhFwnem1SJ50MDRLzNQ9MrD1l4tqUZlOVFpw6zo4mDNYK8behjLTaKnOz+dqzYD2pFj5LNQmq3qi64MqafpvwkIOChDXC2tQoEIm7lz2RqtXxW33dHQdqWpeJKUGmQTBJIPM= Received: by 10.66.252.4 with SMTP id z4mr1029715ugh; Wed, 11 Oct 2006 06:39:46 -0700 (PDT) Received: from ?10.80.1.43? ( [85.221.54.254]) by mx.google.com with ESMTP id k28sm797503ugd.2006.10.11.06.39.46; Wed, 11 Oct 2006 06:39:46 -0700 (PDT) In-Reply-To: <452CE6B0.10409@logix-tt.com> References: <533747B3-BC3C-4D07-A2B8-A3C86B85C718@gmail.com> <452CE6B0.10409@logix-tt.com> Mime-Version: 1.0 (Apple Message framework v752.3) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: <02302CAD-88BF-445E-A642-40EE4266045B@gmail.com> Cc: pgsql-jdbc@postgresql.org Content-Transfer-Encoding: 7bit From: Bendik Rognlien Johansen Subject: Re: Retrieving arrays Date: Wed, 11 Oct 2006 15:39:41 +0200 To: Markus Schaber X-Mailer: Apple Mail (2.752.3) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.191 tagged_above=0 required=5 tests=AWL, DNS_FROM_RFC_ABUSE, SPF_PASS X-Spam-Level: X-Archive-Number: 200610/67 X-Sequence-Number: 15950 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[] 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.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.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