pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Possible to say: [setObject("123")] to insert into int column type ?
2+ messages / 2 participants
[nested] [flat]

* Possible to say: [setObject("123")] to insert into int column type ?
@ 2006-06-28 22:46 j.random.programmer <javadesigner@yahoo.com>
  2006-06-29 00:55 ` Re: Possible to say: [setObject("123")] to insert into int Oliver Jowett <oliver@opencloud.com>
  0 siblings, 1 reply; 2+ messages in thread

From: j.random.programmer @ 2006-06-28 22:46 UTC (permalink / raw)
  To: pgsql-jdbc

Hi:

I think setObject() has been discussed extensively on
this list 
(particularly with setting null values). However,
consider the following:

In PSQL:
	test=# create table foo (number_column int);

Now in Java:
	PreparedStatement ps = con.prepareStatement(
		"insert into foo (number_column) values (?)"
		);
	ps.setObject(1, "123");
	ps.executeUpdate();

This will throw a Driver exception. Is this expected
behavior 
or should the driver do a Integer.parseInt(...) on the
string value 
as needed ? 

This is not a show-stopper or even a serious bug in my
code
and is easy to workaround. I'm just curious as to the
"right"
behavior as per the JDBC spec.....

Best regards,
--j

----------------------- Exception ------------------

ERROR: column "number_column" is of type integer but 
expression is of type character varying
at
org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(
 QueryExecutorImpl.java:1527)
at
org.postgresql.core.v3.QueryExecutorImpl.processResults(
 QueryExecutorImpl.java:1311)
.....
---------------------------------------------------


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



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

* Re: Possible to say: [setObject("123")] to insert into int
  2006-06-28 22:46 Possible to say: [setObject("123")] to insert into int column type ? j.random.programmer <javadesigner@yahoo.com>
@ 2006-06-29 00:55 ` Oliver Jowett <oliver@opencloud.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Oliver Jowett @ 2006-06-29 00:55 UTC (permalink / raw)
  To: j.random.programmer <javadesigner@yahoo.com>; +Cc: pgsql-jdbc

j.random.programmer wrote:

> This will throw a Driver exception. Is this expected
> behavior 
> or should the driver do a Integer.parseInt(...) on the
> string value 
> as needed ? 
> 
> This is not a show-stopper or even a serious bug in my
> code
> and is easy to workaround. I'm just curious as to the
> "right"
> behavior as per the JDBC spec.....

I've found nothing in the JDBC spec to say that the driver is required 
to do an implicit conversion of String to integer (if you found 
something that says it is required, can you let me know where?).

If you explicitly specify a target SQL type (i.e. 
'setObject(1,"123",Types.INTEGER)'), the driver will do the conversion 
for you. But if you don't specify a type, the driver infers the SQL type 
from the Java type of the object, per the rules in the JDBC spec -- in 
this case, VARCHAR for a String object.

Or you could pass 'stringtype=unspecified' as a JDBC URL parameter which 
means that String parameters aren't typed at all when passed to the 
server (and the server tries to infer the type from the query).

-O



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


end of thread, other threads:[~2006-06-29 00:55 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-06-28 22:46 Possible to say: [setObject("123")] to insert into int column type ? j.random.programmer <javadesigner@yahoo.com>
2006-06-29 00:55 ` Re: Possible to say: [setObject("123")] to insert into int Oliver Jowett <oliver@opencloud.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