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
Post a Comment