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: (view raw, whole thread or download thread mbox)
Lists: pgsql-jdbc

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(
>         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>         at sun.reflect.NativeMethodAccessorImpl.invoke(
>         at sun.reflect.DelegatingMethodAccessorImpl.invoke(
>         at java.lang.reflect.Method.invoke(
> 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


Description: text/plain (2.5 KB)


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-2017 The PostgreSQL Global Development Group