>  기사  >  데이터 베이스  >  MySQL 连接池的实际配置问题

MySQL 连接池的实际配置问题

WBOY
WBOY원래의
2016-06-07 16:11:511002검색

以下的文章主要介绍的是 java jsp tomcat6 MySQL 连接池的实际配置问题。 最近将tomcat5.5升到了tomcat6,原本以为只是很简单的升个版本,但是出乎人意料的是tomcat5和tomcat6之间的变化还是蛮大的。 这里我就讲一下我在配置MySQL连接池碰到的一点问c题吧 最

以下的文章主要介绍的是 java jsp tomcat6 MySQL 连接池的实际配置问题。 最近将tomcat5.5升到了tomcat6,原本以为只是很简单的升个版本,但是出乎人意料的是tomcat5和tomcat6之间的变化还是蛮大的。

这里我就讲一下我在配置MySQL连接池碰到的一点问c题吧

最明显的变化就是tomcat5和tomcat6的目录结构有了变化,在tomcat6里没有了common目录,取而代之的是直接有了一个lib目录,下面就看一下tomcat6是如何配置连接池的

1.需要的文件:MySQL-5.0.27-win32.zip(安装文件),MySQL-connector-java-5.0.4-bin.jar(连接驱动程序),apache-tomcat-6.0.10.exe(安装文件)

2.配置tomcat下的conf下的context.xml文件,在之间添加连接池如下:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span class="tag-name">Resource</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"jdbc/</span></span>MySQL<span><span class="attribute-value">"</span><span>   </span></span></span></li>
<li>
<span class="attribute">auth</span><span>=</span><span class="attribute-value">"Container"</span><span>   </span>
</li>
<li class="alt">
<span class="attribute">type</span><span>=</span><span class="attribute-value">"javax.sql.DataSource"</span><span>   </span>
</li>
<li>
<span class="attribute">driverClassName</span><span>=</span><span class="attribute-value">"com.MySQL.jdbc.Driver"</span><span>   </span>
</li>
<li class="alt">
<span class="attribute">url</span><span>=</span><span class="attribute-value">"jdbc:MySQL://localhost/test"</span><span>   </span>
</li>
<li>
<span class="attribute">username</span><span>=</span><span class="attribute-value">"root"</span><span>   </span>
</li>
<li class="alt">
<span class="attribute">password</span><span>=</span><span class="attribute-value">"root"</span><span>   </span>
</li>
<li>
<span class="attribute">maxActive</span><span>=</span><span class="attribute-value">"100"</span><span>   </span>
</li>
<li class="alt">
<span class="attribute">maxIdle</span><span>=</span><span class="attribute-value">"30"</span><span>   </span>
</li>
<li>
<span class="attribute">maxWait</span><span>=</span><span class="attribute-value">"10000"</span><span> </span><span class="tag">/></span><span>   </span>
</li>
</ol>

上面的参数不用我说了吧,这些都知道是什么意思吧.

3.配置你的应用下的web.xml中的之间加入:

<ol class="dp-xml">
<li class="alt"><span><span>xml 代码  </span></span></li>
<li><span class="tag"><span class="tag-name">resource-ref</span><span class="tag">></span><span>   </span></span></li>
<li class="alt"><span class="tag"><span class="tag-name">description</span><span class="tag">></span><span>DB Connection</span><span class="tag"></span><span class="tag-name">description</span><span class="tag">></span><span>   </span></span></li>
<li><span class="tag"><span class="tag-name">res-ref-name</span><span class="tag">></span><span>jdbc/</span>MySQL<span>x</span><span class="tag"></span><span class="tag-name">res-ref-name</span><span class="tag">></span><span>   </span></span></li>
<li class="alt"><span class="tag"><span class="tag-name">res-type</span><span class="tag">></span><span>javax.sql.DataSource</span><span class="tag"></span><span class="tag-name">res-type</span><span class="tag">></span><span>   </span></span></li>
<li><span class="tag"><span class="tag-name">res-auth</span><span class="tag">></span><span>Container</span><span class="tag"></span><span class="tag-name">res-auth</span><span class="tag">></span><span>   </span></span></li>
<li class="alt">
<span class="tag"></span><span class="tag-name">resource-ref</span><span class="tag">></span><span>  </span>
</li>
</ol>

4.大功告成,不用在原来的server.xml里面配置了,下面就可以编写测试程序了,这个网上就很多了,主要的就上面,当然要把连接驱动程序都放到tomcat6下的lib下面.测试代码如下:

java 代码
 

