This question already has an answer here:
- PreparedStatement IN clause alternatives? 27 answers
Say that I have a query of the form
SELECT * FROM MYTABLE WHERE MYCOL in (?)
And I want to parameterize the arguments to in.
Is there a straightforward way to do this in Java with JDBC, in a way that could work on multiple databases without modifying the SQL itself?
The closest question I've found had to do with C#, I'm wondering if there is something different for Java/JDBC.
I got the answer from docs.spring(19.7.3)
Hope this can help you.
See my trial and It success,It is said that the list size has potential limitation. List l = Arrays.asList(new Integer[]{12496,12497,12498,12499}); Map param = Collections.singletonMap("goodsid",l);
There are different alternative approaches that we can use.
Check more details about these here.
sormula makes this simple (see Example 4):
I solved this by constructing the SQL string with as many
?
as I have values to look for.First I searched for an array type I can pass into the statement, but all JDBC array types are vendor specific. So I stayed with the multiple
?
.The standard way to do this is (if you are using Spring JDBC) is to use the org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate class.
Using this class, it is possible to define a List as your SQL parameter and use the NamedParameterJdbcTemplate to replace a named parameter. For example: