dbcp使用

来源:互联网 发布:知乎第一大v 编辑:程序博客网 时间:2024/04/26 07:51


dbcp提供了数据库连接池;
可以在spring,iBatis,hibernate中调用dbcp完成数据库连接,框架一般都提供了dbcp连接的方法;
tomcat中也提供了dbcp的jndi设置方法;

也可以不在框架中使用dbcp,单独使用dbcp需要3个包:common-dbcp.jar,common-pool.jar,common-collections.jar

 

下面是个dbcp的实用类,通过它可以完成DBCP的使用:

Java代码  收藏代码
  1. package dbcp;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.ResultSet;  
  5. import java.sql.SQLException;  
  6. import java.sql.Statement;  
  7. import java.util.HashMap;  
  8. import java.util.Map;  
  9. import javax.sql.DataSource;  
  10. import org.apache.commons.dbcp.BasicDataSource;  
  11.   
  12. /** 
  13.  * @author space 
  14.  * @date Aug 12, 2008 3:25:49 PM 
  15.  * 
  16.  * dbcp 实用类,提供了dbcp连接,不允许继承; 
  17.  *  
  18.  * 该类需要有个地方来初始化 DS ,通过调用initDS 方法来完成,可以在通过调用带参数的构造函数完成调用,可以在其它类中调用,也可以在本类中加一个static{}来完成; 
  19.  */  
  20. public final class DbcpBean {  
  21.     /** 数据源,static */  
  22.     private static DataSource DS;  
  23.   
  24.     /** 从数据源获得一个连接 */  
  25.     public Connection getConn() {  
  26.   
  27.         try {  
  28.             return DS.getConnection();  
  29.         } catch (SQLException e) {  
  30.             System.out.println("获得连接出错!");  
  31.             e.printStackTrace();  
  32.             return null;  
  33.         }  
  34.     }  
  35.   
  36.     /** 默认的构造函数 */  
  37.     public DbcpBean() {  
  38.     }  
  39.   
  40.     /** 构造函数,初始化了 DS ,指定 数据库 */  
  41.     public DbcpBean(String connectURI) {  
  42.         initDS(connectURI);  
  43.     }  
  44.   
  45.     /** 构造函数,初始化了 DS ,指定 所有参数 */  
  46.     public DbcpBean(String connectURI, String username, String pswd, String driverClass, int initialSize,  
  47.             int maxActive, int maxIdle, int maxWait) {  
  48.         initDS(connectURI, username, pswd, driverClass, initialSize, maxActive, maxIdle, maxWait);  
  49.     }  
  50.   
  51.     /** 
  52.      * 创建数据源,除了数据库外,都使用硬编码默认参数; 
  53.      *  
  54.      * @param connectURI 数据库 
  55.      * @return 
  56.      */  
  57.     public static void initDS(String connectURI) {  
  58.         initDS(connectURI, "root""password""com.mysql.jdbc.Driver"51003010000);  
  59.     }  
  60.   
  61.     /**  
  62.      * 指定所有参数连接数据源 
  63.      *  
  64.      * @param connectURI 数据库 
  65.      * @param username 用户名 
  66.      * @param pswd 密码 
  67.      * @param driverClass 数据库连接驱动名 
  68.      * @param initialSize 初始连接池连接个数 
  69.      * @param maxActive 最大激活连接数 
  70.      * @param maxIdle 最大闲置连接数 
  71.      * @param maxWait 获得连接的最大等待毫秒数 
  72.      * @return 
  73.      */  
  74.     public static void initDS(String connectURI, String username, String pswd, String driverClass, int initialSize,  
  75.             int maxActive, int maxIdle, int maxWait) {  
  76.         BasicDataSource ds = new BasicDataSource();  
  77.         ds.setDriverClassName(driverClass);  
  78.         ds.setUsername(username);  
  79.         ds.setPassword(pswd);  
  80.         ds.setUrl(connectURI);  
  81.         ds.setInitialSize(initialSize); // 初始的连接数;  
  82.         ds.setMaxActive(maxActive);  
  83.         ds.setMaxIdle(maxIdle);  
  84.         ds.setMaxWait(maxWait);  
  85.         DS = ds;  
  86.     }  
  87.   
  88.     /** 获得数据源连接状态 */  
  89.     public static Map<String, Integer> getDataSourceStats() throws SQLException {  
  90.         BasicDataSource bds = (BasicDataSource) DS;  
  91.         Map<String, Integer> map = new HashMap<String, Integer>(2);  
  92.         map.put("active_number", bds.getNumActive());  
  93.         map.put("idle_number", bds.getNumIdle());  
  94.         return map;  
  95.     }  
  96.   
  97.     /** 关闭数据源 */  
  98.     protected static void shutdownDataSource() throws SQLException {  
  99.         BasicDataSource bds = (BasicDataSource) DS;  
  100.         bds.close();  
  101.     }  
  102.   
  103.     public static void main(String[] args) {  
  104.         DbcpBean db = new DbcpBean("jdbc:mysql://localhost:3306/testit");  
  105.   
  106.         Connection conn = null;  
  107.         Statement stmt = null;  
  108.         ResultSet rs = null;  
  109.   
  110.         try {  
  111.             conn = db.getConn();  
  112.             stmt = conn.createStatement();  
  113.             rs = stmt.executeQuery("select * from test limit 1 ");  
  114.             System.out.println("Results:");  
  115.             int numcols = rs.getMetaData().getColumnCount();  
  116.             while (rs.next()) {  
  117.                 for (int i = 1; i <= numcols; i++) {  
  118.                     System.out.print("\t" + rs.getString(i) + "\t");  
  119.                 }  
  120.                 System.out.println("");  
  121.             }  
  122.             System.out.println(getDataSourceStats());  
  123.         } catch (SQLException e) {  
  124.             e.printStackTrace();  
  125.         } finally {  
  126.             try {  
  127.                 if (rs != null)  
  128.                     rs.close();  
  129.                 if (stmt != null)  
  130.                     stmt.close();  
  131.                 if (conn != null)  
  132.                     conn.close();  
  133.                 if (db != null)  
  134.                     shutdownDataSource();  
  135.             } catch (Exception e) {  
  136.                 e.printStackTrace();  
  137.             }  
  138.         }  
  139.     }  
  140. }
0 0