Discuss / Java / 使用StringBuilder构造一个INSERT语句

使用StringBuilder构造一个INSERT语句

Topic source
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) ? "success": "fail");}public static String buildInsertSql(String table, String[] fields) {    StringBuffer sb = new StringBuffer(1024);    String s = "INSERT INTO %s (%s, %s, %s) VALUES (?, ?, ?)";    String s2 = String.format(s, table, fields[0], fields[1], fields[2]);    sb.append(s2);    String result = sb.toString();    return result;}

  • 1

Reply