Using “where in” in spring-jdbc

半城伤御伤魂 提交于 2019-12-11 06:42:30

问题


Is there a way to delete a bunch of elements using "where... in" in SQL, like so:

HashSet<String> idStrings = ...;
SimpleJdbcTemplate template = getTemplate();
template.update("DELETE FROM records WHERE idstring IN (?)", idStrings);

I am trying to get some old code to work that uses this method, but whenever I try to run it the Oracle JDBC drivers throw an exception:

QL state [99999]; error code [17004]; Invalid column type; nested exception is java.sql.SQLException: Invalid column type

This is with ojdbc5 11.2.0.1.0 and spring-jdbc 3.0.3


回答1:


It's possible, but you need one placeholder (?) per ID, and each ID must be bound separately.

You could also use NamedParameterJdbcTemplate, which

[...] also allows for expanding a List of values to the appropriate number of placeholders.

Be careful not to put too many values in the set of IDs. Oracle limits them to 1000, for example.



来源:https://stackoverflow.com/questions/8837094/using-where-in-in-spring-jdbc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!