Java Reference
In-Depth Information
private IdDescription accountAsIdDesc(ResultSet rs
) throws SQLException {
return new IdDescription(
new Integer(rs.getInt("id")),
rs.getString("description"));
}
Because there are several places where we need to perform these tasks, building
methods to simplify the mapping saves us some errors and time when creating our
real methods later.
The next method creates our PreparedStatement for the “query-by-example”
methods in the DAO interface. This is where we have the most complexity, and it's
where the SQL Map implementation starts to look much more attractive. While
the other helper methods were simple but tedious, this one is tedious, error
prone, and difficult to test—not a good combination to have to write:
private PreparedStatement prepareQBEStatement(
Account account,
Connection connection,
PreparedStatement ps,
String baseSql
) throws SQLException {
StringBuffer sqlBase = new StringBuffer(baseSql);
StringBuffer sqlWhere = new StringBuffer("");
List<Object> params = new ArrayList<Object>();
String city = account.getCity();
if (!nullOrEmpty(city)) {
sqlWhere.append(" city like ?");
params.add(account.getCity());
}
Integer accountId = account.getAccountId();
if (!nullOrZero(accountId)) {
if sqlWhere.length() > 0) {
sqlWhere.append(" and");
}
sqlWhere.append(" accountId = ?");
params.add(account.getAccountId());
}
if (sqlWhere.length() > 0) {
sqlWhere.insert(0, " where");
sqlBase.append(sqlWhere);
}
ps = connection.prepareStatement(sqlBase.toString());
for (int i = 0; i < params.size(); i++) {
Search WWH ::




Custom Search