您的位置:首页 > 运维架构 > Tomcat

Tomcat jndi 数据库配置 连接池配置

2006-09-19 11:28 736 查看
在Tomcat 5.5/conf/Catalina/localhost/xxxxx.xml(xxxxx.xml为你自己的web的配置xml文件) 中添加resource

<Resource name="MYSQL"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
password="********"
maxIdle="2"
maxWait="5000"
username="root"
url="jdbc:mysql://localhost:3306/jnestore"
maxActive="10"/>

以上是tomcat 5.5版本的配置,5.5版本只有这样陪才有效果,以前的版本可以按照paramter的方法添加;

Resource 一定要包含在<Context ......> </Context> 之间

在你的web的路近下,WEB-INF中找到web.xml,添加资源映射

<resource-ref>
<description>connection</description>
<res-ref-name>MYSQL</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

把mysql的jdbc驱动.jar包放到tomcat的common/lib下去,也可以放到WEB-INF/lib下,

重新启动tomcat.

就可以用Context ctx = new InitialContext();

DataSource ds = (DataSource) ctx.lookup("java:comp/env/MYSQL");

Connection con = ds.getConnection(); 来连接数据库连接池
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: