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

Using BigInteger as argument to AbstractJdbc2Statement.setObject

From: Sylvain Leroux <sl20(at)wanadoo(dot)fr>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Using BigInteger as argument to AbstractJdbc2Statement.setObject
Date: 2009-08-05 10:01:35
Message-ID: 4A79587F.6010603@wanadoo.fr (view raw or flat)
Thread:
Lists: pgsql-jdbc
Hi,

I am using Jython to access a PostgreSQL database throught the JDBC driver.

I have a problem with a code fragment like that:
>         stmt = self.conn.prepareStatement("INSERT INTO campagne(id) VALUES(?)")
>         stmt.setObject(1, 123456899999)

Of course, in the real code, the value 123456899999 is not hard wired.
Anyway, Jython ``autobox'' such values into a java.math.BigInteger object.
But AbstractJdbc2Statement.setObject does not handle that kind of 
object. Which leads to the error:
>     stmt.setObject(1, 123456899999)
>         at org.postgresql.jdbc2.AbstractJdbc2Statement.setObject(AbstractJdbc2Statement.java:1735)
>         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
>         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
>         at java.lang.reflect.Method.invoke(Method.java:597)
> 
> org.postgresql.util.PSQLException: org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of 
> java.math.BigInteger. Use setObject() with an explicit Types value to specify the type to use.

Since the column campagne(id) is a NUMERIC, as the error message 
suggests, I could write something like that instead:
>         stmt.setObject(1, 123456899999, Types.NUMERIC)

But this defeats the purpose of using a scripting language both for 
simplicity and dynamic typing.

Moreover other scripting languages for the Java platform may exhibit the 
same behavior. So, it would be great if we have support for passing 
BigInteger as argument to the setObject method in prepared statements. 
Even if it's not required by the JDBC API.

Best of all, the patch is trivial as you will see: I send the result of 
a ``cvs diff -u8pN'' as an attachment.


Best regards,
Sylvain Leroux

-- 
Website: http://www.chicoree.fr

Attachment: AbstractJdbc2Statement.java.diff
Description: text/plain (2.5 KB)

Responses

pgsql-jdbc by date

Next:From: Michael SchwippsDate: 2009-08-05 10:35:47
Subject: float4 or real in function parameter -> PSQLException: ERRORfunction in_test4(double precision) does not exist
Previous:From: Thomas KellererDate: 2009-08-03 18:11:25
Subject: Re: Datatype of domains not reported correctly.

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