pg.ddx.io pgsql-jdbc@postgresql.org mailing list archive
help / color / mirror / Atom feedJDBC parameter binding not working for PL/PGSQL block JDBC3 Version 9.0-801
5+ messages / 2 participants
[nested] [flat]
* JDBC parameter binding not working for PL/PGSQL block JDBC3 Version 9.0-801
@ 2011-04-27 19:35 Brady S Edwards <brady.s.edwards@seagate.com>
0 siblings, 2 replies; 5+ messages in thread
From: Brady S Edwards @ 2011-04-27 19:35 UTC (permalink / raw)
To: pgsql-jdbc
Database postgresql90-server-9.0.2-1PGDG.rhel4
CentOS 4.7
JDBC3 Version 9.0-901.
I'm working on a project converting a number of applications from Oracle to
Postgres and am running into a problem with the Postgres JDBC driver or the
database.
When trying to setString(1,?) where the ? is in a pl/pgsql block I get an
error.
I included the error and portion of the code.
> java WhatTheF
Trying to connect
Looks like connections succeeded
Bound Vars = 1
Bound Vars = 0
org.postgresql.util.PSQLException: The column index is out of range: 1,
number of columns: 0.
at
org.postgresql.core.v3.SimpleParameterList.bind(SimpleParameterList.java:53)
at
org.postgresql.core.v3.SimpleParameterList.setStringParameter(SimpleParameterList.java:118)
at
org.postgresql.jdbc2.AbstractJdbc2Statement.bindString(AbstractJdbc2Statement.java:2184)
at
org.postgresql.jdbc2.AbstractJdbc2Statement.setString(AbstractJdbc2Statement.java:1303)
at
org.postgresql.jdbc2.AbstractJdbc2Statement.setString(AbstractJdbc2Statement.java:1289)
at WhatTheF.main(WhatTheF.java:37)
try {
System.out.println("Trying to connect");
Class.forName ("org.postgresql.Driver");
Properties props = new Properties();
props.setProperty("user", PG_USER);
props.setProperty("password", PG_PASS);
Connection pgConn = DriverManager.getConnection(PG_CONN_PROP,
props);
System.out.println("Looks like connections succeeded\n");
PreparedStatement pStmt = pgConn.prepareStatement(
"INSERT INTO soo (zoo) values (?)");
ParameterMetaData pmd = pStmt.getParameterMetaData();
System.out.println("Bound Vars = " + pmd.getParameterCount());
pStmt.setString(1, "Monkey");
pStmt.executeUpdate();
pStmt.close();
pStmt = pgConn.prepareStatement(
" DO LANGUAGE plpgsql "
+ " $$\n"
+ " DECLARE\n"
+ " booger varchar(20);\n"
+ " BEGIN\n"
+ " booger := $?OTTER$?BOOG$?;\n"
+ " INSERT INTO soo (zoo) values (booger);\n"
+ " END $$;");
pmd = pStmt.getParameterMetaData();
System.out.println("Bound Vars = " + pmd.getParameterCount());
LINE 37 -> pStmt.setString(1, "Barf");
pStmt.executeUpdate();
pStmt.close();
pgConn.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
--
Brady Edwards
(720) 684-2984
Seagate Technology
^ permalink raw reply [nested|flat] 5+ messages in thread
* JDBC parameter binding not working for PL/PGSQL block JDBC3 Version 9.0-801
@ 2011-04-27 20:11 Brady S Edwards <brady.s.edwards@seagate.com>
parent: Brady S Edwards <brady.s.edwards@seagate.com>
1 sibling, 0 replies; 5+ messages in thread
From: Brady S Edwards @ 2011-04-27 20:11 UTC (permalink / raw)
To: pgsql-jdbc
Had a slight problem in the Java that I fixed but the error is still the same.
Database postgresql90-server-9.0.2-1PGDG.rhel4
CentOS 4.7
JDBC3 Version 9.0-901.
I'm working on a project converting a number of applications from
Oracle to Postgres and am running into a problem with the Postgres
JDBC driver or the database.
When trying to setString(1,?) where the ? is in a pl/pgsql block I get an error.
I included the error and portion of the code:
/mnt/ae_files/users/brady/postgres> java WhatTheF
Trying to connect
Looks like connections succeeded
Bound Vars = 1
Bound Vars = 0
org.postgresql.util.PSQLException: The column index is out of range:
1, number of columns: 0.
at org.postgresql.core.v3.SimpleParameterList.bind(SimpleParameterList.java:53)
at org.postgresql.core.v3.SimpleParameterList.setStringParameter(SimpleParameterList.java:118)
at org.postgresql.jdbc2.AbstractJdbc2Statement.bindString(AbstractJdbc2Statement.java:2184)
at org.postgresql.jdbc2.AbstractJdbc2Statement.setString(AbstractJdbc2Statement.java:1303)
at org.postgresql.jdbc2.AbstractJdbc2Statement.setString(AbstractJdbc2Statement.java:1289)
at WhatTheF.main(WhatTheF.java:35)
try {
System.out.println("Trying to connect");
Class.forName ("org.postgresql.Driver");
Properties props = new Properties();
props.setProperty("user", PG_USER);
props.setProperty("password", PG_PASS);
Connection pgConn = DriverManager.getConnection(PG_CONN_PROP, props);
System.out.println("Looks like connections succeeded\n");
PreparedStatement pStmt = pgConn.prepareStatement(
"INSERT INTO soo (zoo) values (?)");
ParameterMetaData pmd = pStmt.getParameterMetaData();
System.out.println("Bound Vars = " + pmd.getParameterCount());
pStmt.setString(1, "Monkey");
pStmt.executeUpdate();
pStmt.close();
pStmt = pgConn.prepareCall(
" DO LANGUAGE plpgsql "
+ " $$\n"
+ " DECLARE\n"
+ " BEGIN\n"
+ " INSERT INTO soo (zoo) values (?);\n"
+ " END $$;");
pmd = pStmt.getParameterMetaData();
System.out.println("Bound Vars = " + pmd.getParameterCount());
LINE 35 -> pStmt.setString(1, "Barf");
pStmt.executeUpdate();
pStmt.close();
pgConn.close();
}
catch (Exception e) {
e.printStackTrace();
}
I'm guessing that anonymous plpgsql blocks cannot take input parameters.
--
Brady Edwards
(720) 684-2984
Seagate Technology
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: JDBC parameter binding not working for PL/PGSQL block JDBC3 Version 9.0-801
@ 2011-04-27 20:17 Oliver Jowett <oliver@opencloud.com>
parent: Brady S Edwards <brady.s.edwards@seagate.com>
1 sibling, 1 reply; 5+ messages in thread
From: Oliver Jowett @ 2011-04-27 20:17 UTC (permalink / raw)
To: Brady S Edwards <brady.s.edwards@seagate.com>; +Cc: pgsql-jdbc
On 28 April 2011 07:35, Brady S Edwards <brady.s.edwards@seagate.com> wrote:
> When trying to setString(1,?) where the ? is in a pl/pgsql block I get an
> error.
? is not considered a parameter marker if it occurs inside a literal string.
The pl/pgsql code in your case is one big literal string (delimited by $$)
It's just the same case as "SELECT 'should this be a parameter?'",
which is a query that takes no parameters despite happening to contain
a ? character.
Oliver
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: JDBC parameter binding not working for PL/PGSQL block JDBC3 Version 9.0-801
@ 2011-04-27 23:17 Oliver Jowett <oliver@opencloud.com>
parent: Oliver Jowett <oliver@opencloud.com>
0 siblings, 1 reply; 5+ messages in thread
From: Oliver Jowett @ 2011-04-27 23:17 UTC (permalink / raw)
To: Brady S Edwards <brady.s.edwards@seagate.com>; +Cc: pgsql-jdbc
On 28 April 2011 08:40, Brady S Edwards <brady.s.edwards@seagate.com> wrote:
> Thank you for your reply Oliver.
>
> Is it possible to call an anonymous plpgsql block with bind variables?
> With Oracle I would just prepare a statement like:
> declare
> ...
> begin
> xx = ?;
> ...
> end;
>
> These are created on the fly, so I can't really create stored
> procedures for them.
Looking at the description of DO, I don't see a way to provide parameters:
> The code block is treated as though it were the body of a function with no parameters, returning void. It is parsed and executed a single time.
If the body of the block is used just once, then it may be simplest to
just interpolate the parameter values into the block body yourself
while generating it.
If you're going to reuse the block with different values, though, it's
probably worthwhile creating a proper function, even if you turn
around and drop it later. (You could perhaps create it in the pg_temp
schema)
If you don't have explicit reuse of these statements but the same
generated block is likely to be generated again later, you could do
something like a per-connection cache of function text to temporary
function definition.
Oliver
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: JDBC parameter binding not working for PL/PGSQL block JDBC3 Version 9.0-801
@ 2011-04-28 13:01 Brady S Edwards <brady.s.edwards@seagate.com>
parent: Oliver Jowett <oliver@opencloud.com>
0 siblings, 0 replies; 5+ messages in thread
From: Brady S Edwards @ 2011-04-28 13:01 UTC (permalink / raw)
To: Oliver Jowett <oliver@opencloud.com>; +Cc: pgsql-jdbc
Thank you again Oliver for your reply.
I ended up parsing the parameters into the string block and then
preparing the statement for the short term.
I'm getting good performance currently.
For the long term I'll use your advice and create functions where appropriate.
Thanks again
On Wed, Apr 27, 2011 at 5:17 PM, Oliver Jowett <oliver@opencloud.com> wrote:
> On 28 April 2011 08:40, Brady S Edwards <brady.s.edwards@seagate.com> wrote:
>> Thank you for your reply Oliver.
>>
>> Is it possible to call an anonymous plpgsql block with bind variables?
>> With Oracle I would just prepare a statement like:
>> declare
>> ...
>> begin
>> xx = ?;
>> ...
>> end;
>>
>> These are created on the fly, so I can't really create stored
>> procedures for them.
>
> Looking at the description of DO, I don't see a way to provide parameters:
>
>> The code block is treated as though it were the body of a function with no parameters, returning void. It is parsed and executed a single time.
>
> If the body of the block is used just once, then it may be simplest to
> just interpolate the parameter values into the block body yourself
> while generating it.
> If you're going to reuse the block with different values, though, it's
> probably worthwhile creating a proper function, even if you turn
> around and drop it later. (You could perhaps create it in the pg_temp
> schema)
> If you don't have explicit reuse of these statements but the same
> generated block is likely to be generated again later, you could do
> something like a per-connection cache of function text to temporary
> function definition.
>
> Oliver
>
--
Brady Edwards
(720) 684-2984
Seagate Technology
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2011-04-28 13:01 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2011-04-27 19:35 JDBC parameter binding not working for PL/PGSQL block JDBC3 Version 9.0-801 Brady S Edwards <brady.s.edwards@seagate.com>
2011-04-27 20:11 ` Brady S Edwards <brady.s.edwards@seagate.com>
2011-04-27 20:17 ` Oliver Jowett <oliver@opencloud.com>
2011-04-27 23:17 ` Oliver Jowett <oliver@opencloud.com>
2011-04-28 13:01 ` Brady S Edwards <brady.s.edwards@seagate.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