Discuss / Java / 练习:

练习:

Topic source

ypx0410

#1 Created at ... [Delete] [Delete and Lock User]
public static String buildInsertSql(String table, String[] fields) 
	{
		var sql = new StringBuilder(128);

		sql.append("INSERT INTO ").append(table).append(" (");
		var values = new StringBuilder(128);
		values.append(" (");
		
		for(int i = 0; i<fields.length; i++) 
		{
			String s = i == 0 ? "" : " ";
			String e = i+1 == fields.length ? ")" : ",";
			values.append(s).append("?").append(e);
			sql.append(s).append(fields[i]).append(e);	
		}
		sql.append(" VALUES").append(values);
		
		return sql.toString();
	}

  • 1

Reply