mardi 24 octobre 2017

Abstract Factory for different DB connections

I have a class which establishes connection to DB and queries for some data.

public QueryProcessor queryProcessor;
public ConnectionDataSource dataSource;

public String driver_type;
public String url;
public String username;
public String password;

public DataBaseHelper(){
        driver_type="oracle.jdbc.OracleDriver (DERIVED FROM SUBCLASS)";
        url="DERIVED FROM SUBCLASS";
        username="DERIVED FROM SUBCLASS";
        password="DERIVED FROM SUBCLASS";
        }

private void connect(){
        dataSource=new ConnectionDataSource();
        dataSource.setDriverClassName(driver_type);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        queryProcessor=new QueryProcessor(dataSource);
        }


public <T> Object query(String SQLQuery){

        connect();
        return queryProcessor.queryForObject(SQLQuery);

}

My task is to make Abstract Factory out of this class in order to support few types of DB connections. (e.g Oracle, MySQL , Postgre) so I can execute queries to all types of DB.

I will appreciate if somebody can post an example of Abstract Factory used for multiple DB connection.

Aucun commentaire:

Enregistrer un commentaire