6
Considering the object orientation, would the use of inheritance and interface in this way be correct? But in this way, any request for connection to the database will need a new object. Would have some way to statically access or would need a new object created when initializing 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;
}
}
}
}
In fact I took a class ready that I used and tried to apply the inheritance without knowing if it would work because I didn’t test it yet, I used everything static, but each class I repeated the attributes and the getConnection() method, I don’t like to keep creating objects all the time, How do you think I could do it. Put only abstract getConnection() and the rest in the specific child classes in the mother class, I will create one for Sqlserver and one for Oracle.. In fact I will create a little financial control software for myself as learning.
– Ulisses Gimenes
I think the way I put it might be a way. Actually I do it very differently from everyone else, but it works for me ,I don’t teach other people to do the same.
– Maniero
I don’t know if it works, but I tried to create several projects, for example. a project of access to the dao where the connection is and all the daos, and a package where the registration screens are. Referencing the project dao in the libraries. but does not find the property file, can tell me why?
– Ulisses Gimenes
Gives this error: Jul 27, 2016 9:30:41 AM Conection.Genericconnection getDbProperties GRAVE: null java.io.Filenotfoundexception: src properties conf.properties (The system cannot find the specified path)
– Ulisses Gimenes
There is some other more specific problem. needs another more specific question.
– Maniero
Make sure it’s right the way I did.. a Oracleconnection https://drive.google.com/file/d/0B91j9PUXTgJscVBUMS1sMlBrZzg/view?usp=sharing / the mother class https://drive.google.com/filed/0B91j9PUXTgJsWDhzd19ma0ptbA/view?usp=sharing
– Ulisses Gimenes
I’m going to create another more specific question.. I wanted to make sure the rest is correct
– Ulisses Gimenes
It looks much better, I still don’t know if it’s ideal, but as I said, I like to do different.
– Maniero