Received: from localhost (wm.hub.org [200.46.204.128]) by postgresql.org (Postfix) with ESMTP id ED5B09FB392 for ; Wed, 11 Oct 2006 09:42:32 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.128]) (amavisd-new, port 10024) with ESMTP id 02484-07 for ; Wed, 11 Oct 2006 12:42:28 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey- Received: from mail.logix-tt.com (mail.logix-tt.com [212.211.145.186]) by postgresql.org (Postfix) with ESMTP id 0964D9FB391 for ; Wed, 11 Oct 2006 09:42:27 -0300 (ADT) Received: from kingfisher.intern.logix-tt.com (p549D1B3A.dip0.t-ipconnect.de [84.157.27.58]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.logix-tt.com (Postfix) with ESMTP id 671886599A; Wed, 11 Oct 2006 14:44:55 +0200 (CEST) Received: from [127.0.0.1] (localhost [127.0.0.1]) by kingfisher.intern.logix-tt.com (Postfix) with ESMTP id D362218151EB9; Wed, 11 Oct 2006 14:42:24 +0200 (CEST) Message-ID: <452CE6B0.10409@logix-tt.com> Date: Wed, 11 Oct 2006 14:42:24 +0200 From: Markus Schaber Organization: Logical Tracking and Tracing International AG, Switzerland User-Agent: Thunderbird 1.5.0.5 (X11/20060812) MIME-Version: 1.0 To: Bendik Rognlien Johansen Cc: pgsql-jdbc@postgresql.org Subject: Re: Retrieving arrays References: <533747B3-BC3C-4D07-A2B8-A3C86B85C718@gmail.com> In-Reply-To: <533747B3-BC3C-4D07-A2B8-A3C86B85C718@gmail.com> X-Enigmail-Version: 0.94.0.0 Content-Type: text/plain; charset=ISO-8859-15 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.135 tagged_above=0 required=5 tests=FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200610/66 X-Sequence-Number: 15949 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