Discuss / Java / Stringbuilder 链式调用

Stringbuilder 链式调用

Topic source

清尘

#1 Created at ... [Delete] [Delete and Lock User]
public class Main {
    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");
    }

    static String buildInsertSql(String table, String[] fields) {
        // TODO:

        StringBuilder sb = new StringBuilder(1024);
        sb.append("INSERT INTO employee (")
        .append(String.join(", ", fields))
        .append(") VALUES (?, ?, ?)");
        return sb.toString();
    }
}

Joker.fu_95

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

~(@^_^@)~


  • 1

Reply