public List list() throws SQLException,
        javax.naming.NamingException {
        String sql;
        sql =
            "select cy.cyno,cy.cname,cy.prvcode from city cy  order by cy.cname";
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List list = new ArrayList();
        City city = null;
        try {
            conn = getConnection();
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                city = new City();
                populate(city, rs);//struts反射机制
                list.add(city);
            }            close(rs);
            close(pstmt);        }
        catch (SQLException sqle) {
            System.out.println("SQLException" + sqle.getMessage());
            close(rs);
            close(pstmt);            sqle.printStackTrace();            throw sqle;        }
        finally {
            close(conn);        }
   
        return list;
    }//--------------------------------------------------while (rs.next()) {
                city = new City();
                populate(city, rs);//struts反射机制
                list.add(city);
            }
//-------------------------------------------------
这段代码可能出现内存映射吗

解决方案 »

  1.   

    错了,应该是:
    //--------------------------------------------------while (rs.next()) {
                    city = new City();
                    populate(city, rs);//struts反射机制
                    list.add(city);
                }
    //-------------------------------------------------
    这段代码可能出现内存泄漏吗
      

  2.   

    我觉得可能是 rs 搞的鬼。 别把rs 当作参数。你试试
      

  3.   

    pstmt = conn.prepareStatement(sql);
                rs = pstmt.executeQuery();
                while (rs.next()) {
                    city = new City();
                    populate(city, rs);//struts反射机制
                    list.add(city);
                }如果上面这段代码执行的过程中出错(如populate(city, rs)),产生了异常,那么后面的:
    close(rs);
    close(pstmt);就不会执行,这样就会造成数据库相关资源没有关闭,有可能会影响后面的数据库操作,会不会有内存泄漏我不能确定
      

  4.   

    另外:
    catch (SQLException sqle) {
                System.out.println("SQLException" + sqle.getMessage());
                close(rs);
                close(pstmt);            sqle.printStackTrace();            throw sqle;        }
    函数:
     close(rs);
    close(pstmt);
    是不是也有可能会产生异常呢,是不是也会影响程序的运行