PreparedStatement IN clause alternatives?

前端 未结 30 3888
情歌与酒
情歌与酒 2020-11-21 05:19

What are the best workarounds for using a SQL IN clause with instances of java.sql.PreparedStatement, which is not supported for multiple values du

30条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-21 05:54

    Just for completeness: So long as the set of values is not too large, you could also simply string-construct a statement like

    ... WHERE tab.col = ? OR tab.col = ? OR tab.col = ?
    

    which you could then pass to prepare(), and then use setXXX() in a loop to set all the values. This looks yucky, but many "big" commercial systems routinely do this kind of thing until they hit DB-specific limits, such as 32 KB (I think it is) for statements in Oracle.

    Of course you need to ensure that the set will never be unreasonably large, or do error trapping in the event that it is.

提交回复
热议问题