I am working with JDBC driver and my problem is storing SQL queries in a good place. The point is that it will be making a large number of queries.
Statement s = conn.createStatement ();
s.executeUpdate ("DROP TABLE IF EXISTS animal");
s.executeUpdate (
"CREATE TABLE animal ("
+ "id INT UNSIGNED NOT NULL AUTO_INCREMENT,"
+ "PRIMARY KEY (id),"
+ "name CHAR(40), category CHAR(40))");`
Change to this…
Statement s = conn.createStatement ();
s.executeUpdate (StaticVariables.getDropTableAnimalQuery());
s.executeUpdate (StaticVariables.getCreateTableAnimalQuery());`
… and create special class with static variables
public class StaticVariables {
private static String dropTableAnimalQuery = "DROP TABLE IF EXISTS animal";
private static String createTableAnimalQuery = "CREATE TABLE animal ("
+ "id INT UNSIGNED NOT NULL AUTO_INCREMENT,"
+ "PRIMARY KEY (id),"
+ "name CHAR(40), category CHAR(40))";
public static String getDropTableAnimalQuery() {
return dropTableAnimalQuery;
}
public static String getCreateTableAnimalQuery() {
return createTableAnimalQuery;
}
}
Maybe someone has a better way to solve this
I hate that idiom of putting static constants in an interface like your
StaticVariableexample.I prefer to keep constants in the class that uses them to the greatest degree possible. I’ll add constants to an interface if many sub-classes that implement it need them, but there will be methods that are implemented as well.