Java Blog for Alex Wan

          Let life be beautiful like summer flowers and death like autumn leaves.

          統(tǒng)計(jì)

          留言簿(10)

          BlogJava

          Blogs

          DIV+CSS

          JQuery相關(guān)

          友情鏈接

          常去的地方

          數(shù)據(jù)供應(yīng)

          閱讀排行榜

          評(píng)論排行榜

          tomcat數(shù)據(jù)源讀取的簡(jiǎn)單例子

          應(yīng)朋友的要求寫下這篇文章,實(shí)現(xiàn)一個(gè)簡(jiǎn)單的例子,用于讀取tomcat數(shù)據(jù)源

          BaseDAO.java

          import java.sql.Connection;
          import java.sql.PreparedStatement;
          import java.sql.SQLException;
          import javax.naming.Context;
          import javax.naming.InitialContext;
          import javax.naming.NamingException;
          import javax.sql.DataSource;


          public class BaseDAO
          {
           
          private static DataSource pool = null;
           
          private static Context env = null;
           
          //private Connection conn = null;
           protected String tableName="";
           
          public BaseDAO() throws AppException//構(gòu)造
           {
            
          if (pool != nullreturn;
            
            
          try
            
          {
             env 
          = (Context) new InitialContext().lookup("java:comp/env");
             pool 
          = (DataSource)env.lookup("jdbc/" + "appid");//數(shù)據(jù)源id
            }

            
          catch(NamingException ne) 
            
          {
             env 
          = null;
             pool 
          = null;
             System.out.println(ne.getMessage());
             
          throw new AppException(ne.getMessage());
            }

           }

           
           
          public Connection getConn() throws AppException//獲取連接
           {
            
          try
            
          {
             
          if (pool == null)
              
          throw new AppException("Data source invalid!");
             
          else
              
          return pool.getConnection();
            }

            
          catch(SQLException e) 
            
          {
             
          throw new AppException(e.getMessage());
            }

           }

           
           
          public void closeConn(Connection conn)//關(guān)閉連接
           {
            
          try
            
          {
             
          if (conn != null) conn.close();
            }

            
          catch (Exception e)
            
          {
            }

           }

          }

          另外AppException的實(shí)現(xiàn)如下:
          AppException.java

          import java.lang.Exception;

          public class AppException extends Exception
          {
           
          /**
            * 
            
          */

           
          private static final long serialVersionUID = 1L;

           
          public AppException(Exception exc)
           
          {
            
          super(exc.getCause());
           }

           
           
          public AppException(String errorMessage)
           
          {
            
          super(errorMessage);
           }

          }


           

          其實(shí)這一種方式也不是最好的方式,而且依賴tomcat的數(shù)據(jù)源,開啟了連接后一定要記得關(guān)閉連接,這樣管理起來容易出錯(cuò),建議可以是使用ibatis替代



          Let life be beautiful like summer flowers and death like autumn leaves.

          posted on 2008-06-07 10:21 Alexwan 閱讀(1050) 評(píng)論(0)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 天门市| 南宫市| 大厂| 清丰县| 罗甸县| 乌拉特前旗| 汉中市| 颍上县| 凯里市| 招远市| 南部县| 襄垣县| 万载县| 广昌县| 吉首市| 四会市| 双流县| 越西县| 大英县| 邓州市| 根河市| 和林格尔县| 阿拉善左旗| 棋牌| 青海省| 都江堰市| 星子县| 临城县| 平昌县| 绩溪县| 邮箱| 无锡市| 苍梧县| 平定县| 大邑县| 台北市| 南平市| 阳西县| 定安县| 平南县| 岗巴县|