Re: JDBC question: Which class is returned?

From: Peter T Mount <peter(at)retep(dot)org(dot)uk>
To: "Dr(dot) Evil" <drevil(at)sidereal(dot)kz>, pgsql-general(at)postgresql(dot)org
Subject: Re: JDBC question: Which class is returned?
Date: 2001-10-15 13:12:56
Message-ID: 5.1.0.14.0.20011015141031.00b05008@mail.retep.org.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-jdbc

At 20:30 12/10/2001 +0000, Dr. Evil wrote:

>I am doing some queries using JDBC. I use the ResultSet.getObject()
>method to get the result object. This works fine with SQL VARCHAR,
>etc, but there is a big problem when I try it with an INT4.
>
>For example:
>
>Object obj = result.getObject(i);
>
>It gets the object just fine but then I can't do anything with the
>object. I can't do this:
>
>System.out.println("The result is: " + (String) obj);

No, this won't work as not all Objects returned are Strings.

The correct thing to do here is:

System.out.println("The result is: " + obj.toString() );

Which will work as all objects have the toString() method.

>or anything else. I am guessing that the problem may be that it is
>trying to return an integer type, which is not an object. Any
>sugestions on this?

Not possible. The int/long/double entities are not objects, so in this case
can never be returned. The driver should be wrapping them in
Integer/Long/Double objects instead.

>One thing I think I could do is to try to detect the type using
>MetaData.getResultType() or something, and then call
>ResultSet.getInt() or whatever is appropriate. Is this the best way
>to do it?

That is an alternative, but the getObject() call should work.

Peter

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Gordan Bobic 2001-10-15 13:22:53 Re: Any Good Way To Do Sync DB's?
Previous Message Joseph Koenig 2001-10-15 12:35:33 Re: Any Good Way To Do Sync DB's?

Browse pgsql-jdbc by date

  From Date Subject
Next Message Mihai Gheorghiu 2001-10-15 13:56:28 Fw: [GENERAL] Error messages
Previous Message Rene Pijlman 2001-10-14 16:53:45 Re: how can I get all the tables' name by one function call?