简体   繁体   English

如何在Java SQLclarifyParameter调用中声明String数组?

[英]How do I declare a String array in a Java SQL declareParameter call?

Simplified for illustrative purposes: 为了说明目的而简化:

String[] filter = {"foo", "bar"};
String sql = "SELECT * FROM table WHERE column IN ?";
declareParameter(new SqlParameter(Types.ARRAY));

This doesn't work when I execute it (I get a java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Invalid parameter binding(s). exception. 当我执行它时,它不起作用(我得到了java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Invalid parameter binding(s).异常。

JDBC doesn't support this kind of query officially and SQL Server doesn't either as far as I know. JDBC不正式支持这种查询,而据我所知,SQL Server也不支持。 This topic has been discussed on SO many times and several workarounds have been proposed: 已经多次讨论了该主题,并提出了几种解决方法:

You can probably find many more as it is indeed a very relevant yet still open topic. 您可能会发现更多内容,因为这确实是一个非常相关但仍未解决的话题。

I am not sure about MS SQL Server. 我不确定MS SQL Server。 But such a code wouldn't work on oracle DB. 但是这样的代码在oracle DB上不起作用。 In oracle, we cannot pass a java array to an IN clause. 在oracle中,我们不能将java数组传递给IN子句。 The way to work around that limitation is to construct a PL/SQL function that converts a list of strings into a table, something like stringToTable. 解决该限制的方法是构造一个PL / SQL函数,该函数将字符串列表转换为表,类似于stringToTable。 Then pass a concatenated string as a parameter. 然后将串联的字符串作为参数传递。

Again, this answer is applicable for Oracle DB; 同样,此答案适用于Oracle DB。 It might not work on MS SQL server. 它可能无法在MS SQL服务器上运行。

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

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