数据库连接池---总结

来源:互联网 发布:亚马逊 大数据服务器 编辑:程序博客网 时间:2024/05/22 13:50

 
一、DBCP 连接:
 DBCP 连接池是 Apache 软件基金组织下的一个开源连接池实现。
 
 需要的 java 包
  commons - dbcp - 1.2.1.jar //连接池的实现
  commons - pool - 1.2.jar  //连接池实现的依赖库
  commons - collection.jar //连接池实现的依赖库
  
 主要代码:
  import org.apache.commons.dbcp.BasicDataSource;

 // 数据库池

 private static BasicDataSource dataSource = new BasicDataSource();

 public static Connection getConn(){
  Connection con = null ;
  try {
   datasource.setDriverClassName("com.mysql.jdbc.Driver");
   datasource.setUrl("jdbc:mysql://localhost/mysql");
   datasource.setUsername("root");
   datasource.setPassword("root");
   datasource.setMaxActive(10);
   datasource.setMaxIdle(5);
   datasource.setMaxWait(0);
   con = datasource.getConnection();
   
  } catch (Exception e) {
   // TODO: handl exception
   e.printStackTrace();
  }
  return con ;
 }

二、C3PO 连接:
 C3PO 连接池是一个优秀的连接池,推荐使用。
 
 需要的 java 包
  c3po0.902.jar
  
 主要代码:
  import com.mchange.v2.c3p0.ComboPooledDataSource;

 // 数据库池

 private static ComboPooledDataSource dataSource = new ComboPooledDataSource();

 public static Connection getConn(){
  Connection con = null ;
  try {
   datasource.setDriverClass("com.mysql.jdbc.Driver");
   datasource.setJdbcUrl("jdbc:mysql://localhost/mysql");
   datasource.setUser("root");
   datasource.setPassword("root");
   datasource.setMaxActive(10);
   datasource.setMaxIdle(5);
   datasource.setMaxWait(0);
   con = datasource.getConnection();
   
  } catch (Exception e) {
   // TODO: handl exception
   e.printStackTrace();
  }
  return con ;
 }

三、Jndi 与 Tomact 连接池
 此连接池不需要其他的 java 包

 1、在 Tomact 的 conf 文件下的 context.xml 中加上如下:

 <Resource name="jdbc/mysql"
    auth="Container"
    type="javax.sql.DataSource"
                  maxActive="100"
    maxIdle="30"
    maxWait="10000"
                  username="root"
    password="root"
    driverClassName="com.mysql.jdbc.Driver"
                url="jdbc:mysql://localhost:3306/test?autoReconnect=true"/>

 说明:
  type: 指数据源类型;
  name: 为这个Resource 的名字 "jdbc/mysql" 它与 下一步 在 web.xml 的文件中;
   <res-ref-name>jdbc/mysql</res-ref-name> 
  **********************  中的  “ jdbc/mysql ”  名字可以任意取   但用到这个名字时,
  ********************** 一定要一样,有三个地方要用 :context.xml, web.xml, java 程序中的类中,
   和 index.jsp
   <sql:query var="rs" dataSource="jdbc/mysql">
    select bid, bname from books
    </sql:query>
   出现。

  maxActive: 表示 dbcp 的最大连接数目,0 为不受限制;
  maxIdle: 表示 dbcp 空闲时的数据库连接的最大数目;
  maxWait: 表示 dbcp 中的数据库接处于空闲状态的最长时间,0 为不受制。
  username: 数据库用户名;
  password: 数据库登录密码;
  driverClassName: 指数据库的 jdbc 驱动程序
  url :中有个 test 它是 mysql 中的一个数据库的名字


 2、web.xml configuration
 
 <resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/mysql</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
 </resource-ref>

 3、测试:
 在  mysql 数据库中有个 test 数据库
  test 中有表如下
         create table books(bid int , bname varchar(20))
 

 html 测试:
  <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
         <%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>

  <body>
    This is my JSP page. this is test JNDI datasource<br>
   <sql:query var="rs" dataSource="jdbc/mysql">
   select bid, bname from books
   </sql:query>
  
   <c:forEach var="row" items="${rs.rows}">
   bid:${row.bid}<br>
       bname:${row.bname}
    </c:forEach>
   
  </body>

 java 程序测试:
     // 无需导入外部包  
  import java.sql.Connection;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.sql.SQLException;

  import javax.naming.InitialContext;
  import javax.naming.NamingException;
  import javax.sql.DataSource;

 public Connection getConn() {
  Connection conn = null ;
  try {
   InitialContext ctx = new InitialContext();
   
   DataSource dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/mysql");
   
    conn = dataSource.getConnection();
   String sql = "select * from books" ;
   PreparedStatement ps = conn.prepareStatement(sql);
   ResultSet rs = ps.executeQuery();
   while(rs.next()){
    System.out.println (rs.getInt(1));
   }
  } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   
  } catch (NamingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return conn;
 }

 ----------  说明:这里的 java:comp/env 是前缀,java 语言规范,后面是你在 web.xml 中
     <res-ref-name>jdbc/mysql</res-ref-name> 的名字,
   这个java 程序 不能直接测试, 因为 context.xml、 web.xml 都是Tomcat 中的,
   服务没开,无法读取 文件信息。
  用时:可以直接将  context.xml  放到工程的 META-INF/ 目录下。

 

 

 

原创粉丝点击