配置java+jsp+tomcat+mysql 连接池配置

环境:

linux AS4 U5

Tomcat5.5.28

mysql 5.1.X

配置步骤:

1. 将mysql-connector-java-5.1.10-bin.jar复制到$CATALINA_HOME/common/lib目录下,或者在CLASSPATH中指定其位置也可

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

  1. <Resource name="jdbc/mysql" 
  2.        auth="Container" 
  3.        type="javax.sql.DataSource" 
  4.        driverClassName="com.mysql.jdbc.Driver" 
  5.        url="jdbc:mysql://localhost/mysql" 
  6.        username="root" 
  7.        password="password" 
  8.        maxActive="100" 
  9.        maxIdle="30" 
  10.        maxWait="10000" /> 
  1. <!-- maxActive: Maximum number of dB connections in pool. Make sure you 
  2.      configure your mysqld max_connections large enough to handle 
  3.      all of your db connections. Set to -1 for no limit. 
  4.      --> 
  5.  
  6. <!-- maxIdle: Maximum number of idle dB connections to retain in pool. 
  7.      Set to -1 for no limit.  See also the DBCP documentation on this 
  8.      and the minEvictableIdleTimeMillis configuration parameter. 
  9.      --> 
  10.  
  11. <!-- maxWait: Maximum time to wait for a dB connection to become available 
  12.      in ms, in this example 10 seconds. An Exception is thrown if 
  13.      this timeout is exceeded.  Set to -1 to wait indefinitely. 
  14.      --> 
  15.  
  16. <!-- username and password: MySQL dB username and password for dB connections  --> 
  17.  
  18. <!-- driverClassName: Class name for the old mm.mysql JDBC driver is 
  19.      org.gjt.mm.mysql.Driver - we recommend using Connector/J though. 
  20.      Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver. 
  21.      --> 
  22.  
  23. <!-- url: The JDBC connection url for connecting to your MySQL dB. 
  24.      --> 

3.  配置你的应用下的web.xml中的<web-app></web-app>之间加入(实际操作中,没做这步也通过了):

  1. <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. 测试代码:

  1. <!doctype html public "-//w3c//dtd html 4.0 transitional//en"  "http://www.w3.org/TR/REC-html40/strict.dtd">      
  2.   
  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=utf-8">      
  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();      
  19.     //提示:user必须是数据库已有的表,      
  20.     //这里的数据库前文提及的Data Source URL配置里包含的数据库。      
  21.     String strSql = "select * from user";      
  22.     ResultSet rs = stmt.executeQuery(strSql);      
  23.     while(rs.next()){      
  24.         out.print(rs.getString(1));                       
  25.     }      
  26.     out.print("我的测试结束");      
  27. }      
  28. catch(Exception ex){      
  29.     out.print("Err:"+ex.getMessage());      
  30.     ex.printStackTrace();      
  31. }      
  32. %>      
  33. </head>    
  34. <body>    
  35. </body>      
  36. </html>   

此文章由 flyinweb 于 2010-01-04 14:34:42 编辑

本日志由 flyinweb 于 2010-01-04 09:42:46 发表,目前已经被浏览 3818 次,评论 0 次;

作者添加了以下标签: mysql 连接池MySQL Connector/J

引用通告:http://www.517sou.net/Article/373/Trackback.ashx

评论订阅:http://www.517sou.net/Article/373/Feeds.ashx

相关文章

评论列表

    暂时没有评论
(必填)
(必填,不会被公开)