-1

I have a function that writes a sql query

I can't figure out what does the expression %s(%s) do in this function

public void createTable(String tabName) throws SQLException {
    String sql = "create table if not exists %s(%s)";
    sql = String.format(sql,
                        tabName,
                        String.format("%s,%s,%s,%s,%s,%s,%s",
                        "id int primary key auto_increment",
                        "surname varchar(50)",
                        "name varchar(50)",
                        "age int(3)",
                        "course int(1)",
                        "group varchar(50)",
                        "stateFunded int(1)"
                ));
    stat.execute(sql);
    System.out.println(sql);
}

I've tried to search, but there is no information about it in Google.

Nikita28
  • 1
  • 2
  • `%s` is a formatting placeholder for a string. – Unmitigated Mar 13 '23 at 14:36
  • See the documentation of [`String.format(...)`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html#format(java.lang.String,java.lang.Object...)) which refers to the documentation about [format strings](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Formatter.html#syntax). – Jesper Mar 13 '23 at 14:56
  • When in doubt, read the javadoc: https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Formatter.html#syntax – jhamon Mar 13 '23 at 14:56
  • 2
    terrible code.... using two format strings as posted is good to confuse any developer (+ `%s` used for literals) – user16320675 Mar 13 '23 at 15:41

1 Answers1

1

By itself %s(%s) is simply two string placeholder one after the other, with the second being surrounded by parentheses that have no syntactic meaning as far as format is concerned.

What this piece of code does is simply substituting the content of tabName for the first %s and the result of

String.format("%s,%s,%s,%s,%s,%s,%s",
                    "id int primary key auto_increment",
                    "surname varchar(50)",
                    "name varchar(50)",
                    "age int(3)",
                    "course int(1)",
                    "group varchar(50)",
                    "stateFunded int(1)")

for the second one, hence creating something along the lines of

create table if not exists tabName(
    id int primary key auto_increment,
    surname varchar(50),
    name varchar(50),
    age int(3),
    course int(1),
    group varchar(50),
    stateFunded int(1)
)

for whatever value tabName has.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • 3
    Obligatory comment about using string formatting to build SQL: https://xkcd.com/327/ – Andy Turner Mar 13 '23 at 16:18
  • 2
    Of course, just writing all those fragments into a single string or using the good old `+` operator would not only make it obvious what’s going on, it would also keep all the compile-time constants constant, instead of performing unnecessary runtime operations. – Holger Mar 13 '23 at 16:53