Considering object orientation, would the use of inheritance and interface in this way be correct? But in this way, any request to connect to the database will require a new object. Is there a way to statically access or need a new object created when booting the system using Singleton?
Connection Interface
public interface ConnectionDB {
public Connection getConnection();
}
GenericConnection class
public class GenericConnection {
protected static Properties dbProperties = new Properties();
public static Properties getDbProperties() {
if(dbProperties == null){
try {
dbProperties.load(new FileInputStream("src/properties/conf.properties"));
} catch (FileNotFoundException ex) {
Logger.getLogger(GenericConnection.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(GenericConnection.class.getName()).log(Level.SEVERE, null, ex);
}
}
return dbProperties;
}
public static void setDbProperties(Properties dbProperties) {
GenericConnection.dbProperties = dbProperties;
}
}
OracleConnection Class
public class OracleConnection extends GenericConnection implements ConnectionDB {
private Connection conn;
private Statement st;
public Statement getSt() {
return st;
}
public void setSt(Statement st) {
this.st = st;
}
public Connection getConn() {
return conn;
}
public void setConn(Connection conn) {
this.conn = conn;
}
@Override
public Connection getConnection() {
if (getConn() != null) {
return getConn();
} else {
try {
String url = "jdbc:oracle:thin:@" + getDbProperties().getProperty("ServerOracle") + ":" + getDbProperties().getProperty("portOracle") + ":" + getDbProperties().getProperty("sidOracle");
setConn(DriverManager.getConnection(url, getDbProperties().getProperty("userOracle"), getDbProperties().getProperty("passwdOracle")));
setSt(getConn().createStatement());
return getConn();
} catch (SQLException ex) {
Logger.getLogger(OracleConnection.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
}
}