I am trying to do something in my Application but I am stuck & unable to find a way forward. Here is my problem:-
I want to create Oracle Queries programatically. 2 such queries for e.g. are DROP TABLE tableName & ALTER TABLE tableName PRIMARY KEY (col1, col2, col3….);
In my class i have created these 2 final static Strings :-
private static final String DROP_TABLE = "DROP TABLE %1$s";
createDropStmt() {
System.out.format(DROP_TABLE, tableName);
}
As you can see, this approach will work fine with DROP statements but not with PK’s beacuse I don’t know how may columns are going to be there till runtime. (private static final String PRIMARY_KEY = “ALTER TABLE %1$s PRIMARY KEY {don’t know what to put here}”`)
So, my question is how to do this kind of thing for PK so that my template string can dynamically take values & construct the query.? Does Java have a clean way to do it or my approach should be different?
Thanks
If you have multiple primary key columns then you have to join their names with commas. The
java.util.Formatterdoesn’t offer a way to concatenate arrays or lists of values. You have to do that for yourself.When you don’t want to write your own joiner, you could use the Joiner from the Guava libraries like this:
and then insert this into the ALTER TABLE statement.