Re: PreparedStatement.setString with null

Lists: pgsql-jdbc
From: Tim Penhey <tim(at)penhey(dot)net>
To: "pgsql-jdbc(at)postgresql(dot)org" <pgsql-jdbc(at)postgresql(dot)org>
Subject: PreparedStatement.setString with null
Date: 2004-09-30 09:55:19
Message-ID: 415BD807.6040103@penhey.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-jdbc

What is this supposed to do?

PreparedStatement stat = connection.prepareStatement("update table1
set col1 = ?");
stat.setString(1, null);

Will this make a null value in the database or an empty string? Do we
have to use the setNull(1, Types.VARCHAR) in order to get a null value
entered?

Thanks,
Tim


From: Roland Walter <rwa(at)mosaic-ag(dot)com>
To: Tim Penhey <tim(at)penhey(dot)net>
Cc: "pgsql-jdbc(at)postgresql(dot)org" <pgsql-jdbc(at)postgresql(dot)org>
Subject: Re: PreparedStatement.setString with null
Date: 2004-09-30 10:13:09
Message-ID: 415BDC35.1020801@mosaic-ag.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-jdbc

Tim Penhey schrieb:

> What is this supposed to do?
>
> PreparedStatement stat = connection.prepareStatement("update table1
> set col1 = ?");
> stat.setString(1, null);
>
> Will this make a null value in the database or an empty string? Do we
> have to use the setNull(1, Types.VARCHAR) in order to get a null value
> entered?

If you want to be portable to different databases, use the setNull() method.

--
Roland Walter
MOSAIC SOFTWARE AG
Telefon: 02225/882-411 Fax: 02225/882-201
http://www.mosaic-ag.com


From: Oliver Jowett <oliver(at)opencloud(dot)com>
To: Tim Penhey <tim(at)penhey(dot)net>
Cc: "pgsql-jdbc(at)postgresql(dot)org" <pgsql-jdbc(at)postgresql(dot)org>
Subject: Re: PreparedStatement.setString with null
Date: 2004-09-30 10:34:25
Message-ID: 415BE131.8080203@opencloud.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-jdbc

Tim Penhey wrote:
> What is this supposed to do?
>
> PreparedStatement stat = connection.prepareStatement("update table1
> set col1 = ?");
> stat.setString(1, null);
>
> Will this make a null value in the database or an empty string? Do we
> have to use the setNull(1, Types.VARCHAR) in order to get a null value
> entered?

The postgresql JDBC driver treats setString(x, null) identically to
setNull(x, Types.VARCHAR). I don't know how portable this behaviour is;
the javadoc is silent on how this case should be handled. You're
probably better off using setNull() explicitly.

-O