Received: from localhost (maia-5.hub.org [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id 5A55C9FB90F for ; Tue, 1 May 2007 11:10:57 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-maia, port 10024) with ESMTP id 42446-01 for ; Tue, 1 May 2007 11:10:42 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from mir3-fs.mir3.com (mail.mir3.com [65.208.188.100]) by postgresql.org (Postfix) with ESMTP id 0F3BE9FB463 for ; Tue, 1 May 2007 11:10:45 -0300 (ADT) Received: MIR3-FS.mir3.com 172.16.1.11 from 172.16.2.68 172.16.2.68 via HTTP with MS-WebStorage 6.0.6249 Received: from archimedes by MIR3-FS.mir3.com; 01 May 2007 07:10:16 -0700 Subject: Re: Null bind variable in where clause From: Mark Lewis To: Chris Stuhr Cc: pgsql-jdbc@postgresql.org In-Reply-To: <46369940.1030801@ephibian.com> References: <46369940.1030801@ephibian.com> Content-Type: text/plain Content-Transfer-Encoding: 7bit Organization: MIR3, Inc. Date: Tue, 01 May 2007 07:10:16 -0700 Message-Id: <1178028616.20922.259.camel@archimedes> Mime-Version: 1.0 X-Mailer: Evolution 2.8.1 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200705/3 X-Sequence-Number: 16993 Others have already given good suggestions, but just as a matter of form, you shouldn't use setObject() to set a null value. You should always use setNull() instead. I don't think that the distinction matters for PG, so if you're only ever going to support PG it won't matter, but it does on some databases. Sybase is the only one that I know definitely suffered from this, but there may be others as well. -- Mark On Mon, 2007-04-30 at 18:34 -0700, Chris Stuhr wrote: > It looks like you can not use a bind variable when trying to match null > in a where clause. Is there any way to get the desired effect without > having to remove the bind variable and rewrite the query to "WHERE b IS > null"? > > import java.sql.*; > > public class a { > > public static void main(String args[]) throws Exception { > String sql = "CREATE TABLE t(a int, b int)"; > Class.forName("org.postgresql.Driver"); > Connection conn = DriverManager.getConnection( > "jdbc:postgresql://10.20.1.70:5432/firm", "gaiam", ""); > try { > Statement stmt = conn.createStatement(); > stmt.executeUpdate(sql); > sql = "INSERT INTO t(a,b) VALUES (3,null)"; > stmt.executeUpdate(sql); > stmt.close(); > } catch (SQLException sqle) { > sqle.printStackTrace(); > } > > sql = "UPDATE t SET a = ? WHERE b = ?"; > PreparedStatement pstmt = conn.prepareStatement(sql); > pstmt.setInt(1, 4); > pstmt.setObject(2, null); > int x = pstmt.executeUpdate(); > System.out.println(x); > pstmt.close(); > Statement stmt = conn.createStatement(); > stmt.executeUpdate("DROP TABLE t"); > stmt.close(); > conn.close(); > } > } > > Will print 0 instead of 1 > > -Chris > > > ---------------------------(end of broadcast)--------------------------- > TIP 3: Have you checked our extensive FAQ? > > http://www.postgresql.org/docs/faq