I have a DAO class with many methods that have a lot of repeated code along the lines of: -
public void method1(...) {
Connection conn = null;
try {
Create an interface for ex. Executable:
public interface Executable() {
void exec() throws SqlException();
}
Refactor each of your dao method to :
public void method1() {
execute(new Executable() {
@Override
public void exec() throws SqlException() {
// your code here
}
});
}
Create the following method execute in your DAO:
private void execute(Executor ex) {
try {
ex.exec();
} catch(...) {
...
} finally {
...
}
}