class Teste {
private static final String SQL_INSERT
= "INSERT INTO tabela (col_codigo, col_chave) VALUES ('X', 'Y');\n";
String a
= SQL_INSERT.
replace("X", chave
); StringBuilder sb = new StringBuilder(valores.length * (a.length() + 5));
for (String valor
: valores
) { sb.append(SQL_INSERT.replace("Y", valor));
}
return sb.toString();
}
public static void main
(String[] args
) { String[] arrayOriginal
= {"10",
"20",
"30",
"40"}; String resultado
= juntarTodasAsSQLs
(chave, arrayOriginal
); System.
out.
println(resultado
); }
}
Y2xhc3MgVGVzdGUgewogICAgcHJpdmF0ZSBzdGF0aWMgZmluYWwgU3RyaW5nIFNRTF9JTlNFUlQgPQogICAgICAgICJJTlNFUlQgSU5UTyB0YWJlbGEgKGNvbF9jb2RpZ28sIGNvbF9jaGF2ZSkgVkFMVUVTICgnWCcsICdZJyk7XG4iOwoKICAgIHB1YmxpYyBzdGF0aWMgU3RyaW5nIGp1bnRhclRvZGFzQXNTUUxzKFN0cmluZyBjaGF2ZSwgU3RyaW5nW10gdmFsb3JlcykgewogICAgICAgIFN0cmluZyBhID0gU1FMX0lOU0VSVC5yZXBsYWNlKCJYIiwgY2hhdmUpOwogICAgICAgIFN0cmluZ0J1aWxkZXIgc2IgPSBuZXcgU3RyaW5nQnVpbGRlcih2YWxvcmVzLmxlbmd0aCAqIChhLmxlbmd0aCgpICsgNSkpOwogICAgICAgIGZvciAoU3RyaW5nIHZhbG9yIDogdmFsb3JlcykgewogICAgICAgICAgICBzYi5hcHBlbmQoU1FMX0lOU0VSVC5yZXBsYWNlKCJZIiwgdmFsb3IpKTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHNiLnRvU3RyaW5nKCk7CiAgICB9CgogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgewogICAgICAgIFN0cmluZyBjaGF2ZSA9ICI5OTkiOwogICAgICAgIFN0cmluZ1tdIGFycmF5T3JpZ2luYWwgPSB7IjEwIiwgIjIwIiwgIjMwIiwgIjQwIn07CiAgICAgICAgU3RyaW5nIHJlc3VsdGFkbyA9IGp1bnRhclRvZGFzQXNTUUxzKGNoYXZlLCBhcnJheU9yaWdpbmFsKTsKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4ocmVzdWx0YWRvKTsKICAgIH0KfQ==
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');