pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #6292: java.sql.PreparedStatement.setNull() throws PSQLException
2+ messages / 2 participants
[nested] [flat]

* BUG #6292: java.sql.PreparedStatement.setNull() throws PSQLException
@ 2011-11-14 16:31  David Pinheiro <davidsantospinheiro@gmail.com>
  0 siblings, 1 reply; 2+ messages in thread

From: David Pinheiro @ 2011-11-14 16:31 UTC (permalink / raw)
  To: pgsql-bugs@postgresql.org


The following bug has been logged online:

Bug reference:      6292
Logged by:          David Pinheiro
Email address:      davidsantospinheiro@gmail.com
PostgreSQL version: 8.4
Operating system:   Linux
Description:        java.sql.PreparedStatement.setNull() throws
PSQLException
Details: 

I'm trying to make:
java.sql.PreparedStatement.setNull(1,java.sql.Types.NULL);


But i get:
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();


Java says:
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.
  Position: 159
	at
org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorI
mpl.java:2096)
	at
org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.ja
va:1829)
	at
org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)

	at
org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.j
ava:510)
	at
org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2St
atement.java:386)
	at
org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2State
ment.java:332)
	at
pt.cm_olhao.tecidoempresarial.database.estabelecimentos.EstabelecimentosFree
formStatementDelegate.storeRow(EstabelecimentosFreeformStatementDelegate.jav
a:160)



I have Postgresql 8.4 and postgresql-jdbc-8.4-703.

Thanks a lot!
David Pinheiro



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: BUG #6292: java.sql.PreparedStatement.setNull() throws PSQLException
@ 2011-11-16 21:38  Kris Jurka <books@ejurka.com>
  parent: David Pinheiro <davidsantospinheiro@gmail.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Kris Jurka @ 2011-11-16 21:38 UTC (permalink / raw)
  To: David Pinheiro <davidsantospinheiro@gmail.com>; +Cc: pgsql-bugs@postgresql.org; pgsql-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



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2011-11-16 21:38 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2011-11-14 16:31 BUG #6292: java.sql.PreparedStatement.setNull() throws PSQLException David Pinheiro <davidsantospinheiro@gmail.com>
2011-11-16 21:38 ` Kris Jurka <books@ejurka.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox