I'm attempting to use Postgres 9.1's prepared statements feature from a Scala app using Postgres's official JDBC driver 9.3-1100.
If I do this, everything works fine and I get "foo" back:
conn.prepareStatement("PREPARE prep(text) AS SELECT $1").executeUpdate();
val cmd = conn.prepareStatement("EXECUTE prep('foo')");
However, if I attempt to parameterize it:
conn.prepareStatement("PREPARE prep(text) AS SELECT $1").executeUpdate();
val cmd = conn.prepareStatement("EXECUTE prep(?)");
cmd.setString(1, "foo");
Then I get this error upon executing the command:
org.postgresql.util.PSQLException: ERROR: there is no parameter $1
Position: 14
org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2161)
org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1890)
org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:560)
org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:417)
org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:410)
com.jolbox.bonecp.PreparedStatementHandle.execute(PreparedStatementHandle.java:140)
I've tried this in several different ways but I can't find any way to parameterize this query successfully.
I've done a similar thing from a .net app using the Npgsql driver and it works, so I can't figure out why the JDBC driver won't let me do this. Is this just a limitation of the JDBC driver? Is there any work-around?
SELECT stringand retrieve that string as a resultset from the database.