Tomcat7中配置Oracle 11g数据库DBCP连接池

时间:2022-07-22 16:52:24
  1. 将       ojdbc6.jar       tomcat-jdbc-7.0.37.jar            拷贝到工程的WEB-INF\lib  下面

一、在Tomcat的配置文件Tomcat7.0\conf\context.xml中添加信息:

?
1
2
3
4
5
6
7
8
9
10
11
<? xml version = '1.0' encoding = 'utf-8' ?>
< Resource  name = "jdbcoracle"   auth = "Container"  type = "javax.sql.DataSource"
  factory = "org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
maxActive = "20"
maxIdle = "1"
maxWait = "100"
username = "test"
password = "test"
driverClassName = "oracle.jdbc.OracleDriver"
url = "jdbc:oracle:thin:@localhost:1521:orcl" >  
</ Context >

注意这里的driverClassName写的是oracle.jdbc.OracleDriver。oracle10之前的driverClassName写的oracle.jdbc.driver.OracleDriver

二、 在$CATALINA_HOME/webapps/应用名称/WEB-INF/web.xml的前添加如下信息:

?
1
2
3
4
5
6
< resource-ref >
< description >databaseConnection</ description >
< res-ref-name >jdbcoracle </ res-ref-name >
< res-type >javax.sql.DataSource</ res-type >
< res-auth >Container</ res-auth >
</ resource-ref >

其中中的参数名必须和context.xml中声明的连接名一样。

 

三、在jsp中测试connection

?
1
2
3
4
5
6
7
8
9
10
11
<%@ page language= "java" pageEncoding= "UTF-8" % >
<%@ page import = " java.util.*,java.sql.*" % >
<%@ page import = "java.util.Hashtable,javax.sql.DataSource " % >
<%@ page import = " javax.naming.Context,javax.naming.InitialContext" % >
<%
     InitialContext  ctx = new InitialContext();
//通过JNDI查找DataSource
     DataSource ds = (DataSource) ctx.lookup( "java:comp/env/jdbcoracle" );
     Connection conn = ds.getConnection();
     out.println( "数据源测试联接成功!" );
% >