pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
To: Daron Ryan <daron.ryan@gmail.com>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: PreparedStatement for set membership (The IN operator)
Date: Tue, 05 Apr 2011 15:56:36 +0300
Message-ID: <4D9B1184.9090404@enterprisedb.com> (raw)
In-Reply-To: <4D9B0D6C.70707@gmail.com>
References: <4D9AC223.4000602@gmail.com>
	<4D9AD284.1090306@enterprisedb.com>
	<4D9B0D6C.70707@gmail.com>

On 05.04.2011 15:39, Daron Ryan wrote:
> Thanks Heikki. I have tried using the setArray method but I am still
> running into an error.
>
> Exception in thread "main" org.postgresql.util.PSQLException: Unknown
> type _INTEGER.
> at
> org.postgresql.jdbc2.AbstractJdbc2Statement.setArray(AbstractJdbc2Statement.java:2800)
>
> at dictionary.test.Main.main(Main.java:85)
>
> This is the Array implementation I have created.
> http://pastebin.com/tkzPRL4A

Starting with JDBC4, you can use conn.createArrayOf() function. No need 
to create a custom Array class anymore. This is what we have in the test 
suite:

   public void testCreateArrayOfInt() throws SQLException {
         PreparedStatement pstmt = _conn.prepareStatement("SELECT 
?::int[]");
         Integer in[] = new Integer[3];
         in[0] = 0;
         in[1] = -1;
         in[2] = 2;
         pstmt.setArray(1, _conn.createArrayOf("int4", in));

         ResultSet rs = pstmt.executeQuery();
         assertTrue(rs.next());
         Array arr = rs.getArray(1);
         Integer out[] = (Integer [])arr.getArray();

         assertEquals(3, out.length);
         assertEquals(0, out[0].intValue());
         assertEquals(-1, out[1].intValue());
         assertEquals(2, out[2].intValue());
     }

-- 
   Heikki Linnakangas
   EnterpriseDB   http://www.enterprisedb.com



view thread (6+ messages)  latest in thread

Message-ID: <4D9B1184.9090404@enterprisedb.com>
Permalink:  ../4D9B1184.9090404@enterprisedb.com/
Also on:    postgresql.org/message-id/4D9B1184.9090404@enterprisedb.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: heikki.linnakangas@enterprisedb.com, daron.ryan@gmail.com
  Subject: Re: PreparedStatement for set membership (The IN operator)
  In-Reply-To: <4D9B1184.9090404@enterprisedb.com>
  References: <4D9AC223.4000602@gmail.com> <4D9AD284.1090306@enterprisedb.com> <4D9B0D6C.70707@gmail.com> <4D9B1184.9090404@enterprisedb.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