N
N
Nikolay Baranenko2017-08-20 09:53:13
Java
Nikolay Baranenko, 2017-08-20 09:53:13

How to dynamically define conditions for a parametrized select?

Hello.
For example, I have a parametrized select

String sql = "SELECT STUDENT FROM SCHOOL WHERE SCHOOL LIKE ? ";
PreparedStatement prepStmt = conn.prepareStatement(sql);
prepStmt.setString(1, "Waterloo%");
ResultSet rs = prepStmt.executeQuery();

everything is fine for cases when the filter in the select is constant, BUT there is a situation when the
select must be dynamically interpreted like
SELECT STUDENT FROM SCHOOL WHERE 
SCHOOL LIKE "Waterloo%" or SCHOOL LIKE "Stackloo%"

String sql = "SELECT STUDENT FROM SCHOOL WHERE SCHOOL LIKE ? OR SCHOOL LIKE ?";
PreparedStatement prepStmt = conn.prepareStatement(sql);
prepStmt.setString(1, filter_select);
prepStmt.setString(2, filter_select);
ResultSet rs = prepStmt.executeQuery();

What is the best way to solve this problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
Zakharov Alexander, 2017-08-20
@drno-reg

There is no special built-in mechanism in jdbc. You must first generate a string for a new request with parameters, then set these parameters. Or prepare several queries with different conditions and also parameterize them before execution.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question