随笔 - 91,  文章 - 0,  评论 - 3,  阅读 - 47581

请使用StringBuilder构造一个INSERT语句:

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拼接字符串
        StringBuilder s=new StringBuilder();
        s.append("INSERT INTO ");
        s.append(table);
        s.append(" (");
        for (int i = 0; i < fields.length; i++) {
            if (i < fields.length - 1) {
                s.append( fields[i] + ", " );
            } else {
                s.append( fields[i] );
            }
        }
        s.append(") VALUES (?, ?, ?)");
        return s.toString();
    }
}

还可以使用StringJoiner,可以发现使用StringJoiner后代码简化了很多
语法:StringJoiner s=new StringJoiner(间隔符,指定开头字符串(可省),指定结尾字符串(可省))

import java.util.StringJoiner;
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:使用StringJoiner拼接字符串
        StringJoiner sj = new StringJoiner(", ","INSERT INTO "+table+" (",") VALUES (?, ?, ?)");
        for (String field : fields) {
            sj.add(field);
        }
        return sj.toString();
    }
}

String.join():String还提供了一个静态方法join(),这个方法在内部使用了StringJoiner来拼接字符串,在不需要指定“开头”和“结尾”的时候,用String.join()更方便:

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:使用String.join和StringBuilder拼接字符串
        String s = String.join(", ", fields);
        StringBuilder sd = new StringBuilder();
        sd.append("INSERT INTO "+table+" (");
        sd.append(s);
        sd.append(") VALUES (?, ?, ?)");
        return sd.toString();
    }
}
posted on   飞飞乐园  阅读(403)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示