StringBuilder を使用することをお勧めします。特に sql または params が長い文字列の場合、文字列操作のタイプでパフォーマンスが向上します。
次に例を示します。
String param = "aa,bb,cc";
String str =
"select column_val from table_name where a = '?' and b = '?' and c = '?'";
@Test
public void Substitute(){
StringBuilder builder=new StringBuilder(str);
String[] params = param.split(",");
int position=0;
for (String paramValue:params){
position=builder.indexOf("?",position);
if (position==-1)
throw new RuntimeException("too parameter values specified.");
builder.replace(position,position+1,paramValue);
position++;
}
position=str.indexOf("?",position);
if (position!=-1)
throw new RuntimeException("Not all parameter specified.");
Assert.assertEquals(builder.toString(),
"select column_val from table_name where a = 'aa' and b = 'bb' and c = 'cc'");
}
他の人が言ったように、セキュリティの問題を回避するために param 値をサニタイズすることを忘れないでください...