Which one is correct and why:
String dynamic = new String();
dynamic = \" where id=\'\" + unitId + \"\'\";
Or
String dynamic = \" where id=\'\" + unitId + \"
String dynamic = new String();
dynamic = " where id='" + unitId + "'";
creates a binding to an empty string, and then dumps it completely by re-initializing it.
String dynamic = " where id='" + unitId + "'";
creates the binding and leaves it.
The first one is unnecessary. Just go with the direct string literal.