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

 

相关文章:

  • 2021-10-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-08
  • 2022-02-12
  • 2021-11-19
猜你喜欢
  • 2021-05-20
  • 2022-01-08
  • 2021-07-18
  • 2022-01-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案