38

In many programming languages something like this is possible for prepared statements:

PreparedStatement statement = connection.prepareStatement(
    "SELECT id FROM Company WHERE name LIKE ${name}");
statement.setString("name", "IBM");

But not with java.sql.PreparedStatement. In Java one has to use parameter indices:

PreparedStatement statement = connection.prepareStatement(
    "SELECT id FROM Company WHERE name LIKE ?");
statement.setString(1, "IBM");

Is there a solution to work with string variables like in the first example? Is "${.*}" not used somewhere else in the SQL language, or are there any conflicts? Cause then I would implement it by myself (parsing the SQL string and replacing every variable by "?" and then doing it the Java way).

Regards, Kai

4
  • Hey kd304, I wonder why you didn't leave your message as answer instead of using a comment. Cause I think it's the way for me to solve that problem. Commented Jul 8, 2009 at 13:32
  • @tokel: I wasn't sure about your question being theoretical or not and my comment does not really answer your typed question. Commented Jul 8, 2009 at 21:24
  • Even though ... thank you :-) Commented Jul 8, 2009 at 22:33
  • "In Java one has to use parameter indices:" - actually, it doesn't look like that in your sample. In Java, one cannot even use parameter indices in the SQL string. Commented Nov 25, 2014 at 15:04

3 Answers 3

31

Standard JDBC PreparedStatements don't have this ability. Spring JDBC provides this functionality through NamedParameterJdbcTemplate.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the very good solution. The problem for me is that it will be part of another open source framework and I don't want to import so much code there. Otherwise I would have used your solution :-)
14

As kd304 mentioned in the comment to my posting, this is a very nice solution if you don't want to incorporate another 3rd party library (like Spring) into your project: Javaworld Article: Named Parameters for PreparedStatement

2 Comments

Link no longer works...
@XelaNimed that's why link-only answer aren't accepted anymore, but it might have moved to this. Looks at least similar to the old page.
6

Using a raw PreparedStatement, this is not possible, as you say. It is possible with CallableStatement, but that requires a stored procedure rather than just a SQL statement.

ORM layers like Hibernate also provide named parameter substitution, and Hibernate also allows you to execute native SQL, bypassing the OR mapping functionality completely.

So if you were really keen to use named parameters, you could employ Hibernate as a way of doing this; you'd only be using a tiny fraction of its functionality.

3 Comments

Forget my idea, @laz has a much better one.
I think Oracle JDBC driver supports calling regular SQL statements with named parameters when using CallableStatement, not just for procedures. PostgreSQL says "Not yet implements" (postgresql-9.1-901.jdbc4.jar)
That's so sad, as DBMSsupports this, at least SQL server does, and JSBC intrnaly creates named parameters, like following: @P0 nvarchar(4000),@P1 varchar(8000),@P2 nvarchar(4000),@P3 smallint,@P4 nvarchar(4000),@P5 int

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.