heting

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            40 隨筆 :: 9 文章 :: 45 評論 :: 0 Trackbacks
          public class Pools extends HttpServlet {
           
           
          private static final long serialVersionUID = 1L;
           
           
          public static BasicDataSource bds = new BasicDataSource();

           
          public static ConnectionFactory fac = null;

           
          public synchronized static Connection getDSConn() {
            Pools aa
          =new Pools();
            
          try {
             aa.init();
            }
           catch (ServletException e1) {
             e1.printStackTrace();
            }

            Connection conn 
          = null;
            
          try {
             conn 
          = fac.createConnection();
            }
           catch (Exception e) {
             System.out.println(
          "fac.createConnection()"+e.toString());
            }

            
          return conn;
           }


           
          public void init(ServletConfig conf) throws ServletException {
           
          //super.init(conf);
            try {
             
             bds.setDriverClassName(
          "com.microsoft.jdbc.sqlserver.SQLServerDriver");
             bds.setUrl(
          "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=dbname");

             bds.setUsername(
          "sa");  

             bds.setPassword(
          "1234"); 

             bds.setInitialSize(
          100); 
             bds.setMaxActive(
          2);     
             bds.setMaxIdle(
          10); 

             bds.setMaxWait(
          1000 * 60); 

             fac 
          = new DataSourceConnectionFactory(bds);
             System.out.println(
          ">>>>init success>>>>");
            }
           catch (Exception e) {
             e.printStackTrace();
            }

           }


           
          public void destory() throws SQLException {
            bds.close();
            bds
          =null;
            fac
          =null;
           }


          }



          在web.xml中添加Pools配置信息

           

          <servlet>
            
          <servlet-name>Pools</servlet-name>
            
          <servlet-class>dbconn.Pools</servlet-class>
            
          <load-on-startup>1</load-on-startup>
           
          </servlet>

          可以將配置連接池的一些參數(shù)寫在web.xml中,實(shí)現(xiàn)可配置的連接池

          posted on 2009-03-04 11:09 賀挺 閱讀(182) 評論(0)  編輯  收藏

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 盘锦市| 江都市| 封丘县| 额济纳旗| 海南省| 张家口市| 富平县| 内黄县| 莆田市| 汉川市| 淅川县| 光山县| 庆安县| 永寿县| 台前县| 精河县| 咸丰县| 台北县| 沁阳市| 岗巴县| 吉安县| 革吉县| 太谷县| 宜良县| 彰武县| 永新县| 兴海县| 建瓯市| 三台县| 吴旗县| 温宿县| 视频| 泰兴市| 赣榆县| 建宁县| 凌源市| 新绛县| 城市| 洛隆县| 花垣县| 明光市|