Re: JDBC question: Which class is returned?

From: o2(at)trustcommerce(dot)com
To: "Dr(dot) Evil" <drevil(at)sidereal(dot)kz>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: JDBC question: Which class is returned?
Date: 2001-10-13 01:23:28
Message-ID: 20011012182328.A848@trustcommerce.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-jdbc

On Fri, Oct 12, 2001 at 08:30:02PM -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);
>
> 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?
>
> 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?
>
> Thanks

The reason you can't cast it to a string is because it will be class Integer if the col type is int.

Integer val = (Integer)result.getObject(i);

This is how to do what you want to do.

Object obj = result.getObject(i);
System.out.println("The result is: " + obj.toString());

If the .getObject() is messing you up there are a wide number of other functions to use like getString() that will convert the return type for you.

Orion

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Barry Lind 2001-10-13 01:30:02 Re: [GENERAL] Error messages
Previous Message Barry Lind 2001-10-13 01:20:35 Re: [GENERAL] Error messages

Browse pgsql-jdbc by date

  From Date Subject
Next Message Barry Lind 2001-10-13 01:30:02 Re: [GENERAL] Error messages
Previous Message Barry Lind 2001-10-13 01:20:35 Re: [GENERAL] Error messages