public class UsersDAO { private Connection con; public UsersDAO(Connection con){ this.con = con; } public UsersDataSet get(long id) throws SQLException{ TExecutor exec = new TExecutor(); return exec.execQuery(con, "select * from users where id=" + id, new TResultHandler<UsersDataSet>(){ public UsersDataSet handle(ResultSet result) throws SQLException { result.next(); return new UsersDataSet(result.getLong(1), result.getString(2)); } }); } }
public class TExecutor { public <T> T execQuery(Connection connection, String query, TResultHandler<T> handler) throws SQLException { Statement stmt = connection.createStatement(); stmt.execute(query); ResultSet result = stmt.getResultSet(); T value = handler.handle(result); result.close(); stmt.close(); return value; } }