Rumah > Artikel > pangkalan data > MySQL 连接池的实际配置问题
以下的文章主要介绍的是 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 连接池配置的介绍,望你能有所收获。