java - Weird behaviour with collection and StringBuilder -


i try make sql query objects contained in collection , stringbuilder. seems simple code write:

public string makequery(collection<myobject> collection) {      logger.info("the size of collection {}", collection.size());      stringbuilder querystring = new stringbuilder("insert my_table (my_column) values ");     int = 1;     (myobject object : collection) {          querystring.append("('");         querystring.append(object.get...);         querystring.append("')");          if (i == collection.size()) {             querystring.append(";");         } else {             querystring.append(",");         }         i++;      }      logger.info("the size of collection {}", collection.size());      return querystring.tostring();  } 

for me, code should works have result:

 insert my_table (my_column) values ('a'),('b');('c'), 

how possible semi colon not last character ?

edit

i added logs before , after loop, , size of collection same.

you try use iterator() , untill hasnext() normal appending. when hasnext() returns false append ';'. don't need if's then.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -