I tried the solution provided by Farmour. It is simple, efficient and works. I changed it according to my need ... & also tried debugging to ensure that it is formatted exactly the way I want.
public static String sqlFormatedList (list listList) {StringBuilder sb = new StringBuilder (); sb.append ("(");
for (String str : codeList){ sb.append("'"+str+"',"); } sb.deleteCharAt(sb.length() -1); sb.append(")"); return sb.toString(); }
Analysis in brief: Input pairs: [ABC, MNP, XYZ]; as a list of strings in Java format
Output: ('ABC', 'MNP', 'XYZ'); as a String for use in an SQL query.
sqlFormattedString = ('ABC', 'MNP', 'XYZ');
In this query, the above line is o / p. select * from tableName as tn, where tn.code in sqlFormattedString;
PS: codeList in the parameter list - java.util.List
source share