Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 1043E9FB471 for ; Thu, 19 Jul 2007 07:04:40 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 28561-04 for ; Thu, 19 Jul 2007 07:04:30 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from mail.asci-systemhaus.de (port-83-236-232-74.static.qsc.de [83.236.232.74]) by postgresql.org (Postfix) with ESMTP id B54949FB3C0 for ; Thu, 19 Jul 2007 07:04:35 -0300 (ADT) Received: from mail.asci-systemhaus.de (localhost [127.0.0.1]) by mail.asci-systemhaus.de (Postfix) with ESMTP id 68FB527E61 for ; Thu, 19 Jul 2007 12:04:30 +0200 (CEST) Received: from [62.180.165.92] (c-180-165-92.b.dial.de.ignite.net [62.180.165.92]) by mail.asci-systemhaus.de (Postfix) with ESMTP id ECCB727E26 for ; Thu, 19 Jul 2007 12:04:28 +0200 (CEST) Message-ID: <469F372F.2050602@asci-systemhaus.de> Date: Thu, 19 Jul 2007 12:04:31 +0200 From: =?ISO-8859-15?Q?Ingmar_L=F6tzsch?= User-Agent: Thunderbird 1.5.0.12 (Windows/20070509) MIME-Version: 1.0 To: pgsql-jdbc@postgresql.org Subject: Re: IN clause with PreparedStatement References: <469E34E6.3010002@asci-systemhaus.de> In-Reply-To: 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.101 tagged_above=0 required=5 tests=BAYES_50=0.001, RDNS_DYNAMIC=0.1 X-Spam-Level: X-Archive-Number: 200707/89 X-Sequence-Number: 17263 >> SELECT * >> FROM table1 >> WHERE id IN (1, 2, 3); > This identical thread concluded with a perl example that is relevant. > > http://archives.postgresql.org/pgsql-jdbc/2007-06/msg00015.php Thank you very much. They suggest two approaches 1. int[] num = new int[]{1,2,3}; StringBuffer sb = new StringBuffer(); sb.append("SELECT * FROM items WHERE id IN(").append(generateCSV("?", num.length)).append(")"); for (int i = 0; i < num.length; i++) { ps.setInt(i, num[i]); } ps.executeQuery(); 2. $sth = $dbh->prepare("select * from foo where bar =ANY(?::int[])") $sth->execute('{' . join(@array, ',') . '}'); What do you think about combining the ANY key word with the PreparedStatement the following way? class IntArray package test; import java.sql.Array; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Types; import java.util.Map; public class IntArray implements Array { private int[] array; public IntArray(int[] array) { if (array == null) { throw new IllegalArgumentException("parameter array should not be null"); } this.array = array; } public Object getArray() throws SQLException { return null; } public Object getArray(Map map) throws SQLException { return null; } public Object getArray(long index, int count) throws SQLException { return null; } public Object getArray(long index, int count, Map map) throws SQLException { return null; } public int getBaseType() throws SQLException { return Types.INTEGER; } /** * This method is called by driver ver. 8 but not by ver. 7. */ public String getBaseTypeName() throws SQLException { return "int4"; } public ResultSet getResultSet() throws SQLException { return null; } public ResultSet getResultSet(long index, int count) throws SQLException { return null; } public ResultSet getResultSet(long index, int count, Map map) throws SQLException { return null; } public ResultSet getResultSet(Map map) throws SQLException { return null; } /** * This method is called by both drivers ver. 8 and 7. */ public String toString() { String result = "{"; for (int i = 0; i < this.array.length; ++i) { if (i > 0) { result += ","; } result += this.array[i]; } result += "}"; return result; } } class ArrayTest package test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class ArrayTest { public static void main(String[] args) { int[] array = new int[]{1, 2, 3}; IntArray intArray = new IntArray(array); try { Class.forName("org.postgresql.Driver"); String url = "jdbc:postgresql://localhost/test"; Connection con = DriverManager.getConnection(url, "postgres", "admin"); String sql = "SELECT *\n" + "FROM table1\n" + "WHERE id = ANY (?::int[]);"; PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setArray(1, intArray); ResultSet rs = pstmt.executeQuery(); while (rs.next()) { int id = rs.getInt("id"); String str1 = rs.getString("str1"); System.out.println(id + ", " + str1); } rs.close(); pstmt.close(); con.close(); } catch (Exception e) { e.printStackTrace(System.out); } } } This works with postgresql-8.1-404.jdbc3.jar and pg74.216.jdbc3.jar. I don't know how to implement the getArray() and getResultSet() methods. Both drivers only call the toString() method and the driver version 8 additionaly calls getBaseTypeName(). Thank you