小偉

          我的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)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 安阳县| 镇巴县| 东港市| 彰武县| 金华市| 县级市| 景谷| 乌恰县| 辽中县| 元氏县| 修文县| 含山县| 南丰县| 托克托县| 堆龙德庆县| 永仁县| 台安县| 蒲城县| 滦南县| 方城县| 平阳县| 中江县| 广南县| 山西省| 安远县| 镇雄县| 平乡县| 闻喜县| 如皋市| 浙江省| 电白县| 合江县| 靖西县| 禹城市| 淮滨县| 右玉县| 千阳县| 应城市| 文安县| 大足县| 多伦县|