Discuss / Java / append加入三元 用foreach 循环

append加入三元 用foreach 循环

Topic source
/** * @author Xixian */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) ? "测试成功" : "测试失败");    }    static String buildInsertSql(String table, String[] fields) {        StringBuilder s = new StringBuilder(1024);        s.append("INSERT INTO ").append(table).append(" (");        for (String sys : fields) {            s.append(sys.equals(fields[fields.length - 1]) ? sys : sys + ", ");        }        s.append(") ").append("VALUES (?, ?, ?)");        return s.toString();    }}

  • 1

Reply