fork download
  1. class Teste {
  2. private static final String SQL_INSERT =
  3. "INSERT INTO tabela (col_codigo, col_chave) VALUES ('X', 'Y');\n";
  4.  
  5. public static String juntarTodasAsSQLs(String chave, String[] valores) {
  6. String a = SQL_INSERT.replace("X", chave);
  7. StringBuilder sb = new StringBuilder(valores.length * (a.length() + 5));
  8. for (String valor : valores) {
  9. sb.append(SQL_INSERT.replace("Y", valor));
  10. }
  11. return sb.toString();
  12. }
  13.  
  14. public static void main(String[] args) {
  15. String chave = "999";
  16. String[] arrayOriginal = {"10", "20", "30", "40"};
  17. String resultado = juntarTodasAsSQLs(chave, arrayOriginal);
  18. System.out.println(resultado);
  19. }
  20. }
Success #stdin #stdout 0.1s 320576KB
stdin
Standard input is empty
stdout
INSERT INTO tabela (col_codigo, col_chave) VALUES ('X', '10');
INSERT INTO tabela (col_codigo, col_chave) VALUES ('X', '20');
INSERT INTO tabela (col_codigo, col_chave) VALUES ('X', '30');
INSERT INTO tabela (col_codigo, col_chave) VALUES ('X', '40');