小偉

          我的java筆記

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            29 隨筆 :: 1 文章 :: 23 評論 :: 0 Trackbacks


          當我看到大家給我帖子的回復,我很高興!我希望能給大家提醒,在寫程序中時候出現的“漏洞”

          這是致命的“打擊”,那現在我發表一個比較完善的異常處理的程序,以便大家能參考。


          內容如下:
          public class TestJdbc {
           public static void main(String[] args) {
            
            Connection conn = null;
            Statement stmt = null;
            ResultSet rs = null;
            
            try {
             Class.forName("oracle.jdbc.driver.OracleDriver");
             conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xx", "scott", "tiger");
             stmt = conn.createStatement();
             String sql ="select * from dept2";
             rs = stmt.executeQuery(sql);
             while(rs.next()) {
              System.out.println(rs.getString("dname"));
             }
            } catch (ClassNotFoundException e) {
             e.printStackTrace();
            } catch (SQLException e) {
             e.printStackTrace();
            }finally {
             try {
              if(rs !=null){
               rs.close();
               rs = null;
              }
              if(stmt != null) {
               stmt.close();
               stmt = null;
              }
              if(conn != null) {
               conn.close();
               conn = null;
              }
             } catch (SQLException e) {
              e.printStackTrace();
             }
            }
            
           }

          }

          posted on 2007-04-23 15:01 小偉 閱讀(489) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 舞钢市| 新泰市| 登封市| 商都县| 英德市| 城口县| 乐至县| 九寨沟县| 故城县| 册亨县| 武汉市| 遂昌县| 石林| 东方市| 分宜县| 金塔县| 祁阳县| 泗阳县| 台中县| 洪泽县| 高要市| 左贡县| 东乌| 漳平市| 田林县| 尼玛县| 龙胜| 裕民县| 肃宁县| 湘西| 陈巴尔虎旗| 资阳市| 什邡市| 公安县| 井冈山市| 辽中县| 靖州| 长寿区| 婺源县| 成安县| 醴陵市|