tomcat配置连接池(MySQL数据库)
2011-03-14 15:28
316 查看
在TOMCAT_HOME/conf/context.xml文件的<Context>节点中加入:
<Resource
name="jdbc/sql"
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"
/>
属性解释:
name:数据源名字 auth:权限 type:类型
maxActive:最大连接数据库连接数,设 0 为没有限制
maxIdle:最大等待连接中的数量,设 0 为没有限制
maxWait:最大等待毫秒数, 单位为 ms, 超过时间会出错误信息
在web应用的web.xml中加入
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/sql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
在项目中获取连接方法:
Context initContext=new InitialContext();
Context envContext=(Context)initContext.lookup("java:/comp/env");
DataSource ds=(DataSource)envContext.lookup("jdbc/sql");
Connection conn=ds.getConnection();
Statement st=null;
ResultSet rs=null;
st=conn.createStatement();
rs=st.executeQuery(sql);
<Resource
name="jdbc/sql"
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"
/>
属性解释:
name:数据源名字 auth:权限 type:类型
maxActive:最大连接数据库连接数,设 0 为没有限制
maxIdle:最大等待连接中的数量,设 0 为没有限制
maxWait:最大等待毫秒数, 单位为 ms, 超过时间会出错误信息
在web应用的web.xml中加入
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/sql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
在项目中获取连接方法:
Context initContext=new InitialContext();
Context envContext=(Context)initContext.lookup("java:/comp/env");
DataSource ds=(DataSource)envContext.lookup("jdbc/sql");
Connection conn=ds.getConnection();
Statement st=null;
ResultSet rs=null;
st=conn.createStatement();
rs=st.executeQuery(sql);
相关文章推荐
- tomcat5.5配置MYSQL数据库连接池
- Tomcat下配置MySQL数据库连接池
- 更新Tomcat5.5+MySql数据库连接池配置方法
- Tomcat下配置MySQL数据库连接池
- tomcat环境下的mysql数据库连接池配置方法
- Tomcat下配置MySQL数据库连接池
- Tomcat5.5.23 配置mysql数据库连接池
- tomcat7下的MySQL数据库连接池的配置
- Tomcat下配置MySQL数据库连接池
- tomcat5.5+mysql数据库连接池 的相关配置
- (备忘)Eclipse下配置tomcat连接池连接mysql数据库
- 学习Tomcat下配置MySQL数据库连接池
- Tomcat下配置MySQL数据库连接池
- Tomcat下配置MySQL数据库连接池
- tomcat配置mysql数据库连接池
- tomcat5.5配置MYSQL数据库连接池
- Tomcat下配置MySQL数据库连接池
- Tomcat下配置MySQL数据库连接池
- Java开发:Tomcat和MySQL数据库连接池配置
- [zz]tomcat6.0配置mysql数据库连接池