小偉

          我的java筆記

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            29 隨筆 :: 1 文章 :: 23 評(píng)論 :: 0 Trackbacks


          當(dāng)我看到大家給我帖子的回復(fù),我很高興!我希望能給大家提醒,在寫(xiě)程序中時(shí)候出現(xiàn)的“漏洞”

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


          內(nèi)容如下:
          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) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 陆河县| 怀集县| 社旗县| 威信县| 新和县| 兴隆县| 西藏| 合阳县| 壤塘县| 新郑市| 翼城县| 镇雄县| 宁国市| 晋城| 德格县| 图们市| 枞阳县| 库车县| 巨野县| 略阳县| 兰西县| 奉节县| 澳门| 灵台县| 重庆市| 鄂伦春自治旗| 宁国市| 三门峡市| 明水县| 奉贤区| 上栗县| 上饶市| 宜良县| 双江| 修文县| 平昌县| 宣汉县| 新宁县| 黄浦区| 抚宁县| 东莞市|