Earlier I was discussing with some colleagues the most suitable way to create a DAO in Java. Some argue that a DAO should only create, prepare bind and return the statements, which in turn should be executed by other classes, whereas others argue that the statements should be executed by DAOs. I was wondering if there is a best practice regarding this matter.
More specifically,
Option 1, DAO only constructs the statement to be executed later:
public class MyDAO {
public static final String SELECT_ALL = "SELECT * FROM my_table WHERE column= ?";
public static Statement getAll(String param) {
BoundStatement boundStatement = new BoundStatement(MySession.getInstance().prepareStmt(SELECT_ALL);
boundStatement.bind(param);
return boundStatement;
}
}
Option 2, DAO also executes the statement and returns the result set:
public class MyDAO {
public static final String SELECT_ALL = "SELECT * FROM my_table WHERE column= ?";
public static ResultSet getAll(String param) {
BoundStatement boundStatement = new BoundStatement(MySession.getInstance().prepareStmt(SELECT_ALL);
boundStatement.bind(param);
return MySession.getInstance().execute(boundStatement);
}
}
i) Is Option 1 more correct than option 2?; ii) the other way around?; iii) Is there an Option 3 more correct than 1 and 2?; iv) all the same?
Thank you for your time.