Discuss / Java / 作业,已验证。

作业,已验证。

Topic source

Nobody52297

#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);

System.out.println("INSERT INTO employee (name, position, salary) VALUES (?, ?, ?)".equals(insert) ? "测试成功" : "测试失败");

}

static String buildInsertSql(String table, String[] fields) {

// TODO:

StringBuilder sb = new StringBuilder();

sb.append("INSERT INTO ").append(table).append(" (");

for(String i:fields) {

sb.append(i).append(", ");

}

sb.deleteCharAt(sb.length() - 1).deleteCharAt(sb.length() - 1).append(") VALUES (?, ?, ?)");

return sb.toString();

}

}


  • 1

Reply