pg.ddx.io pgsql-jdbc@postgresql.org mailing list archive
help / color / mirror / Atom feedarrays as parameters in callableStatements
4+ messages / 2 participants
[nested] [flat]
* arrays as parameters in callableStatements
@ 2006-05-15 02:56 J. <sweepingoar@yahoo.com>
0 siblings, 1 reply; 4+ messages in thread
From: J. @ 2006-05-15 02:56 UTC (permalink / raw)
To: pgsql-jdbc
I've got a PL/pgSQL stored function in my database and I'm trying to figure out how to call it from my admin script. Some of the parameters are ArrayList objects of strings and it returns an array that I want to read to determine what the function actually did. Here is the function header(?):
CREATE OR REPLACE FUNCTION add_thing(
varchar, -- name:varchar ($1)
integer, -- itemNum:integer ($2)
varchar, -- owner:varchar ($3)
varchar, -- image_url:varchar ($4)
varchar, -- specs_url:varchar ($5)
varchar[], -- categories[]:varchar ($6)
varchar[], -- component_name[]:varchar ($7)
varchar[], -- component_id[]:varchar ($8)
date[], -- component_date[]:date ($9)
varchar[] -- componentImage_url[]:varchar ($10)
) RETURNS varchar[] as '
Here is how I'm trying to call it in Java:
// Turn transactions off.
connection.setAutoCommit(false);
// Procedure call.
CallableStatement stmt = connection.prepareCall("{ ? = call add_thing( ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) }");
stmt.registerOutParameter(1, Types.ARRAY);
stmt.setString(2, name);
stmt.setInt(3, itemNum);
stmt.setString(4, owner);
stmt.setString(5, item_url);
stmt.setString(6, specs);
stmt.setObject(7, compNames);
stmt.setObject(8, compIds);
stmt.setObject(9, compDates);
stmt.setObject(10, compURL);
stmt.execute();
String[] results = stmt.getArray(1);
stmt.close();
I'm getting a compiler error "Incompatible Types" on the second to last line. Looking at the API to me it seems like that's how it should work. I'm thinking of trying getResultSet() instead, but since this is only the compile stage I'm asking for some advice in case I'm way off base (in particular, aside from the error mentioned I'm concerned about IN params 7-10 being written correctly). Thanks.
-Jason
---------------------------------
Yahoo! Messenger with Voice. Make PC-to-Phone Calls to the US (and 30+ countries) for 2ยข/min or less.
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: arrays as parameters in callableStatements
@ 2006-05-15 04:10 Kris Jurka <books@ejurka.com>
parent: J. <sweepingoar@yahoo.com>
0 siblings, 1 reply; 4+ messages in thread
From: Kris Jurka @ 2006-05-15 04:10 UTC (permalink / raw)
To: J. <sweepingoar@yahoo.com>; +Cc: pgsql-jdbc
On Sun, 14 May 2006, J. wrote:
> I've got a PL/pgSQL stored function in my database and I'm trying to
> figure out how to call it from my admin script. Some of the parameters
> are ArrayList objects of strings and it returns an array that I want to
> read to determine what the function actually did. Here is the function
> header(?):
>
> String[] results = stmt.getArray(1);
>
> I'm getting a compiler error "Incompatible Types" on the second to last
> line.
getArray returns a java.sql.Array, not a String[]. Also the JDBC driver
does not know how to interpret array parameters that are not sent in via
setArray or setObject with a java.sql.Array.
Kris Jurka
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: arrays as parameters in callableStatements
@ 2006-05-15 04:25 J. <sweepingoar@yahoo.com>
parent: Kris Jurka <books@ejurka.com>
0 siblings, 1 reply; 4+ messages in thread
From: J. @ 2006-05-15 04:25 UTC (permalink / raw)
To: Kris Jurka <books@ejurka.com>; +Cc: pgsql-jdbc
Thanks, I thought that might be an issue, but looking at the API, I don't see how to convert an array or ArrayList to an Array object or back. Any tips on where to look?
Kris Jurka <books@ejurka.com> wrote:
On Sun, 14 May 2006, J. wrote:
> I've got a PL/pgSQL stored function in my database and I'm trying to
> figure out how to call it from my admin script. Some of the parameters
> are ArrayList objects of strings and it returns an array that I want to
> read to determine what the function actually did. Here is the function
> header(?):
>
> String[] results = stmt.getArray(1);
>
> I'm getting a compiler error "Incompatible Types" on the second to last
> line.
getArray returns a java.sql.Array, not a String[]. Also the JDBC driver
does not know how to interpret array parameters that are not sent in via
setArray or setObject with a java.sql.Array.
Kris Jurka
---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend
---------------------------------
Get amazing travel prices for air and hotel in one click on Yahoo! FareChase
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: arrays as parameters in callableStatements
@ 2006-05-15 09:47 Kris Jurka <books@ejurka.com>
parent: J. <sweepingoar@yahoo.com>
0 siblings, 0 replies; 4+ messages in thread
From: Kris Jurka @ 2006-05-15 09:47 UTC (permalink / raw)
To: J. <sweepingoar@yahoo.com>; +Cc: pgsql-jdbc
On Sun, 14 May 2006, J. wrote:
> Thanks, I thought that might be an issue, but looking at the API, I
> don't see how to convert an array or ArrayList to an Array object or
> back. Any tips on where to look?
>
For java.sql.Array -> type[] it's easy, the Array.getArray call will
return a String[] or int[] or whatever typed array is appropriate.
The type[] -> java.sql.Array side is more complicated, searching this
lists archives will give you some example, but the basic idea is that you
create a class that implements java.sql.Array and can therefore accept any
input format you need.
Kris Jurka
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2006-05-15 09:47 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-05-15 02:56 arrays as parameters in callableStatements J. <sweepingoar@yahoo.com>
2006-05-15 04:10 ` Kris Jurka <books@ejurka.com>
2006-05-15 04:25 ` J. <sweepingoar@yahoo.com>
2006-05-15 09:47 ` Kris Jurka <books@ejurka.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox