zhyiwww
          用平實(shí)的筆,記錄編程路上的點(diǎn)點(diǎn)滴滴………
          posts - 536,comments - 394,trackbacks - 0
          通常的操作方法
              public List<Category> queryCategory(String queryStr) throws SQLException {
                  ArrayList<Category> catList = new ArrayList<Category>();
                  PreparedStatement pstmt = DBUtil.getConnection().prepareStatement(
                          queryStr);
                  if (pstmt == null) {
                      return catList;
                  } else {
                      ResultSet rst = pstmt.executeQuery();
                      while (rst.next()) {
                          Category c = new Category();
                          c.setCategoryId(rst.getInt("CATEGORY_ID"));
                          c.setCategoryName(rst.getString("CATEGORY_NAME"));
                          c.setCategoryParent(rst.getInt("PARENT_ID"));
                          catList.add(c);
                      }

                      rst.close();
                      pstmt.getConnection().close();

                  }

                  return catList;
              }

          這種方式是不保險(xiǎn)的方式,因?yàn)榇颂幉⒉惶幚懋惓#遣慌懦龝l(fā)生異常。
          假設(shè)在黑體代碼部分發(fā)生異常,那么下面的代碼并不會執(zhí)行,也就是說,rst,pstmt,con都不會關(guān)閉掉。
          所以導(dǎo)致的結(jié)果是這些資源得不到釋放。

          解決的方法,就是在最后用finally去解決問題

              PreparedStatement pstmt = null;
                  ResultSet rst = null;

                  try {
                      pstmt = this.getPstmt(queryStr);
                      rst = pstmt.executeQuery();
                      while (rst.next()) {
                          Category c = new Category();
                          c.setCategoryId(rst.getInt("CATEGORY_ID"));
                          c.setCategoryName(rst.getString("CATEGORY_NAME"));
                          c.setCategoryParent(rst.getInt("PARENT_ID"));
                          catList.add(c);
                      }

                  } catch (SQLException e) {
                      throw e;
                  }finally{
                      if(rst!=null){
                         
                          rst.close();
                      }
                      if(pstmt!=null){
                          pstmt.close();
                         
                      }
                      if(this.getCon()!=null&&this.getCon().getAutoCommit()){
                          this.getCon().close();
                      }
                  }

          這樣,無論在那個(gè)地方發(fā)生了異常,都能讓資源得到釋放。






          |----------------------------------------------------------------------------------------|
                                     版權(quán)聲明  版權(quán)所有 @zhyiwww
                      引用請注明來源 http://www.aygfsteel.com/zhyiwww   
          |----------------------------------------------------------------------------------------|
          posted on 2010-02-09 15:19 zhyiwww 閱讀(660) 評論(0)  編輯  收藏 所屬分類: j2ee
          主站蜘蛛池模板: 常宁市| 清徐县| 云浮市| 北宁市| 三都| 阿合奇县| 巴东县| 通化市| 团风县| 南涧| 宿州市| 武城县| 定安县| 昌图县| 大名县| 花莲县| 合作市| 太白县| 华蓥市| 永平县| 宜昌市| 忻州市| 南开区| 永川市| 定西市| 济阳县| 赞皇县| 英山县| 金塔县| 云霄县| 台北县| 永嘉县| 拉萨市| 普陀区| 黄梅县| 加查县| 苏尼特右旗| 峡江县| 璧山县| 湖南省| 富源县|