如题,如何配置数据库连接池,小弟快要疯了。

解决方案 »

  1.   

    在tomcat下配置mysql的链接池步骤如下:1.在CATALINA_HOME/conf/server.xml中添加配置信息,声明连接池的具体信息,添加内容如下:
      
      <!--声明连接池-->
      
      <Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource"/>
      
      <!-- 对连接池的参数进行设置 -->
      
      <ResourceParams name="jdbc/mysql">
      
      <parameter>
      
      <name>factory</name>
      
      <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
      
      </parameter>
      
      <parameter>
      
      <name>maxWait</name>
      
      <value>5000</value>
      
      </parameter>
      
      <parameter>
      
      <name>maxActive</name>
      
      <value>20</value>
      
      </parameter>
      
      <parameter>
      
      <name>username</name>
      
      <value>shopadm</value>
      
      </parameter>
      
      <parameter>
      
      <name>password</name>
      
      <value>123</value>
      
      </parameter>
      
      <parameter>
      
      <name>url</name>
      
      <value>jdbc:mysql://localhost/shopdb?useUnicode=true&charact-erEncoding=gb2312</value>
      
      </parameter>
      
      <parameter>
      
      <name>driverClassName</name>
      
      <value>com.mysql.jdbc.Driver</value>
      
      </parameter>
      
      <parameter>
      
      <name>maxIdle</name>
      
      <value>10</value>
      
      </parameter>
      
      </ResourceParams>
      
      2. 在CATALINA_HOME/conf/web.xml的</web-app>前添加如下信息:
      
      <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>
      
      其中<res-ref-name>中的参数名必须和server.xml中声明的连接名一样。
      
      3. 在CATALINA_HOME/conf/catalina/localhost目录下找到需要进行数据库连接的当前程序的配置信息,比如这里是shopping.xml,在这个文件中添加如下信息:
      
      <Context …>
      
      …
      
      <ResourceLink name=”jdbc/mysql” global=”jdbc/mysql” type=”javax.sql.DataSource”/>
      
      …
      
      </Context>
      
      

  2.   

    1、把数据库驱动包 copy 到 %CATALINA_HOME%\common\lib 下。2、修改 %CATALINA_HOME%\conf\server.xml 文件,在 <Host> 节点下添加: <!-- appName 为项目名 --!>  
    <Context path="/appName" docBase="appName" auth="Container">  
        <Resource name="jdbc/MySQLDS" scope="Shareable"  
            type="javax.sql.DataSource"  
            url="jdbc:mysql://localhost:3306/test?useUnicode=true&charact-erEncoding=GBK"  
            driverClassName="com.mysql.jdbc.Driver"  
            username="root" password="111111"  
            maxWait="3000" maxIdle="100" maxActive="10" />  
    </Context>(或者在 %appName%\META-INF 下建立 context.xml,内容为上面的代码。) 3、修改 web.xml,在 <web-app> 节点下添加: <resource-ref>  
        <description>Oracle Datasource example</description>  
        <res-ref-name>MySQLDS</res-ref-name>  
        <res-type>javax.sql.DataSource</res-type>  
        <res-auth>Container</res-auth>  
    </resource-ref> (这一步不添加页没问题。)4、在代码中获取数据库连接: import java.sql.Connection;  
      
    import javax.naming.Context;  
    import javax.naming.InitialContext;  
    import javax.sql.DataSource;  
      
    public class DBUtil {  
      
        public Connection getConnection() throws Exception {  
            Context context = new InitialContext();  
      
            // 获取数据源  
            DataSource ds = (DataSource) context.lookup("java:comp/env/jdbc/MySQLDS");  
      
            // 获取数据库连接  
            Connection conn = ds.getConnection();  
      
            if (conn != null && !conn.isClosed()) {  
                return conn;  
            } else {  
                return null;  
            }  
        }  
      
    }
      

  3.   

    上的,楼上的方法可行,,其实各种数据库都大体相同,, 毕竟都是把  Connection 放到一个容器中。