I am trying to create a MySql create table statement in java. However the challenge i am facing is that my table's column name are stored in the String Array entered by the user. It is straight forward to run if i know the column names but this time i am trying to create a table on the fly based on the data i receive in my String array
How do i add it dynamically to the create table statement ? any help/tips ?
The create table below is a static one.
static String[] StopNamesForDB;
Set<String> set = new LinkedHashSet<String>(StopNames);
StopNamesForDB = new String[set.size()];
for (String string : set) {
StopNamesForDB[i] = string;
i++;
}
String sql = "CREATE TABLE Route3 "+
"(id INTEGER not NULL, " +
" first VARCHAR(255), " +
" last VARCHAR(255), " +
" age INTEGER, " +
" PRIMARY KEY ( id ))";
stmt.executeUpdate(sql);
StringBuilder sql = new StringBuilder();
sql.append("create table route3(");
for(String name : StopNamesForDB)
{
sql.append(name).append(",");
}
sql.append("PRIMARY KEY (").append(StopNamesForDB[0]).append(")");
sql.append(")");
stmt.executeUpdate(sql.toString());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.