首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DAO层代码检查

DAO层代码检查
EN

Code Review用户
提问于 2013-03-08 07:09:35
回答 1查看 557关注 0票数 3

请查看我下面的DAO层代码。

代码语言:javascript
复制
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;
EN

回答 1

Code Review用户

发布于 2013-03-08 10:37:09

在这里可以做一些改进:

  • a)你的QUERY_STRING是一根神奇的绳子。将它移到另一个地方,因为它会使您的代码难以维护。
  • 用户链接列表而不是数组列表。在最坏情况下插入时有O(1),而数组列表有O(n)
  • 不要在最后块中重新抛出异常。您将丢失原始异常的所有细节。
  • 每次将字符串与"+“连接时,都会使用内存执行太多操作,因为字符串是不可变的。使用*.format代替
票数 3
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/23609

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档