PreparedStatement 具有可变数量的参数但没有迭代它们

PreparedStatement with variable number of params yet without iterating on them

我正在寻找以下方面的验证:

使用PreparedStatement with an unknown number of parameters is the way shown in this question的方法,在Java/application端没有办法绕过这样的迭代。

我一直在寻找一种 "varargs" 的方法和语法来将参数输入 PreparedStatement 可变数量的参数,比如在集合上,并将其作为单个变量占位符插入进入查询。

不是问题。寻找验证只是为了不错过。

如果您想使用不同的参数,则无法使用 PreparedStatement。 SQL 字符串必须有正确数量的问号。这就是解决方法是先迭代并添加问号,然后从该动态字符串创建 PreparedStatement 的原因。

使用 PreparedStatement 的部分动机是避免 SQL 注入。 PreparedStatement 就像和数据库的契约一样,只有 this 多个参数,带有 this SQL 语句。如果参数可以改变,它就会破坏契约,数据库将不再知道要准备什么。