Java method to return String that can be used in SQL IN statement -


i have comma separated string need convert in order use in sql in statement e.g select * table filedvalue in(conversion)

below method have come far. value being passed parameter parameter gbl075,gbl008

public string paramconverter(string parameter)     {         string conversion="";          string newstring="";           string[] parts = parameter.split(",");              for(string part : parts)             {                                 newstring = newstring.concat("'"+part+"',");             }                conversion = new stringbuilder().append(newstring).tostring();             int ind = conversion.lastindexof(",");             conversion = new stringbuilder(conversion).delete(ind,ind+1).tostring();             system.out.println(conversion);            return conversion;      } 

however when read console output below results variable conversion

'gbl075','gbl008' 'gbl075','gbl008'

this not work in sql statement. need correct method , need assistance. thanks.

in java 8 added stringjoiner class:

stringjoiner joiner = new stringjoiner(", "); joiner.add("foo"); joiner.add("bar"); joiner.add("baz"); string joined = joiner.tostring(); // "foo, bar, baz" 

check this question, offer different third party libs


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -