0

I have a list of strings which is returned from a query in List A.

I am trying to use String Utils.join to combine the values in the list to a string separated by comma and in quotes. But it is not working as expected.

Values in abcList - [abc, cde, fgh]

abcList.addAll(jdbcTemplate.queryForList(abcSql, String.class));
String abc= StringUtils.join(abcList, "','");
abc = "'" +abc+ "'";

Expected output - 'abc', 'cde', 'fgh'
Actual output - 'abc, cde, fgh'

I am not sure what I am doing wrong here as I want to pass the values form the string abc into query with "IN" condition.

4

2 回答 2

1

作为替代方案,您也可以使用stream.Collectors.joining

List<String> myList = Arrays.asList("abc","def","ghi");
String joined = myList.stream().collect(Collectors.joining("','", "'", "'"));
System.out.println(joined);
于 2018-03-21T15:28:22.523 回答
1

如果您使用的是 Java 8,则可以使用本机方法来连接字符串。

List<String> list = <get a list of strings somehow>;
String joinedString = String.join("','", list);

请参阅 String.join javadoc

就像 JDBC 查询的提示一样……您应该使用命名参数在查询中插入值,而不是手动构造查询字符串。

有关示例,请参见此 SO 帖子

于 2018-03-21T15:29:42.277 回答