Home > Database > Mysql Tutorial > body text

java jsp tomcat6 MySQL 连接池的正确配置

WBOY
Release: 2016-06-07 16:13:49
Original
983 people have browsed it

以下的文章主要讲述的是java jsp tomcat6 MySQL 连接池的正确配置,有很多人都是用tomcat5 对java jsp tomcat6 MySQL 连接池进行配置,但是我个人认为用tomcat5 太费时间也很麻烦,现在分享如下: 1.需要的文件:MySQL-5.0.27-win32.zip(安装文件),MySQL-conne

以下的文章主要讲述的是java jsp tomcat6 MySQL 连接池的正确配置,有很多人都是用tomcat5 对java jsp tomcat6 MySQL 连接池进行配置,但是我个人认为用tomcat5 太费时间也很麻烦,现在分享如下:

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文件,在之间添加java jsp tomcat6 MySQL 连接池如下:

<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>
<li class="alt"><span> </span></li>
</ol>
Copy after login

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

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

<ol class="dp-xml">
<li class="alt"><span><span>xml 代码</span><span class="tag"><span class="tag-name">resource-ref</span><span class="tag">></span><span>   </span></span></span></li>
<li><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 class="alt"><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><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 class="alt"><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>
<span class="tag"></span><span class="tag-name">resource-ref</span><span class="tag">></span><span>  </span>
</li>
</ol>
Copy after login

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

<ol class="dp-xml">
<li class="alt"><span><span>java 代码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>
Copy after login

提示: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>
Copy after login

上面的保证能行,我已经测试过了.如有问题可以给我留言.以上的相关内容就是对java jsp tomcat6 MySQL 连接池配置的介绍,望你能有所收获。


 


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template