1、环境(我做测试时的环境)
1)tomcat 6.0.20
2)oracle版本:Oracle Database 10g Enterprise Edition Release 10.2.0.2.0 - 64bit
通过pl sql 查看oracle版本sql:select banner from sys.v_$version
3)jdk版本:1.5.0_16
4)oracle驱动jar包:ojdbc14-10.2.0.3.jar
5)如果出现这个错误 java.lang.NoClassDefFoundError:
oracle/dms/instrument/ExecutionContextForJDBC
则需要到oracle安装目录下拷dms.jar到%TOMCATHOME%\lib下
2、配置tomcat jndi数据源
1)%TOMCATHOME%\conf\context.xml
在<Context></Context>直接添加
<Resource name="jdbc/myoracle" auth="Container"
type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@localhost:billdb" username="orcl" password="orcl" maxActive="20"
maxIdle="10" maxWait="-1"/>
2)%TOMCATHOME%\conf\web.xml
在<web-app>下添加
<resource-ref>
<description>Oracle Datasource example</description>
<res-ref-name>jdbc/myoracle</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
3、Spring配置
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:/comp/env/jdbc/myoracle"></property>
</bean>
也可以通过jsp来测试
<%
out.print("测试连接池<br/>");
try{
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle");
Connection conn = ds.getConnection();
out.print("connection success!");
conn.close();
}
catch(Exception ex){
out.println("连接错误:");
out.print(ex.printStackTrace());
}
%>