本站首页    管理页面    写新日志    退出


«September 2025»
123456
78910111213
14151617181920
21222324252627
282930


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7619689
建立时间:2006年5月29日




[Apache(jakarta)]apache dbcp数据库连接池的使用
软件技术

lhwork 发表于 2006/7/18 11:05:21

  1 500)this.width=500'> 500)this.width=500'> public   class  DaoUtil  500)this.width=500'> {   2 500)this.width=500'>   3 500)this.width=500'> 500)this.width=500'>     /** */ /**   4 500)this.width=500'>     * 数据库连接池   5 500)this.width=500'>     *    6 500)this.width=500'>     *  @see   http://jakarta.apache.org/commons/dbcp/index.html   7 500)this.width=500'>      */   8 500)this.width=500'>     private   static  PoolingDriver driver  =   null ;   9 500)this.width=500'>  10 500)this.width=500'> 500)this.width=500'>     /** */ /**  11 500)this.width=500'>     * 设置一个数据库连接池  12 500)this.width=500'>     *   13 500)this.width=500'>     *  @param  name  14 500)this.width=500'>     *            连接池的名称  15 500)this.width=500'>     *  @param  url  16 500)this.width=500'>     *            数据源  17 500)this.width=500'>     *  @throws  SQLException  18 500)this.width=500'>      */  19 500)this.width=500'>     private   static   void  setUpDriverPool(String name, String url)  20 500)this.width=500'> 500)this.width=500'>             throws  SQLException  500)this.width=500'> {  21 500)this.width=500'> 500)this.width=500'>         if  ((driver  ==   null )  ||  driver.getPoolNames().length  <   2 )  500)this.width=500'> {  22 500)this.width=500'> 500)this.width=500'>             try   500)this.width=500'> {  23 500)this.width=500'> 500)this.width=500'>                 /** */ /**  24 500)this.width=500'>                 * 首先创建一个对象池来保存数据库连接 使用 commons.pool 的 GenericObjectPool对象  25 500)this.width=500'>                  */  26 500)this.width=500'>                ObjectPool connectionPool  =   new  GenericObjectPool();  27 500)this.width=500'> 500)this.width=500'>                 /** */ /**  28 500)this.width=500'>                 * 创建一个 DriverManagerConnectionFactory对象 连接池将用它来获取一个连接  29 500)this.width=500'>                  */  30 500)this.width=500'>                ConnectionFactory connectionFactory  =   new  DriverManagerConnectionFactory(  31 500)this.width=500'>                        url,  null );  32 500)this.width=500'> 500)this.width=500'>                 /** */ /**  33 500)this.width=500'>                 * 创建一个PoolableConnectionFactory 对象。  34 500)this.width=500'>                  */  35 500)this.width=500'>                PoolableConnectionFactory poolableConnectionFactory  =   new  PoolableConnectionFactory(  36 500)this.width=500'>                        connectionFactory, connectionPool,  null ,  null ,  false ,  37 500)this.width=500'>                         true );  38 500)this.width=500'> 500)this.width=500'>                 /** */ /**  39 500)this.width=500'>                 * 注册PoolingDriver。  40 500)this.width=500'>                  */  41 500)this.width=500'>                Class.forName( " org.apache.commons.dbcp.PoolingDriver " );  42 500)this.width=500'>                driver  =  (PoolingDriver) DriverManager.getDriver( " jdbc:apache:commons:dbcp: " );  43 500)this.width=500'>                driver.registerPool(name, connectionPool);  44 500)this.width=500'> 500)this.width=500'>            }   catch  (ClassNotFoundException e)  500)this.width=500'> {  45 500)this.width=500'>                 throw   new  RuntimeException(e);  46 500)this.width=500'>            }  47 500)this.width=500'>        }  48 500)this.width=500'>    }  49 500)this.width=500'>  50 500)this.width=500'> 500)this.width=500'>     /** */ /**  51 500)this.width=500'>     * 关闭所有数据库连接池  52 500)this.width=500'>     *   53 500)this.width=500'>      */  54 500)this.width=500'> 500)this.width=500'>     public   static   void  shutDownDriver()  500)this.width=500'> {  55 500)this.width=500'>  56 500)this.width=500'> 500)this.width=500'>         try   500)this.width=500'> {  57 500)this.width=500'>            PoolingDriver driver  =  (PoolingDriver) DriverManager  58 500)this.width=500'>                    .getDriver( " jdbc:apache:commons:dbcp: " );  59 500)this.width=500'>            String[] poolNames  =  driver.getPoolNames();  60 500)this.width=500'> 500)this.width=500'>             if (poolNames.length  >   1 ) 500)this.width=500'> {  61 500)this.width=500'> 500)this.width=500'>                 for  ( int  i  =   0 ; i  <  poolNames.length; i ++ )  500)this.width=500'> {  62 500)this.width=500'>                    driver.closePool( " pool " );  63 500)this.width=500'>                }  64 500)this.width=500'>            }  65 500)this.width=500'> 500)this.width=500'>        }   catch  (SQLException sqle)  500)this.width=500'> {  66 500)this.width=500'>             throw   new  RuntimeException(sqle);  67 500)this.width=500'>        }  68 500)this.width=500'>    }  69 500)this.width=500'>  70 500)this.width=500'> 500)this.width=500'>     /** */ /**  71 500)this.width=500'>     * 取得一个数据库连接对象。  72 500)this.width=500'>     *   73 500)this.width=500'>     * 因为可能使用两个不同的数据库, 所以依据report的值来确定使用那个数据库。  74 500)this.width=500'>     *   75 500)this.width=500'>     *  @param  report  76 500)this.width=500'>     *  @return  77 500)this.width=500'>      */  78 500)this.width=500'> 500)this.width=500'>     public   static  Connection getConnection()  500)this.width=500'> {  79 500)this.width=500'>        Connection con  =   null ;  80 500)this.width=500'> 500)this.width=500'>         try   500)this.width=500'> {  81 500)this.width=500'>            ReadConfig readConfig  =   new  ReadConfig();  82 500)this.width=500'>            readConfig.init( null );  83 500)this.width=500'>             //  装载mysql的jdbc驱动  84 500)this.width=500'>             String driver  =  readConfig.getDBDriver();  85 500)this.width=500'>            String url  =  readConfig.getDBUrl();  86 500)this.width=500'>            String poolName  =   " pool " ;  87 500)this.width=500'>            Class.forName(driver);  88 500)this.width=500'>            setUpDriverPool(poolName, url);  89 500)this.width=500'>            con  =  DriverManager.getConnection( " jdbc:apache:commons:dbcp: "  90 500)this.width=500'>                     +  poolName);  91 500)this.width=500'>             return  con;  92 500)this.width=500'> 500)this.width=500'>        }   catch  (ClassNotFoundException cnfe)  500)this.width=500'> {  93 500)this.width=500'>             throw   new  RuntimeException( " 无法装入数据库引擎 " );  94 500)this.width=500'> 500)this.width=500'>        }   catch  (SQLException sqle)  500)this.width=500'> {  95 500)this.width=500'>             throw   new  RuntimeException( " 无法打开数据库连接 " );  96 500)this.width=500'>        }  97 500)this.width=500'>    }  98 500)this.width=500'>  99 500)this.width=500'> 500)this.width=500'>     /** */ /** 100 500)this.width=500'>     * 执行清理过程 101 500)this.width=500'>     * <li>关闭数据库连接</li> 102 500)this.width=500'>     * <li>关闭语句对象</li> 103 500)this.width=500'>     * <li>关闭结果集</li> 104 500)this.width=500'>     *  105 500)this.width=500'>     *  @param  con 106 500)this.width=500'>     *  @param  s 107 500)this.width=500'>     *  @param  rs 108 500)this.width=500'>      */ 109 500)this.width=500'> 500)this.width=500'>     public   static   void  closeAll(Connection con, Statement s, ResultSet rs)  500)this.width=500'> { 110 500)this.width=500'> 500)this.width=500'>         try   500)this.width=500'> { 111 500)this.width=500'> 500)this.width=500'>             if  (rs  !=   null )  500)this.width=500'> { 112 500)this.width=500'>                rs.close(); 113 500)this.width=500'>                rs  =   null ; 114 500)this.width=500'>            } 115 500)this.width=500'> 500)this.width=500'>             if  (s  !=   null )  500)this.width=500'> { 116 500)this.width=500'>                s.close(); 117 500)this.width=500'>                s  =   null ; 118 500)this.width=500'>            } 119 500)this.width=500'> 500)this.width=500'>             if  (con  !=   null )  500)this.width=500'> { 120 500)this.width=500'>                con.close(); 121 500)this.width=500'>                con  =   null ; 122 500)this.width=500'>            } 123 500)this.width=500'> 500)this.width=500'>        }   catch  (SQLException sqle)  500)this.width=500'> { 124 500)this.width=500'>             //  nothing to do, forget it; 125 500)this.width=500'>         } 126 500)this.width=500'>    } 127 500)this.width=500'> 128 500)this.width=500'> 500)this.width=500'>     public   static   void  main(String[] args)  500)this.width=500'> { 129 500)this.width=500'> //         DaoUtil daoUtil = new DaoUtil(); 130 500)this.width=500'> //         Connection connection = null; 131 500)this.width=500'> //         Statement statement = null; 132 500)this.width=500'> //         connection = daoUtil.getConnection(); 133 500)this.width=500'> //         ResultSet rs = null; 134 500)this.width=500'> //         try { 135 500)this.width=500'> //             statement = connection.createStatement(); 136 500)this.width=500'> //             rs = statement.executeQuery("select * from admin"); 137 500)this.width=500'> //             while(rs.next()){ 138 500)this.width=500'> //                 System.out.println(rs.getString("adminName")); 139 500)this.width=500'> //             } 140 500)this.width=500'> //         } catch (SQLException e) { 141 500)this.width=500'> //             e.printStackTrace(); 142 500)this.width=500'> //         } 143 500)this.width=500'> 144 500)this.width=500'>    } 145 500)this.width=500'> 146 500)this.width=500'>}


阅读全文(2565) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.095 second(s), page refreshed 144784529 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号