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

以下的文章主要讲述的是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文件,在<context></context>之间添加java jsp tomcat6 MySQL 连接池如下:

 

 
 
 
  1. <Resource name="jdbc/MySQL"   
  2. auth="Container"   
  3. type="javax.sql.DataSource"   
  4. driverClassName="com.MySQL.jdbc.Driver"   
  5. url="jdbc:MySQL://localhost/test"   
  6. username="root"   
  7. password="root"   
  8. maxActive="100"   
  9. maxIdle="30"   
  10. maxWait="10000" />   
  11.  

 

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

3.配置你的应用下的web.xml中的<web-app></web-app>之间加入:

 

 
 
 
  1. xml 代码<resource-ref>   
  2. <description>DB Connection</description>   
  3. <res-ref-name>jdbc/MySQLx</res-ref-name>   
  4. <res-type>javax.sql.DataSource</res-type>   
  5. <res-auth>Container</res-auth>   
  6. </resource-ref>  

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

 
 
 
  1. java 代码<!doctype html public "-//w3c//dtd html 4.0 transitional//en"   
  2. "http://www.w3.org/TR/REC-html40/strict.dtd">   
  3. <%@ page import="java.sql.*"%>   
  4. <%@ page import="javax.sql.*"%>   
  5. <%@ page import="javax.naming.*"%>   
  6. <%@ page session="false" %>   
  7. <html>   
  8. <head>   
  9. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">   
  10. <title></title>   
  11. <%   
  12. out.print("我的测试开始");   
  13. DataSource ds = null;   
  14. try{   
  15. InitialContext ctx=new InitialContext();   
  16. ds=(DataSource)ctx.lookup("java:comp/env/jdbc/MySQL");   
  17. Connection conn = ds.getConnection();   
  18. Statement stmt = conn.createStatement();   

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

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

 
 
 
  1. String strSql = " select * from users";   
  2. ResultSet rs = stmt.executeQuery(strSql);   
  3. while(rs.next()){   
  4. out.print(rs.getString(1));   
  5. }   
  6. out.print("我的测试结束");   
  7. }   
  8. catch(Exception ex){   
  9. out.print(“出现例外,信息是:”+ex.getMessage());   
  10. ex.printStackTrace();   
  11. }   
  12. %>   
  13. </head>   
  14. <body>   
  15. </body>   
  16. </html>   

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

【编辑推荐】

  1. 让MySQL支持中文的实际操作步骤
  2. 配置MySQL与卸载MySQL实操
  3. MySQL数据库访问妙招在Linux之下
  4. 图解MySQL数据库安装与实际操作
  5. MySQL数据库进行备份在Linux异构网络里 

 

THE END