X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id DA88F9DC9C0 for ; Thu, 9 Mar 2006 12:32:14 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 97527-07 for ; Thu, 9 Mar 2006 12:32:12 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from nproxy.gmail.com (nproxy.gmail.com [64.233.182.203]) by postgresql.org (Postfix) with ESMTP id 6235A9DC845 for ; Thu, 9 Mar 2006 12:32:11 -0400 (AST) Received: by nproxy.gmail.com with SMTP id x29so386639nfb for ; Thu, 09 Mar 2006 08:32:10 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:mime-version:content-type:message-id:cc:content-transfer-encoding:from:subject:date:to:x-mailer; b=L500shwxSK98G72y+uHtgGpEN6vnwv0nT/VMmXwQcOqE2HVlw5OyhxS2X+hPW05+BCos6Oj82Y3uq3T3FJFJLX7ZFzYsOdC0F7/3VPjGfjK8oe77IxwikcMNUcGuPDQRF3qDbybV6wArVPz7aWb5aoOI8h3/sg0OGDRJYRuTLQY= Received: by 10.49.93.14 with SMTP id v14mr971113nfl; Thu, 09 Mar 2006 08:32:10 -0800 (PST) Received: from ?10.80.1.47? ( [194.248.208.94]) by mx.gmail.com with ESMTP id q27sm63845nfc.2006.03.09.08.32.07; Thu, 09 Mar 2006 08:32:08 -0800 (PST) Mime-Version: 1.0 (Apple Message framework v746.2) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: Cc: bendik.johansen@gmail.com Content-Transfer-Encoding: 7bit From: Bendik Rognlien Johansen Subject: updateRow with Array Date: Thu, 9 Mar 2006 17:32:05 +0100 To: pgsql-jdbc@postgresql.org X-Mailer: Apple Mail (2.746.2) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.12 required=5 tests=[AWL=0.120] X-Spam-Score: 0.12 X-Spam-Level: X-Archive-Number: 200603/48 X-Sequence-Number: 14829 Hello, I have a table with a column "categories" of type integer[] I wish to update it like this (simplified): ResultSet rs = st.executeQuery(query); while(rs.next()) { rs.updateArray("categories", ); rs.updateRow(); } I know this has been asked before, but none of the answers worked for me. I have tried many different things, but none seem to work. I used "?protocolVersion=2" and updateStatement.setString(1, "{1,2,3}"); and it worked with prepared statements but I can not get it to work with updateRow. I also tried creating an implementation of java.sql.Array as suggested by someone (see below), but i get: org.postgresql.util.PSQLException: Method org.postgresql.jdbc3.Jdbc3ResultSet.updateArray(String,Array) is not yet implemented. Any help would be great! I really need to solve this quickly. Thanks! The sql.Array impl. several things are hard coded for testing: import java.math.BigDecimal; import java.sql.*; import java.util.ArrayList; import java.util.Map; /* * Array is used collect one column of query result data. * *

Read a field of type Array into either a natively-typed * Java array object or a ResultSet. Accessor methods provide * the ability to capture array slices. * *

Other than the constructor all methods are direct implementations * of those specified for java.sql.Array. Please refer to the javadoc * for java.sql.Array for detailed descriptions of the functionality * and parameters of the methods of this class. * * This class stolen from postgresql 7.2.1's source tree!!! * * @see ResultSet#getArray * */ public class PostgresArray implements Array { private String rawString = null; private String typeName = null; private int baseType = -1; private static final String jdbc2Types[] = { "int2", "int4", "oid", "int8", "cash", "money", "numeric", "float4", "float8", "bpchar", "char", "char2", "char4", "char8", "char16", "varchar", "text", "name", "filename", "bytea", "bool", "date", "time", "abstime", "timestamp", "timestamptz", "_bool", "_char", "_int2", "_int4", "_text", "_integer", "_oid", "_varchar", "_int8", "_float4", "_float8", "_abstime", "_date", "_time", "_timestamp", "_numeric", "_bytea" }; /* * This table holds the JDBC type for each entry above. * * Note: This must be in the same order as above * * Tip: keep these grouped together by the Types. value */ private static final int jdbc2Typei[] = { Types.SMALLINT, Types.INTEGER, Types.INTEGER, Types.BIGINT, Types.DOUBLE, Types.DOUBLE, Types.NUMERIC, Types.REAL, Types.DOUBLE, Types.CHAR, Types.CHAR, Types.CHAR, Types.CHAR, Types.CHAR, Types.CHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.BINARY, Types.BIT, Types.DATE, Types.TIME, Types.TIMESTAMP, Types.TIMESTAMP, Types.TIMESTAMP, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY }; /* * Create a new Array * * @param conn a database connection * @param idx 1-based index of the query field to load into this Array * @param field the Field descriptor for the field to load into this Array * @param rs the ResultSet from which to get the data for this Array */ PostgresArray(String rawString, int baseType, String typeName) throws SQLException { System.out.println("[PostgresArray] - constructor"); this.rawString = rawString; this.baseType = baseType; this.typeName = typeName; if (this.typeName.startsWith("_")) { this.typeName = this.typeName.substring(1); } } /* public static Array create(Object[] array) throws SQLException { throw new SQLException("Not Implemented"); } */ public static Array create(int[] array) throws SQLException { System.out.println("[PostgresArray] - create(int[] array)"); if (array == null) return new PostgresArray(null, -1, null); StringBuffer sb = new StringBuffer("{"); for (int x = 0; x < array.length; x++) { if (x > 0) sb.append(","); sb.append("\"").append(array[x]).append("\""); } sb.append("}"); return new PostgresArray(sb.toString(), Types.INTEGER, "int"); } public Object getArray() throws SQLException { System.out.println("[PostgresArray] - getArray()"); return getArray(1, 0, null); } public Object getArray(long index, int count) throws SQLException { System.out.println("[PostgresArray] - getArray(long index, int count)"); return getArray(index, count, null); } public Object getArray(Map map) throws SQLException { System.out.println("[PostgresArray] - getArray(Map map)"); return getArray(1, 0, map); } public Object getArray(long index, int count, Map map) throws SQLException { System.out.println("[PostgresArray] - getArray(long index, int count, Map map)"); if (map != null) // For now maps aren't supported. throw new SQLException("Maps are not supported in Array.getArray"); if (index < 1) throw new SQLException("index < 1"); Object retVal = null; return retVal; } public int getBaseType() throws SQLException { System.out.println("[PostgresArray] - getBaseType() "); int sqlType = Types.OTHER; // default value for (int i = 0; i < jdbc2Types.length; i++) { if (this.typeName.equals(jdbc2Types[i])) { sqlType = jdbc2Typei[i]; break; } } return sqlType; } public String getBaseTypeName() throws SQLException { System.out.println("[PostgresArray] - getBaseTypeName() "); return this.typeName; } public java.sql.ResultSet getResultSet() throws SQLException { System.out.println("[PostgresArray] - getResultSet() "); return getResultSet(1, 0, null); } public java.sql.ResultSet getResultSet(long index, int count) throws SQLException { System.out.println("[PostgresArray] - getResultSet(long index, int count) "); return getResultSet(index, count, null); } public java.sql.ResultSet getResultSet(Map map) throws SQLException { System.out.println("[PostgresArray] - getResultSet(Map map) "); return getResultSet(1, 0, map); } public java.sql.ResultSet getResultSet(long index, int count, Map map) throws SQLException { System.out.println("[PostgresArray] - getResultSet(long index, int count, Map map)"); throw new SQLException("Array.getResultSet() not supported"); } public String toString() { System.out.println("[PostgresArray] - toString() "); return "{123}"; //return rawString; } }