PreparedStatement IN clause alternatives?

前端 未结 30 3887
情歌与酒
情歌与酒 2020-11-21 05:19

What are the best workarounds for using a SQL IN clause with instances of java.sql.PreparedStatement, which is not supported for multiple values du

30条回答
  •  甜味超标
    2020-11-21 05:33

    Just for completeness and because I did not see anyone else suggest it:

    Before implementing any of the complicated suggestions above consider if SQL injection is indeed a problem in your scenario.

    In many cases the value provided to IN (...) is a list of ids that have been generated in a way that you can be sure that no injection is possible... (e.g. the results of a previous select some_id from some_table where some_condition.)

    If that is the case you might just concatenate this value and not use the services or the prepared statement for it or use them for other parameters of this query.

    query="select f1,f2 from t1 where f3=? and f2 in (" + sListOfIds + ");";
    

提交回复
热议问题