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.
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 asformatis concerned.What this piece of code does is simply substituting the content of
tabNamefor the first%sand the result offor the second one, hence creating something along the lines of
for whatever value
tabNamehas.