请查看我下面的DAO层代码。
public List<Channel> getListOfChannels() throws DataAccessException {
// will have the channel in form of List
// to hold the channels list
List<Channel> listChannels = null;
Connection conn = null;
Statement statement = null;
ResultSet resultSet = null;
try {
// get the db connection from pool
// this is DBCP lib on top doing this
conn = ManageConnections.getConnection();
statement = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
final String QUERY_STRING = "Select * from channel";
resultSet = statement.executeQuery(QUERY_STRING);
// is this good practice to put this ?
if (isResultSetEmptyOrNull(resultSet)) {
throw new DataAccessException(
"No more data of Channels found from db");
}
Channel channel = null;
listChannels = new ArrayList<Channel>();
while (resultSet.next()) {
// get the object from the result set and
listChannels.add(channel);
}
log.debug("getListOfChannels Got the list of channels "
+ listChannels);
} catch (SQLException ex) {
throw new DataAccessException(SQL_EXCEPTION + ex, ex);
} catch (DBConnectionException ex) {
// re thrown, no logging
throw new DataAccessException(ex);
} catch (Exception ex) {
// Generic exception thrown , now throw Custom Exceptionn
throw new DataAccessException(GENERIC_EXCEPTION + ex, ex);
} finally {
try {
if (conn != null) {
// this is DBCP lib on top doing this
ManageConnections.close(conn);
}
if (statement != null) {
statement.close();
}
if (resultSet != null) {
resultSet.close();
}
} catch (SQLException ex) {
throw new DataAccessException(
"Exception while closing resource" + ex, ex);
} catch (DBConnectionException ex) {
throw new DataAccessException(
"Exception while closing resource" + ex, ex);
}
}
return listChannels;发布于 2013-03-08 10:37:09
在这里可以做一些改进:
https://codereview.stackexchange.com/questions/23609
复制相似问题