To fit a SQL template in a Java app, I need to format elements as follows:
RIGHT JOIN (values (123), (456), (789)..
In particular, given a Java list of integers:
List<int> ints = Arrays.asList(123, 456, 789)
How can I correctly use StringJoiner (examples) to output the following string?
(123), (456), (789)
The closest example I can find is
StringJoiner mystring = new StringJoiner(",", "(", ")");
Which returns
(123, 456, 789)
More Background/Context
MadProgrammer pointed out that I may approach this differently via PreparedStatement. Here is the actual query I'm trying to fill...
So for example, here is a snippet of the query in native SQL:
SELECT loadlist.id as load_id, payload
FROM payload_log
RIGHT JOIN (values (123), (456), (789)) as loadlist(id)
ON payload_log.entity_id = loadlist.id;
And here is how it's being called from application. Ideally we could bind using the :ids parameter, but not sure how so was going to do String.format w/ the joiner.
String queryString = String.format("SELECT loadlist.id as load_id, payload\n"+
"FROM payload_log" +
" RIGHT JOIN (values (:ids)) as loadlist(id)" +
" ON payload_log.entity_id = loadlist.id)";
return entityManager.createNativeQuery(queryString).setParameter("ids", loadIds);
Now I should be able to pass the :ids parameter using query.setParameter after applying formatting as in answers below? So where does a PreparedStatement fit in?