JDBC Array double precision [] error

Started by Juan Pablo Cookabout 13 years ago6 messagesgeneral
Jump to latest
#1Juan Pablo Cook
juampick@gmail.com

Hi everyone! I need your help with this problem.

I'm using PostgreSQL 9.2 Server & the latest jdbc
driver: postgresql-9.2-1002.jdbc4.jar

I have a table with this column array:
-- histograma double precision[]

And I want to retrieve this and cast into java to double[] but I can't.

This is the extract of the code:

Statement stat1 = con.createStatement();
ResultSet rs1 = stat1.executeQuery("SELECT * FROM \"Vector\");

while (rs1.next()) {

double[] array = (double[]) rs1.getArray("histograma").getArray();

}

And the error:
"
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:
[Ljava.lang.Double; cannot be cast to [D
"
Also I tried to do a for loop but didn't work.

Can you help me?

Thanks a lot! ;)

#2Juan Pablo Cook
juampick@gmail.com
In reply to: Juan Pablo Cook (#1)
Fwd: JDBC Array double precision [] error

Hi everyone! I need your help with this problem.

I'm using PostgreSQL 9.2 Server & the latest jdbc
driver: postgresql-9.2-1002.jdbc4.jar

I have a table with this column array:
-- histograma double precision[]

And I want to retrieve this and cast into java to double[] but I can't.

This is the extract of the code:

Statement stat1 = con.createStatement();
ResultSet rs1 = stat1.executeQuery("SELECT * FROM \"Vector\");

while (rs1.next()) {

double[] array = (double[]) rs1.getArray("histograma").getArray();

}

And the error:
"
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:
[Ljava.lang.Double; cannot be cast to [D
"
Also I tried to do a for loop but didn't work.

Can you help me?

Thanks a lot! ;)

#3Hannes Erven
hannes@erven.at
In reply to: Juan Pablo Cook (#2)
Re: Fwd: JDBC Array double precision [] error

Hi Juan Pablo,

double[] array = (double[]) rs1.getArray("histograma").getArray();
java.lang.ClassCastException: [Ljava.lang.Double; cannot be cast to [D

The error message already tells you the solution: use Double, not double.

Like this:
Double[] array = (Double[]) rs1.getArray("histograma").getArray();

Best regards,

-hannes

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#4Juan Pablo Cook
juampick@gmail.com
In reply to: Hannes Erven (#3)
Re: Fwd: JDBC Array double precision [] error

Hi Hannes...
Thanks a lot for your help!
I was stuck being many many hours trying and googling it the problem but
didn't realize that.

This: *Double[] array = (Double[]) rs1.getArray("histograma").get**Array();*
solve my problem!

Thanks again!!

Juan Cook

On Sun, Mar 31, 2013 at 7:38 PM, Hannes Erven <hannes@erven.at> wrote:

Show quoted text

Hi Juan Pablo,

double[] array = (double[]) rs1.getArray("histograma").**getArray();

java.lang.ClassCastException: [Ljava.lang.Double; cannot be cast to [D

The error message already tells you the solution: use Double, not double.

Like this:
Double[] array = (Double[]) rs1.getArray("histograma").**getArray();

Best regards,

-hannes

#5Nicholas White
n.j.white@gmail.com
In reply to: Juan Pablo Cook (#1)
Re: JDBC Array double precision [] error

getArray returns a Double[] - not a primitive double[]. You can't cast
between the two; use something like
http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/primitives/Doubles.html#toArray(java.util.Collection)
...

On Sunday, 31 March 2013, Juan Pablo Cook wrote:

Show quoted text

Hi everyone! I need your help with this problem.

I'm using PostgreSQL 9.2 Server & the latest jdbc
driver: postgresql-9.2-1002.jdbc4.jar

I have a table with this column array:
-- histograma double precision[]

And I want to retrieve this and cast into java to double[] but I can't.

This is the extract of the code:

Statement stat1 = con.createStatement();
ResultSet rs1 = stat1.executeQuery("SELECT * FROM \"Vector\");

while (rs1.next()) {

double[] array = (double[]) rs1.getArray("histograma").getArray();

}

And the error:
"
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:
[Ljava.lang.Double; cannot be cast to [D
"
Also I tried to do a for loop but didn't work.

Can you help me?

Thanks a lot! ;)

#6Juan Pablo Cook
juampick@gmail.com
In reply to: Nicholas White (#5)
Re: JDBC Array double precision [] error

Thanks everyone! that's help a lot ;) Double[] instead of the primitive
double[].

Thanks!

JP

On Mon, Apr 1, 2013 at 3:28 AM, Nicholas White <n.j.white@gmail.com> wrote:

Show quoted text

getArray returns a Double[] - not a primitive double[]. You can't cast
between the two; use something like
http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/primitives/Doubles.html#toArray(java.util.Collection)
...

On Sunday, 31 March 2013, Juan Pablo Cook wrote:

Hi everyone! I need your help with this problem.

I'm using PostgreSQL 9.2 Server & the latest jdbc
driver: postgresql-9.2-1002.jdbc4.jar

I have a table with this column array:
-- histograma double precision[]

And I want to retrieve this and cast into java to double[] but I can't.

This is the extract of the code:

Statement stat1 = con.createStatement();
ResultSet rs1 = stat1.executeQuery("SELECT * FROM \"Vector\");

while (rs1.next()) {

double[] array = (double[]) rs1.getArray("histograma").getArray();

}

And the error:
"
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:
[Ljava.lang.Double; cannot be cast to [D
"
Also I tried to do a for loop but didn't work.

Can you help me?

Thanks a lot! ;)