tomcat 6.0 + mysql + jndi 数据库连接池配置

1、新建动态web项目。

 

2、添加jar包

    将mysql jdbc驱动添加到tomcat安装目录下的lib目录。

3、在META-INF下添加content.xml文件。内容如下:

<?xml version="1.0" encoding="UTF-8"?> <Context reloadable="true" crossContext="true"> <!-- Default set of monitored resources --> <WatchedResource>WEB-INF/web.xml</WatchedResource> <Resource name="jdbc/mydb" auth="Container" type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/spring" username="root" password="123456" maxActive="20" maxIdle="10" maxWait="10000" /> </Context>

 

  • WatchedResource - The auto deployer will monitor the specified static resource of the web application for updates, and will reload the web application if is is updated. The content of this element must be a string.
  •  

    4、连接jdbc的Java代码:

     

    package cn.guopeng.dbcp.dbconn; import java.sql.Connection; import java.sql.SQLException; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; public class MySqlDbconn { public static Connection getConnection(){ Connection conn = null; try { Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mydb"); conn = ds.getConnection(); } catch (NamingException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } }

     

     

    你可能感兴趣的:(tomcat,mysql,jdbc,数据库连接池,application,resources)