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 sql = new StringBuilder(1024); sql.append("INSERT INTO ") .append(table) .append(" ("); for (int i = 0, len = fields.length; i < len; i++) { sql.append(fields[i]); if (i < len - 1) { sql.append(", "); } } sql.append(") ").append("VALUES (?, ?, ?)"); return sql.toString(); } }
Sign in to make a reply
一切繁华皆落尽