Skip site navigation (1) Skip section navigation (2)

Null bind variable in where clause

From: Chris Stuhr <cstuhr(at)ephibian(dot)com>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Null bind variable in where clause
Date: 2007-05-01 01:34:56
Message-ID: 46369940.1030801@ephibian.com (view raw or flat)
Thread:
Lists: pgsql-jdbc
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


Responses

pgsql-jdbc by date

Next:From: Dave CramerDate: 2007-05-01 11:21:39
Subject: Re: Null bind variable in where clause
Previous:From: Kris JurkaDate: 2007-04-30 22:34:27
Subject: Re: Bug in org.postgresql.jdbc2.AbstractJdbc2ResultSet?

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group