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

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

环境:
linux AS4 U5
Tomcat5.5.28
mysql 5.1.X

配置步骤:

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

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

<Resource name="jdbc/mysql" 
       auth="Container" 
       type="javax.sql.DataSource" 
       driverClassName="com.mysql.jdbc.Driver" 
       url="jdbc:mysql://localhost/mysql" 
       username="root" 
       password="password" 
       maxActive="100" 
       maxIdle="30" 
       maxWait="10000" /> 

<!-- maxActive: Maximum number of dB connections in pool. Make sure you 
     configure your mysqld max_connections large enough to handle 
     all of your db connections. Set to -1 for no limit. 
     --> 
 
<!-- maxIdle: Maximum number of idle dB connections to retain in pool. 
     Set to -1 for no limit.  See also the DBCP documentation on this 
     and the minEvictableIdleTimeMillis configuration parameter. 
     --> 
 
<!-- maxWait: Maximum time to wait for a dB connection to become available 
     in ms, in this example 10 seconds. An Exception is thrown if 
     this timeout is exceeded.  Set to -1 to wait indefinitely. 
     --> 
 
<!-- username and password: MySQL dB username and password for dB connections  --> 
 
<!-- driverClassName: Class name for the old mm.mysql JDBC driver is 
     org.gjt.mm.mysql.Driver - we recommend using Connector/J though. 
     Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver. 
     --> 
 
<!-- url: The JDBC connection url for connecting to your MySQL dB. 
     --> 

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

<resource-ref>    
    <description>DB Connection</description>    
    <res-ref-name>jdbc/mysqlx</res-ref-name>    
    <res-type>javax.sql.DataSource</res-type>    
    <res-auth>Container</res-auth>    
</resource-ref>   

测试代码:

<!doctype html public "-//w3c//dtd html 4.0 transitional//en"  "http://www.w3.org/TR/REC-html40/strict.dtd">      
  
<%@ page import="java.sql.*"%>      
<%@ page import="javax.sql.*"%>      
<%@ page import="javax.naming.*"%>      
<%@ page session="false" %>      
<html>      
<head>      
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">      
<title></title>      
<%       
out.print("我的测试开始");      
DataSource ds = null;      
try{      
    InitialContext ctx=new InitialContext();      
    ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql");      
    Connection conn = ds.getConnection();      
    Statement stmt = conn.createStatement();      
    //提示:user必须是数据库已有的表,      
    //这里的数据库前文提及的Data Source URL配置里包含的数据库。      
    String strSql = "select * from user";      
    ResultSet rs = stmt.executeQuery(strSql);      
    while(rs.next()){      
        out.print(rs.getString(1));                       
    }      
    out.print("我的测试结束");      
}      
catch(Exception ex){      
    out.print("Err:"+ex.getMessage());      
    ex.printStackTrace();      
}      
%>      
</head>    
<body>    
</body>      
</html>   

发表回复