Java Reference
In-Depth Information
ps.setObject(i+1, params.get(i));
}
return ps;
}
Now that we have all of the helper methods in place, we can start to build the pub-
lic interface. The insert method is the most involved, because it requires both a
query and an insert:
public Integer insert(Account account) {
Connection connection = this.getConnection();
Statement statement = null;
PreparedStatement ps = null;
ResultSet rs = null;
Integer key = null;
if (null != connection) {
try{
statement = connection.createStatement();
rs = statement.executeQuery(sqlGetSequenceId);
if (rs.next()) {
key = new Integer(rs.getInt(1));
account.setAccountId(key);
if (log.isDebugEnabled()) {
log.debug("Key for inserted record is " + key);
}
}
ps = connection.prepareStatement(sqlInsert);
int i = 1;
ps.setObject(i++, account.getAccountId());
ps.setObject(i++, account.getUsername());
ps.setObject(i++, account.getPassword());
ps.setObject(i++, account.getFirstName());
ps.setObject(i++, account.getLastName());
ps.setObject(i++, account.getAddress1());
ps.setObject(i++, account.getAddress2());
ps.setObject(i++, account.getCity());
ps.setObject(i++, account.getState());
ps.setObject(i++, account.getPostalCode());
ps.setObject(i++, account.getCountry());
ps.executeUpdate();
} catch (SQLException e) {
log.error("Error inserting data", e);
throw new DaoException(e);
} finally {
closeStatement(ps);
closeResources(statement, rs);
}
}
return key;
}
Search WWH ::




Custom Search