Discuss / Java / 托尔斯泰

托尔斯泰

Topic source

.

#1 Created at ... [Delete] [Delete and Lock User]
public class TestString {    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) {        // TODO:        var sb = new StringBuilder(1024);        sb.insert(0, "INSERT INTO ");        sb.append(table);//        sb.append(" (");        sb.append(" ("+fields[0]);        sb.append(", "+fields[1]);        sb.append(", "+fields[2]);        sb.append(") "+"VALUES (?, ?, ?)");//        System.out.print(sb.toString());        return sb.toString();    }}

#2 Created at ... [Delete] [Delete and Lock User]

借楼练习题:

 static String buildInsertSql(String table, String[] fields) {
       String fieldStr = " (" + String.join(", ",fields) + ")";
       StringBuilder sb = new StringBuilder(1024);
        sb.append("INSERT INTO ")
          .append(table)
          .append(fieldStr)
          .append(" VALUES (?, ?, ?)");
        return sb.toString();
    }

  • 1

Reply