开发者

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.


JDBC doesn't support this kind of query officially and SQL Server doesn't either as far as I know. This topic has been discussed on SO many times and several workarounds have been proposed:

  • What is the best approach using JDBC for parameterizing an IN clause?
  • PreparedStatement IN clause alternatives?
  • pass variable from java to SQL IN clause
  • Trouble with PreparedStatement that uses union of selects query and IN CLAUSE

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. But such a code wouldn't work on oracle DB. In oracle, we cannot pass a java array to an IN clause. 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. Then pass a concatenated string as a parameter.

Again, this answer is applicable for Oracle DB; It might not work on MS SQL server.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