`

配置TOMCAT6的中mysql数据库连接池

    博客分类:
  • java
阅读更多

1、把mysql的jar包拷贝到tomcat中的lib文件夹中

2、在tomcat的conf文件夹中找到一个叫context.xml文件

 

<!-- The contents of this file will be loaded for each web application -->
<Context>

    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
	<Resource 
	   name="jdbc/mysql"  
       auth="Container" 
       type="javax.sql.DataSource"  
       driverClassName="com.mysql.jdbc.Driver"  
       url="jdbc:mysql://localhost/数据库名称"  
       username="root"  
       password="root"  
       maxActive="10000"  
       maxIdle="30"  
       maxWait="10000" />
</Context>

 3、在你的项目中web.xml中添加

 

<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>

 4、好了tomcat连接池就这样配置好了,接下来获取连接

 

public class DBPoolTomcat {
	public static Connection getConnection() {
		try {
			Context initCtx = new InitialContext();
			Context envCtx = (Context) initCtx.lookup("java:comp/env");
			DataSource ds = (DataSource) envCtx.lookup("jdbc/mysql");
			Connection conn = ds.getConnection();
			return conn;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}

 5、详细参数说明!请参照http://commons.apache.org/dbcp/configuration.html

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics