tomcat下配置mysql数据库连接池

来源:互联网 发布:iface702软件下载 编辑:程序博客网 时间:2024/06/07 20:18

1. 在Tomcat安装目录下conf目录下server.xml中最后的“</Host>”标记之前添加
如下配置:

 <Context path="/myinfoSystem" docBase="myinfoSystem"
          debug="5" reloadable="true" crossContext="true"   workDir="">
    <Resource   name="infosys"
               auth="Container"
      type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="root" password="system"
      driverClassName="org.gjt.mm.mysql.Driver"
      url="jdbc:mysql://localhost/myinfoSystem"/>
 </Context>

2. 在本应用的WEB-INF目录下web.xml文件中添加如下配置
   <resource-ref>
       <description>DB Connection</description>
       <res-ref-name>infosys</res-ref-name>
       <res-type>javax.sql.DataSource</res-type>
       <res-auth>Container</res-auth>
   </resource-ref> 

3. 在JSP或Servlet或JavaBean中用如下Java代码获得数据库连接

            Context initial = new InitialContext();   
            //其中mysql为数据源jndi名称       
            DataSource ds = (DataSource)initial.lookup("java:comp/env/infosys");
            Connection con=ds.getConnection();

原创粉丝点击