I'm writing database client using sql2o library. There are a lot of data access objects in my project, so I supposed to have lot of similar functions for getting access to the tables. For example, for class Persons
.
public List<Persons> getPersonsData(){
String sql = "SELECT * FROM " + PERSONS;
try(org.sql2o.Connection con = sql2o.open()) {
return con.createQuery(sql).executeAndFetch(Persons.class);
}
}
Are there any ways to optimize it? I was thinking about using generics, but as I know there no ways to get instance of generic class. Is it really impossible to create something like this?
public class Getter<T> {
public List<T> getGenericClass(){
String sql = "SELECT * FROM " + T.tableName;
try(org.sql2o.Connection con = sql2o.open()) {
return con.createQuery(sql).executeAndFetch(T.class);
}
}
}