Discuss / Java / 练习:请使用StringJoiner构造一个SELECT语句

练习:请使用StringJoiner构造一个SELECT语句

Topic source

韦雪松

#1 Created at ... [Delete] [Delete and Lock User]
import java.util.StringJoiner;

public class Main {

    public static void main(String[] args) {
        String[] fields = {"name", "position", "salary"};
        String table = "employee";
        String select = buildSelectSql(table, fields);
        System.out.println(select);
        System.out.println("SELECT name, position, salary FROM employee".equals(select) ? "测试成功" : "测试失败");
    }

    public static String buildSelectSql(String table, String[] fields) {
        StringJoiner joiner = new StringJoiner(", ", "SELECT ", " FROM " + table);
        for (String field : fields) {
            joiner.add(field);
        }
        return joiner.toString();
    }
}

  • 1

Reply