Tomcat6.0配置C3P0连接池(Oracle)

时间:2021-06-27 09:14:05

1、链接地址:http://sourceforge.net/projects/c3p0/

下载最新的C3P0版本

2、将c3p0-0.9.1.2.jar放到toamcat/lib/下面

3、在conf中server.xml中<GlobalNamingResources>和</GlobalNamingResources>之间添加

   <Resource auth="Container" 
    description="DB Connection" 
    driverClass="oracle.jdbc.driver.OracleDriver" 
    maxPoolSize="10" 
    minPoolSize="2" 
    acquireIncrement="2" 
    name="jdbc/oratest"
    user="user" 
    password="user" 
    factory="org.apache.naming.factory.BeanFactory" 
    type="com.mchange.v2.c3p0.ComboPooledDataSource" 
    jdbcUrl="jdbc:oracle:thin:@192.168.0.0:1521:wwhdb" />

4、在web.xml中</web-app>前添加

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

5、测试网页

<%@ page import="java.sql.*"%> 
<%@ page import="javax.sql.*"%> 
<%@ page import="javax.naming.*"%> 
<%//! 
//final String JNDINAME = "java:comp/env/jdbc/oratest" ; 
%> 
<%

     Connection conn=null;
     try{
         InitialContext ctx = new InitialContext(); 
         DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/oratest"); 
         conn=ds.getConnection();
     }catch(NamingException ex){
         ex.printStackTrace();
     }
     out.println("111111");
     String sql="select * from msm002";
     PreparedStatement ps=conn.prepareStatement(sql);
     ResultSet rs=ps.executeQuery();
     while(rs.next()){
     %>
         字段1:<%=rs.getString(1)%> 字段2:<%=rs.getString(3)%><br>
     <%
     }
     if(rs!=null){
         rs.close();
         rs=null;
     }
     if(ps!=null){
         ps.close();
         ps=null;
     }
     if(conn!=null){
         conn.close();
         conn=null;
     }

%>