Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Td2w4-0006tQ-Fh for pgsql-jdbc@arkaria.postgresql.org; Mon, 26 Nov 2012 17:57:36 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1Td2w3-0000cU-Q8 for pgsql-jdbc@arkaria.postgresql.org; Mon, 26 Nov 2012 17:57:35 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Td2w2-0000cM-06 for pgsql-jdbc@postgresql.org; Mon, 26 Nov 2012 17:57:34 +0000 Received: from outbound.vnet-inc.com ([216.146.104.103]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Td2vx-0008Mk-9h for pgsql-jdbc@postgresql.org; Mon, 26 Nov 2012 17:57:32 +0000 Received: from [192.168.157.132] (unknown [76.75.27.41]) by outbound.vnet-inc.com (Postfix) with ESMTP id 8833C5AA5; Mon, 26 Nov 2012 10:57:27 -0700 (MST) Message-ID: <50B3AA5F.8000402@ttc-cmc.net> Date: Mon, 26 Nov 2012 10:43:59 -0700 From: dmp User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.13) Gecko/20100914 SeaMonkey/2.0.8 MIME-Version: 1.0 To: Ermengol Bota , PostgreSQL JDBC Subject: Re: Problems with BIT datatype and preparedStatment References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-jdbc Precedence: bulk Sender: pgsql-jdbc-owner@postgresql.org Hello, A search of the forum may turn up a better answer, but: Ermengol Bota wrote: > Hello, > > We are having problems when using BIT datatype. Mainly when using it on > prepared statement. > > The code below is a copy of the one send at: > pgsql-patches on 2003 > http://archives.postgresql.org/pgsql-patches/2003-06/msg00402.php > > sql.execute("Create Table jdbc_demo (b bit)"); > PreparedStatement pstmt = conn.prepareStatement("insert into jdbc_demo > values (?)"); > pstmt.setBoolean(1,true); // There is no way to set a value for the bit > field (or I don't know ... :-) > pstmt.execute(); PostreSQL actually has the data types: boolean and bit. From the information show below they are defined the same from a java.sql.boolean Datatype, but actually the bit type in PostgreSQL does not need be one bit. columnName, columnClass, columnType, columnSize boolean_type java.lang.Boolean bool 1 bit2_type java.lang.Boolean bit 2 So the first problem with your execution above is you have tried to insert a PostgreSQL boolean type into a defined PostgreSQL bit type. You definition is actually by default bit(1), it could have been bit(2) has the example bit2_type above. Can not use setBoolean() with bit type in the way you have tried. The one way that it can be done has I have seen before would be to do a CAST. pstmt = con.prepareStatement("INSERT INTO jdbc_demo VALUES (?::bit)"); pstmt.setString(1, "1"); > > Theorically (I think) it should works, but it answers: > > ERROR: column"b" is of type bit but expression is of type boolean > > We are working with PG 9.2.1 (one-click-installer on Ubuntu) and JDBC4 > 9.2-1002 > > After trying a lot of different types for the pstmt.setXXXX(), we think > that there is something not working well, or we are missing something :-) > Perhaps I can not say, with my limited knowledge. > BIT datatype works well with statement like it: > sqlStmt = "insert into table values ('1')"; > My limited understanding here is that a cast is taking place on the fly while with the PreparedStatement the engine is expecting the specifiy data type to be sent. > I know that we can do the same behavior with a boolean datatype instead > of BIT, and at the end we did this way, but I'm still curious about how > should it be done using bit > As indicated above because you are now using a boolean type. > Thank you for any help! danap. -- Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-jdbc