tomcat 5.5 tomcat6.0配置连接池

本文章是针对mysql数据库的,如果更换其他数据库,可以修改相应的
url,username,password,driverClassName.

1.
在tomcat5.5中,需要修改server.xml,在<Host></Host>之间加入如下代码:

<Context path="/test_pool"  debug="0" reloadable="true" crossContext="true">    
<Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_quality_log." suffix=".txt" timestamp="true"/>    
  <Resource    
    name="jdbc/connectDB"           
    type="javax.sql.DataSource"   
    password="root"       
    driverClassName="com.mysql.jdbc.Driver"  
    maxIdle="2"    
    maxWait="5000"    
    username="root"  
    url="jdbc:mysql://localhost:3306/limits"    
    maxActive="4" />        
  
<ResourceParams name="jdbc/test">    
      
<parameter>    
  <name>removeAbandoned</name>    
  <!-- Abandoned DB connections are removed and recycled -->    
  <value>true</value>    
</parameter>    
<parameter>    
  <name>removeAbandonedTimeout</name>    
  <!-- Use the removeAbandonedTimeout parameter to set the number of seconds a DB connection has been idle before it is considered abandoned.  -->    
  <value>60</value>    
</parameter>    
<parameter>    
  <name>logAbandoned</name>    
  <!-- Log a stack trace of the code which abandoned -->    
  <value>false</value>    
</parameter>    
  
<parameter>    
  <name>factory</name>    
  <!--DBCP Basic Datasource Factory -->    
  <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>    
</parameter>    
  
</ResourceParams>
</Context>
  

然后将mysql-connector-java.jar复制到tomcat5.5目录 /common/lib中
重启tomcat即可.

2.tomcat6.0 在tomcat6.0中貌似不能使用<ResourceParams>的形式,我尝试了一下,总是失败,如果说的不对,还请大家指正.
也是在server.xml中修改 
在<Host></Host>标签之间加入如下代码:

<Context   path="/test_pool"     reloadable="true"   crossContext="true"> 
  <Resource 
   name="jdbc/connectDB"
   type="javax.sql.DataSource"
   password="root"
   driverClassName="com.mysql.jdbc.Driver"
   maxIdle="5"
   maxWait="5000"
   removeAbandoned="true"
   username="root"
   url="jdbc:mysql://localhost:3306/limits"
   maxActive="10"/>
  </Context>    




注:path="/test_pool"是要访问的工程名
name="jdbc/connectDB"是要访问的jndi名.

3.然后就可以测试了.
写如下的DBUtil类:
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DBUtil {

	static Connection conn;
	
	public static Connection getConnection(){
		Context ctx = null;
		Connection conn = null;
		try {
			ctx = new InitialContext();
			if(ctx == null){
				throw new Exception("没有匹配的数据库环境.");
			}
			System.out.println("*******************************");
			System.out.println(ctx);
			//DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/connectDB");
			System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
			ctx = (Context)ctx.lookup("java:/comp/env");
			DataSource ds = (DataSource)ctx.lookup("jdbc/connectDB");
			
			if(ds == null){
				throw new Exception("没有合适的数据源");
			}
			System.out.println(ds);
			conn = ds.getConnection();
			System.out.println(conn == null);
			if(conn == null){
				throw new Exception("创建连接失败");
			}
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
}



当然,这个类只是用来测试的,可能写的不是很完善.

然后在jsp页面上调用:

  Connection conn = DBUtil.getConnection();
	conn.setAutoCommit(false);
	Statement stmt = conn.createStatement();
	
	PreparedStatement pstmt = conn.prepareStatement(" insert into users(userId,userName , password , email) values (" +
			"? , ? , ? , ? )");
	pstmt.setString(1, "jackeysion");
	pstmt.setString(2,"张杰");
	pstmt.setString(3, "19871223");
	pstmt.setString(4, "[email protected]");
	int i = pstmt.executeUpdate();
	
	conn.commit();
	ResultSet rs = stmt.executeQuery("select * from users");
	pstmt.close();
	while(rs.next()){
		out.println(rs.getString(3) + "<br>");
	}
	conn.close();
	
//	if(i == 0){
//		throw new Exception("操作数据错误!!!");
//	}


上面是jsp页面中嵌入的代码.

另外 ,本人数据库名叫limits, 里面用到了个表叫users.
users中有这么几个字段: id(主键,自动增长),userId(varchar(50)),userName(varchar(500)),password(varchar(50)) , email(varchar(50),address(varchar(500)等.
测试的时候可以自己做一下.


附件中是mysql连接数据库的驱动包.

你可能感兴趣的:(apache,tomcat,mysql,SQL Server,jdbc)