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) {
// insert sql
StringBuilder sb = new StringBuilder(20);
StringBuilder qm = new StringBuilder(20); // question mark
for(int i = 0;; i++) {
sb.append(fields[i]);
qm.append("?");
if(i==fields.length-1) break;
sb.append(", ");
qm.append(", ");
return "INSERT INTO " + table + " (" + sb.toString() + ") VALUES (" + qm.toString() + ")";
Sign in to make a reply
🐳smlie
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) {
// insert sql
StringBuilder sb = new StringBuilder(20);
StringBuilder qm = new StringBuilder(20); // question mark
for(int i = 0;; i++) {
sb.append(fields[i]);
qm.append("?");
if(i==fields.length-1) break;
sb.append(", ");
qm.append(", ");
}
return "INSERT INTO " + table + " (" + sb.toString() + ") VALUES (" + qm.toString() + ")";
}
}