I have configured Oracle and Postgres databases in the code. Since oracle and Postgres have their differences in setting boolean values and others. I want to configure 2 different classes(PostgresSetter and OracleSetter) that set values according to DB type. At the start of the run time, I want to inject the database's setter class according to the driver type. Currently, I comment out the unused database but I want to do that programmatically.
public abstract class DBSetter<T> /*implements DBSetter this interface added later*/{
public static void setParameter(PreparedStatement statement, int index, Boolean data)
throws Exception {
if (data != null) {
statement.setBoolean(index, data);
} else {
//if psql
statement.setNull(index, Types.BOOLEAN);
//if oracle
statement.setNull(index, Types.INTEGER);
}
...//other functions
}
}
Above is the class that sets the boolean value to the prepared statement. I want to split that into 2 as below.
public interface DBSetter {
static void setParameter(PreparedStatement statement, int index, Boolean data)
throws Exception {}
}
public class OracleSetter implements DBSetter {
public static void setParameter(PreparedStatement statement, int index, Boolean data)
throws Exception {
if (data != null) {
statement.setBoolean(index, data);
} else {
statement.setNull(index, Types.INTEGER);
}
}
}
public class PostgresSetter implements DBSetter {
public static void setParameter(PreparedStatement statement, int index, Boolean data)
throws Exception {
if (data != null) {
statement.setBoolean(index, data);
} else {
statement.setNull(index, Types.BOOLEAN);
}
}
}
I created an interface and created 2 new classes called OracleSetter and PostgreSetter and implemented the interface. Still, the function setParameter is not recognized. How can I inject Setters at the startup according to an if clause?