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

Re: BUG #6292: java.sql.PreparedStatement.setNull() throws PSQLException

From: Kris Jurka <books(at)ejurka(dot)com>
To: David Pinheiro <davidsantospinheiro(at)gmail(dot)com>
Cc: pgsql-bugs(at)postgresql(dot)org, pgsql-jdbc(at)postgresql(dot)org
Subject: Re: BUG #6292: java.sql.PreparedStatement.setNull() throws PSQLException
Date: 2011-11-16 21:38:46
Message-ID: alpine.BSO.2.00.1111161635310.14447@leary.csoft.net (view raw or flat)
Thread:
Lists: pgsql-bugspgsql-jdbc

On Mon, 14 Nov 2011, David Pinheiro wrote:

> 
> Bug reference:      6292
> PostgreSQL version: 8.4
> Description:        java.sql.PreparedStatement.setNull() throws
> PSQLException
> Details: 
> 
> I'm trying to make:
> java.sql.PreparedStatement.setNull(1,java.sql.Types.NULL);
> 
> org.postgresql.util.PSQLException: ERROR: column "number_column" is of type
> integer but expression is of type character varying
> Hint: You will need to rewrite or cast the expression.
> 
> 
> My code is something like:
> if (String.valueOf(input_field) == "null"){
>      statement.setNull(1, java.sql.Types.NULL);
> } else {
>      statement.setObject(1, valor);
> }
> statement.executeUpdate();
> 

I don't think your problem is with setNull, I think your problem is 
actually with the setObject branch of your if statement.  If the 
valor variable is a String, the JDBC driver is assigning it a string 
type.  You may want to do setObject(1, valor, Types.INTEGER) or 
convert it prior to doing the setObject call to an appropriate 
numeric type.

Kris Jurka

In response to

pgsql-bugs by date

Next:From: Robert HaasDate: 2011-11-17 04:43:14
Subject: Re: BUG #6296: High level backend-write activity on db
Previous:From: Kris JurkaDate: 2011-11-16 21:29:36
Subject: Re: BUG #6293: JDBC driver performance

pgsql-jdbc by date

Next:From: Anish KejariwalDate: 2011-11-16 21:42:40
Subject: Re: avoid prepared statements on complex queries?
Previous:From: Bruno HarbulotDate: 2011-11-16 21:31:11
Subject: Re: SSL patch

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