<ol class="dp-xml">
<li class="alt"><span><span>nbsp;html public "-//w3c//dtd html 4.0 transitional//en"   </span></span></li>
<li>
<span>"http://www.w3.org/TR/REC-html40/strict.dtd"</span><span class="tag">></span><span>   </span>
</li>
<li class="alt"><span class="tag"><span>%@ page </span><span class="attribute">import</span><span>=</span><span class="attribute-value">"java.sql.*"</span><span>%</span><span class="tag">></span><span>   </span></span></li>
<li><span class="tag"><span>%@ page </span><span class="attribute">import</span><span>=</span><span class="attribute-value">"javax.sql.*"</span><span>%</span><span class="tag">></span><span>   </span></span></li>
<li class="alt"><span class="tag"><span>%@ page </span><span class="attribute">import</span><span>=</span><span class="attribute-value">"javax.naming.*"</span><span>%</span><span class="tag">></span><span>   </span></span></li>
<li><span class="tag"><span>%@ page </span><span class="attribute">session</span><span>=</span><span class="attribute-value">"false"</span><span> %</span><span class="tag">></span><span>   </span></span></li>
<li class="alt"><span class="tag"><span class="tag-name">html</span><span class="tag">></span><span>   </span></span></li>
<li><span class="tag"><span class="tag-name">head</span><span class="tag">></span><span>   </span></span></li>
<li class="alt"><span class="tag"><span class="tag-name">meta</span><span> </span><span class="attribute">http-equiv</span><span>=</span><span class="attribute-value">"Content-Type"</span><span> </span><span class="attribute">content</span><span>=</span><span class="attribute-value">"text/html; charset=gb2312"</span><span class="tag">></span><span>   </span></span></li>
<li><span class="tag"><span class="tag-name">title</span><span class="tag">></span><span class="tag"></span><span class="tag-name">title</span><span class="tag">></span><span>   </span></span></li>
<li class="alt"><span class="tag"><span>%   </span></span></li>
<li><span>out.print("我的测试开始");   </span></li>
<li class="alt">
<span>DataSource </span><span class="attribute">ds</span><span> = </span><span class="attribute-value">null</span><span>;   </span>
</li>
<li><span>try{   </span></li>
<li class="alt">
<span>InitialContext </span><span class="attribute">ctx</span><span>=</span><span class="attribute-value">new</span><span> InitialContext();   </span>
</li>
<li>
<span class="attribute">ds</span><span>=(DataSource)ctx.lookup("java:comp/env/jdbc/</span>MySQL<span>");   </span>
</li>
<li class="alt">
<span>Connection </span><span class="attribute">conn</span><span> = </span><span class="attribute-value">ds</span><span>.getConnection();   </span>
</li>
<li>
<span>Statement </span><span class="attribute">stmt</span><span> = </span><span class="attribute-value">conn</span><span>.createStatement();   </span>
</li>
</ol>

提示:users必须是数据库已有的表,

这里的数据库前文提及的Data Source URL配置里包含的数据库。

<ol class="dp-xml">
<li class="alt"><span><span>String </span><span class="attribute">strSql</span><span> = </span><span class="attribute-value">" select * from users"</span><span>;   </span></span></li>
<li>
<span>ResultSet </span><span class="attribute">rs</span><span> = </span><span class="attribute-value">stmt</span><span>.executeQuery(strSql);   </span>
</li>
<li class="alt"><span>while(rs.next()){   </span></li>
<li><span>out.print(rs.getString(1));   </span></li>
<li class="alt"><span>}   </span></li>
<li><span>out.print("我的测试结束");   </span></li>
<li class="alt"><span>}   </span></li>
<li><span>catch(Exception ex){   </span></li>
<li class="alt"><span>out.print(“出现例外,信息是:”+ex.getMessage());   </span></li>
<li><span>ex.printStackTrace();   </span></li>
<li class="alt"><span>}   </span></li>
<li>
<span>%</span><span class="tag">></span><span>   </span>
</li>
<li class="alt">
<span class="tag"></span><span class="tag-name">head</span><span class="tag">></span><span>   </span>
</li>
<li><span class="tag"><span class="tag-name">body</span><span class="tag">></span><span>   </span></span></li>
<li class="alt">
<span class="tag"></span><span class="tag-name">body</span><span class="tag">></span><span>   </span>
</li>
<li>
<span class="tag"></span><span class="tag-name">html</span><span class="tag">></span><span>   </span>
</li>
</ol>

以上的相关内容就是对 java jsp tomcat6 MySQL 连接池配置的介绍,望你能有所收获。


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.