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) { // TODO: StringBuilder sb = new StringBuilder(new String("INSERT INTO " + table + " (")); for (String f : fields) { sb.append(f); sb.append(", "); } int index = sb.lastIndexOf(", "); sb.delete(index, index+2); sb.append(") VALUES ("); for (String f : fields) { sb.append('?'); sb.append(", "); } index = sb.lastIndexOf(", "); sb.delete(index, index+2); sb.append(')'); return sb.toString(); }}
Sign in to make a reply
回来的狼2013