StringBuilder 小练习实现方法二

`public class Demo13 {
public static void main(String[] args) {
String[] fields = { "name", "position", "salary" };
String table = "employee";
String insert = buildInsertSql(table, fields);
System.out.println(insert);
String s = "INSERT INTO employee (name, position, salary) VALUES (?, ?, ?)";
System.out.println(s.equals(insert) ? "测试成功" : "测试失败");
}
static String buildInsertSql(String table, String[] fields) {

	StringBuilder sb = new StringBuilder(1024);
	sb.append("INSERT INTO "+ table + " (");
	
	//使用String.join()方法操作数组,第一个参数是分隔符
	String s = String.join(", ",fields);
	
	sb.append(s)
	  .append(") VALUES (?, ?, ?)");
	
    return sb.toString();
}

}`

posted @ 2020-12-23 14:31  dog_IT  阅读(77)  评论(0编辑  收藏  举报