Tomat 连接数据连接池

来源:互联网 发布:设计软件源码 编辑:程序博客网 时间:2024/05/17 16:53

在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(); 来连接数据库连接池

原创粉丝点击