關(guān)閉數(shù)據(jù)庫資源的新寫法

          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

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(11)

          隨筆分類(48)

          文章分類(29)

          常去逛逛

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 西和县| 烟台市| 梁平县| 武邑县| 乌兰县| 宾阳县| 嘉义县| 咸丰县| 江孜县| 石景山区| 赤壁市| 张家界市| 江川县| 渭南市| 沙坪坝区| 叶城县| 织金县| 理塘县| 电白县| 垫江县| 永修县| 宣化县| 轮台县| 林州市| 浙江省| 台湾省| 南宫市| 洞口县| 临安市| 昌宁县| 鞍山市| 海口市| 青海省| 齐河县| 昭通市| 崇信县| 鄂托克前旗| 台东市| 阿坝| 民乐县| 瑞丽市|