Ree
Ree

Reputation: 6211

Binding parameter as PostgreSQL array

I'm trying to bind a prepared statement parameter which is a "multidimensional" PostgreSQL array. Here's an array example (column type is numeric[]):

{{1,10},{2,20}}

How do I bind a value like that using a prepared statement? I tried:

stmt.setObject(1, "{{1,10},{2,20}}", Types.ARRAY);

It didn't work:

Cannot cast an instance of java.lang.String to type Types.ARRAY

Any ideas?

Upvotes: 14

Views: 15555

Answers (1)

roman
roman

Reputation: 117337

Try something like this (untested):

                ------------------ your connection
                V
Array inArray = conn.createArrayOf("integer", new Integer[][] {{1,10},{2,20}});
stmt.setArray(1, inArray);

Links:

Upvotes: 19

Related Questions