1

If i have a query that is similar to the one shown below

select first_name, author, location
from author_details
where first_name=?

The ? value is populated on the Java side using

statement = getConnection().prepareStatement(MY_QUERY);
            statement.setString(1, name);

I want to update the query for an additional check in the where clause:

select first_name, author, location
from author_details
where first_name=? or last_name=?

I want to use the same value that is passed in as the 'name' from the java code. Is it possible to use the same passed in value to check both the first_name and last_name or do i have to add another statement.setString(2, name) entry on the java code?

I know within SQLPlus i can declare a variable and use it later but im not sure how to do the same on an sql query executed through JDBC

1 Answer 1

1

JDBC (for Oracle at least) creates distinct bind variable names for all ? placeholders in the PreparedStatement query. You can't have one setString call cover multiple bind variables. That means that you'll have to add another call to setString on the second parameter, even if you're passing in the same name variable.

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

Comments

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.