關閉數據庫資源的新寫法

          import java.sql.Connection;
          import java.sql.ResultSet;
          import java.sql.SQLException;
          import java.sql.Statement;
          import javax.sql.DataSource;
          import org.springframework.context.ApplicationContext;
          import org.springframework.context.support.ClassPathXmlApplicationContext;


          public class SpringDataSourceTest {

              
          /**
               * 
          @param args
               
          */
              
          public static void main(String[] args) {
                  
          // TODO Auto-generated method stub
                  ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
                  DataSource dataSource
          =(DataSource)ctx.getBean("dataSource");
                  Connection con
          =null;
                  Statement stmt
          =null;
                  ResultSet res
          =null;
                  
          try{
                      con
          =dataSource.getConnection();
                      stmt
          =con.createStatement();
                      res
          =stmt.executeQuery("select * from student");
                      
          while(res.next()){
                          System.out.println(res.getInt(
          "id"));
                          System.out.println(res.getString(
          "username"));
                      }
                  }
                  
          catch(SQLException e){
                      e.printStackTrace();
                  }
                  
          finally{
                      
          /*
                      if(res!=null){
                          try {
                              res.close();
                          } catch (SQLException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                          }
                      }
                      if(stmt!=null){
                          try {
                              stmt.close();
                          } catch (SQLException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                          }
                      }
                      if(con!=null){
                          try {
                              con.close();
                          } catch (SQLException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                          }
                      }
          */
                      closeJdbcResource(res);
                      closeJdbcResource(stmt);
                      closeJdbcResource(con);
                      
                  }
              }
              
          public static void closeJdbcResource(Object resource){
                  Class clazz
          =resource.getClass();
                  
          try {
                      java.lang.reflect.Method method
          =clazz.getMethod("close"null);
                      method.invoke(resource, 
          null);
                  } 
          catch (Exception e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
              }
          }
          注釋掉的是以前的寫法。

          posted on 2008-01-17 10:49 我為J狂 閱讀(327) 評論(0)  編輯  收藏 所屬分類: DataBase

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導航

          統計

          常用鏈接

          留言簿(11)

          隨筆分類(48)

          文章分類(29)

          常去逛逛

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 平顺县| 青冈县| 开阳县| 涿州市| 类乌齐县| 荥经县| 石河子市| 霍州市| 江山市| 邵武市| 建宁县| 江门市| 五大连池市| 上犹县| 含山县| 山西省| 临江市| 张北县| 临漳县| 扶绥县| 肥东县| 宁陕县| 万安县| 定兴县| 大新县| 涿鹿县| 招远市| 忻州市| 桃源县| 永年县| 栾川县| 玉溪市| 盖州市| 滨海县| 南溪县| 临泽县| 黔南| 中西区| 东丽区| 藁城市| 中宁县|