Discuss / Java / HelloCao

HelloCao

Topic source

#1 Created at ... [Delete] [Delete and Lock User]

package Qiantao;

public class StringBu {

    public static void main(String[] args) {

        String[] fields = { "name", "pos", "sal" };

        String table = "employee";

        String insert = buildInsertSql(table, fields);

        System.out.println(insert);

        String s = "INSERT INTO employee (name, pos, sal) VALUES (?, ?, ?)";

        System.out.println(s.equals(insert) ? "success" : "fail");

    }

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

        // 

       var s = new StringBuilder(1024);

           s.append("INSERT INTO ")

           .append(table)

           .append(" (")

           ;

           for (String f:fields) {

           s.append(f).append(", ");

           }

          s.deleteCharAt(s.lastIndexOf(","));

         s.deleteCharAt(s.lastIndexOf(" "));

          s.append(") VALUES (?, ?, ?)");  

           /*

             .append("(name, position, salary) VALUES (?, ?, ?)")

             .append("salary")

             .insert(0, "employee");

           */

        return s.toString();


  • 1

Reply