简体   繁体   English

在spring-jdbc中使用“ where in”

[英]Using “where in” in spring-jdbc

Is there a way to delete a bunch of elements using "where... in" in SQL, like so: 有没有一种方法可以在SQL中使用“ where ... in”删除一堆元素,如下所示:

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: 我试图使一些使用此方法的旧代码正常工作,但是每当我尝试运行它时,Oracle JDBC驱动程序都会引发异常:

QL state [99999]; QL状态[99999]; error code [17004]; 错误代码[17004]; Invalid column type; 无效的列类型; nested exception is java.sql.SQLException: Invalid column type 嵌套异常为java.sql.SQLException:无效的列类型

This is with ojdbc5 11.2.0.1.0 and spring-jdbc 3.0.3 这与ojdbc5 11.2.0.1.0和spring-jdbc 3.0.3一起使用

It's possible, but you need one placeholder ( ? ) per ID, and each ID must be bound separately. 可以,但是每个ID需要一个占位符( ? ),并且每个ID必须单独绑定。

You could also use NamedParameterJdbcTemplate , which 您还可以使用NamedParameterJdbcTemplate ,它

[...] 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. 注意不要在ID集中放置太多值。 Oracle limits them to 1000, for example. 例如,Oracle将它们限制为1000。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM