Java Reference
In-Depth Information
} catch (SQLException e) {
log.error("Exception closing statement", e);
returnValue = false;
}
}
return returnValue;
}
Because closing a Statement can throw a SQLException , this method closes it and
wraps the SQLException as a DaoException . So instead of the code above, we sim-
ply call closeStatement() and it logs the original exception and throws the DaoEx-
ception . A similar method is used to close ResultSet objects for the same reasons.
The next shortcuts we build are ways to extract our data structures from a
ResultSet object:
private Account extractAccount(ResultSet rs
) throws SQLException {
Account accountToAdd = new Account();
accountToAdd.setAccountId(rs.getInt("accountId"));
accountToAdd.setAddress1(rs.getString("address1"));
accountToAdd.setAddress2(rs.getString("address2"));
accountToAdd.setCity(rs.getString("city"));
accountToAdd.setCountry(rs.getString("country"));
accountToAdd.setFirstName(rs.getString("firstname"));
accountToAdd.setLastName(rs.getString("lastname"));
accountToAdd.setPassword(rs.getString("password"));
accountToAdd.setPostalCode(rs.getString("postalcode"));
accountToAdd.setState(rs.getString("state"));
accountToAdd.setUsername(rs.getString("username"));
return accountToAdd;
}
private Map<String, Object> accountAsMap(ResultSet rs
) throws SQLException {
Map<String, Object> acct =
new HashMap<String, Object>();
acct.put("accountId", rs.getInt("accountId"));
acct.put("address1", rs.getString("address1"));
acct.put("address2", rs.getString("address2"));
acct.put("city", rs.getString("city"));
acct.put("country", rs.getString("country"));
acct.put("firstName", rs.getString("firstname"));
acct.put("lastName", rs.getString("lastname"));
acct.put("password", rs.getString("password"));
acct.put("postalCode", rs.getString("postalcode"));
acct.put("state", rs.getString("state"));
acct.put("username", rs.getString("username"));
return acct;
}
Search WWH ::




Custom Search