如何在预准备语句上设置参数列表?

2022-09-01 07:44:55

我有一个名字列表,例如:

List<String> names = ...
names.add('charles');
...

和一个语句:

PreparedStatement stmt = 
  conn.prepareStatement('select * from person where name in ( ? )');

如何执行以下操作:

stmt.setParameterList(1,names);

是否有解决方法?有人可以解释为什么缺少这种方法吗?

using: java, postgresql, jdbc3


答案 1

这个问题很老了,但没有人建议使用setArray。

这个答案可能有助于 https://stackoverflow.com/a/10240302/573057


答案 2

没有一种干净的方法可以做到这一点,只需在我所知道的上设置一个列表。PreparedStatement

编写代码,将 SQL 语句构造为具有适当数量的问号(与列表中的数字相同),然后循环访问列表,为每个问号设置参数。


推荐